ShowAI.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class ShowAI : CraftAI
  5. {
  6. public Vector2 origin;
  7. // Use this for initialization
  8. override protected void Start ()
  9. {
  10. base.Start();
  11. checkTimeInterval = 2f;
  12. }
  13. // Update is called once per frame
  14. protected virtual void FixedUpdate ()
  15. {
  16. if (owner.IsCtrl ())
  17. ManualCheck ();
  18. else
  19. AICheck ();
  20. }
  21. private void ManualCheck()
  22. {
  23. if(owner.IsDead())
  24. {
  25. return;
  26. }
  27. if(battleController.isGameOver)
  28. {
  29. return;
  30. }
  31. if(CheckDead())
  32. {
  33. return;
  34. }
  35. if(owner.GetSwapManager().isPreparingSwap)
  36. {
  37. Debuger.Log("swap prapare time : "+owner.GetSwapManager().GetPreparingSwapTime());
  38. if(owner.GetSwapManager().IsReadySwap())
  39. {
  40. battleController.GetMessageManager().RequestSwap(owner.id, owner.GetSwapManager().GetTargetSwapId());
  41. owner.GetSwapManager().RequestedSwap();
  42. }
  43. }
  44. else if(GameTime.time-lastCheckTime >= checkTimeInterval)
  45. {
  46. lastCheckTime = GameTime.time;
  47. CheckAction();
  48. }
  49. }
  50. private void CheckAction()
  51. {
  52. if(owner.target != null)
  53. {
  54. float targetDis = NumberUtil.distanceVector3(owner.position, owner.target.position, true);
  55. Power attack = owner.GetPowerManager().GetAttack();
  56. if(!owner.target.CanShoot())
  57. {
  58. owner.target = null;
  59. }
  60. else if(targetDis > owner.viewRange)
  61. {
  62. owner.target = null;
  63. }
  64. else if(targetDis > attack.GetDistance())
  65. {
  66. if(!owner.isMoving)
  67. {
  68. Vector3 targetPos = owner.target.position;
  69. owner.MoveTo(targetPos.x, targetPos.z);
  70. }
  71. }
  72. else if(targetDis <= attack.GetDistance())
  73. {
  74. if(owner.isMoving && !owner.forceMove)
  75. owner.StopMove();
  76. owner.GetPowerManager().AttempUserPower(attack.GetId(), battleController);
  77. }
  78. }
  79. else
  80. {
  81. FindTarget();
  82. if(owner.target == null)
  83. {
  84. CheckOccupy();
  85. }
  86. }
  87. }
  88. protected override bool CheckOccupy ()
  89. {
  90. if(!base.CheckOccupy())
  91. return false;
  92. CrystalBase targetCrystal = battleController.GetMap().GetInRangeCrystalBase(owner.position);
  93. if(targetCrystal == null || targetCrystal.GetStation() != null)
  94. return false;
  95. List<BattleObject> list = map.GetBattleObjectByRange(targetCrystal.position, targetCrystal.range, TeamUtil.GetOpponentTeam(owner.team));
  96. if(list.Count > 0)
  97. return false;
  98. if(owner && targetCrystal != null)
  99. battleController.GetMessageManager().OccupyStation(owner, targetCrystal);
  100. return true;
  101. }
  102. private void AICheck()
  103. {
  104. if(CheckDead())
  105. {
  106. return;
  107. }
  108. if(GameTime.time-lastCheckTime >= checkTimeInterval)
  109. {
  110. lastCheckTime = GameTime.time;
  111. if(origin.x == 0)
  112. origin.x = owner.col;
  113. if(origin.y == 0)
  114. origin.y = owner.row;
  115. if(!owner.isMoving)
  116. {
  117. // int col = (int)origin.x + Random.Range(-1, 2);
  118. // int row = (int)origin.y + Random.Range(-1, 2);
  119. if ((int)origin.x != owner.col && (int)origin.y != owner.row) {
  120. owner.MoveTo ((int)origin.x, (int)origin.y);
  121. } else if (Random.Range (0, 100) > 50) {
  122. float angle = owner.rotateMoveTargetAngle + (Random.Range(0, 100) > 50 ? 1 : -1) * Random.Range (45f, 90f);
  123. angle = NumberUtil.corverAngleBetween (angle, -180, 180f);
  124. owner.Rotate (angle);
  125. }
  126. }
  127. }
  128. else
  129. {
  130. return;
  131. }
  132. }
  133. }