AI.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class AI : MonoBehaviour
  5. {
  6. public enum AIType
  7. {
  8. None,
  9. Manual,
  10. Hero,
  11. Soldier,
  12. Station,
  13. Door,
  14. Remote,
  15. Show,
  16. }
  17. protected BattleController battleController;
  18. protected Map map;
  19. protected BattleObject _owner;
  20. protected Player player;
  21. protected float lastCheckTime = 0;
  22. protected float checkTimeInterval = 0.5f;
  23. protected bool sendDeathHold = false;
  24. protected float lastSendDeathTime = 0f;
  25. public void init(BattleController battleController)
  26. {
  27. this.battleController = battleController;
  28. this.map = battleController.GetMap();
  29. }
  30. // Use this for initialization
  31. protected virtual void Start ()
  32. {
  33. _owner = GetComponent<BattleObject>();
  34. if(battleController.isReplay)
  35. {
  36. enabled = false;
  37. }
  38. }
  39. public virtual void SetPlayer(Player player)
  40. {
  41. this.player = player;
  42. }
  43. public virtual Player GetPlayer()
  44. {
  45. return player;
  46. }
  47. public bool CanSendMessage()
  48. {
  49. if(player == null)
  50. return false;
  51. if(!battleController.IsAITaker() || battleController.isGameOver || !battleController.isBattleStart || _owner.IsDead())
  52. {
  53. return false;
  54. }
  55. else
  56. {
  57. return true;
  58. }
  59. if(player.isMe)
  60. return true;
  61. return false;
  62. }
  63. protected virtual bool CheckDead()
  64. {
  65. if(sendDeathHold && GameTime.time-lastSendDeathTime < 5f)
  66. return true;
  67. if(!sendDeathHold || GameTime.time-lastSendDeathTime>5f)//wait 5s to get response
  68. {
  69. if(_owner.hp == 0)
  70. {
  71. sendDeathHold = true;
  72. lastSendDeathTime = GameTime.time;
  73. battleController.GetMessageManager().Dead(_owner, _owner.lastShooterId, _owner.lastShooterCraftId);
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. protected virtual void FindTarget()
  80. {
  81. BattleObject nearestCraft = null;
  82. float nearestDistance = _owner.viewRange;
  83. List<BattleObject> list = map.GetBattleObjectByRange(_owner.position, _owner.viewRange, TeamUtil.GetOpponentTeam(_owner.team));
  84. for (int i=0; i<list.Count; i++)
  85. {
  86. BattleObject battleObj = list[i];
  87. if(!battleObj.CanShoot() || battleObj.overwhelming)
  88. continue;
  89. float distance = NumberUtil.distanceVector3(_owner.position, battleObj.position);
  90. if(distance < nearestDistance)
  91. {
  92. nearestDistance = distance;
  93. nearestCraft = battleObj;
  94. }
  95. }
  96. _owner.target = nearestCraft;
  97. }
  98. protected virtual void FindTargetAllMap()
  99. {
  100. BattleObject nearestCraft = null;
  101. float nearestDistance = float.MaxValue;
  102. List<BattleObject> list = map.GetBattleObjectByRange(_owner.position, nearestDistance, TeamUtil.GetOpponentTeam(_owner.team));
  103. for (int i=0; i<list.Count; i++)
  104. {
  105. BattleObject battleObj = list[i];
  106. if(!battleObj.CanShoot() || battleObj.overwhelming)
  107. continue;
  108. float distance = NumberUtil.distanceVector3(_owner.position, battleObj.position);
  109. if(distance < nearestDistance)
  110. {
  111. nearestDistance = distance;
  112. nearestCraft = battleObj;
  113. }
  114. }
  115. _owner.target = nearestCraft;
  116. }
  117. }