CraftAI.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class CraftAI : AI
  5. {
  6. public bool followTarget;
  7. protected float lastCheckOccupyTime;
  8. protected float checkOccupyInterval = 3f;
  9. // Use this for initialization
  10. override protected void Start ()
  11. {
  12. base.Start();
  13. owner.MoveStepComplete += CheckGetItem;
  14. }
  15. protected virtual void OnDestroy()
  16. {
  17. owner.MoveStepComplete -= CheckGetItem;
  18. }
  19. protected Craft owner
  20. {
  21. get{
  22. return _owner as Craft;
  23. }
  24. }
  25. protected virtual bool CheckOccupy()
  26. {
  27. if(GameTime.time - lastCheckOccupyTime > checkOccupyInterval)
  28. {
  29. lastCheckOccupyTime = GameTime.time;
  30. return true;
  31. }
  32. return false;
  33. }
  34. protected void CheckGetItem()
  35. {
  36. if(!CanSendMessage())
  37. {
  38. return;
  39. }
  40. if(!owner.IsHero())
  41. return;
  42. List<MapItem> itemList = battleController.GetMap().GetMapItemByGrid(owner.col, owner.row);
  43. if(itemList != null && itemList.Count > 0)
  44. {
  45. MapItem mapItem = itemList[0];
  46. if(mapItem is FlagItem)
  47. DealFlag(mapItem as FlagItem);
  48. else
  49. battleController.GetMessageManager().GetItem(player, owner.id, mapItem.id);
  50. }
  51. }
  52. private void DealFlag(FlagItem flagItem)
  53. {
  54. if(flagItem.team == owner.team)
  55. {
  56. if(flagItem.IsInBase())
  57. {
  58. FlagItem oppFlagItem = battleController.GetMap().GetFlag(TeamUtil.GetOpponentTeam(flagItem.team));
  59. if(oppFlagItem.linkedCraftId == owner.id)
  60. battleController.GetMessageManager().CaptureFlag(player, owner);
  61. }
  62. else
  63. {
  64. battleController.GetMessageManager().ReturnFlag(player, owner, flagItem);
  65. }
  66. }
  67. else
  68. battleController.GetMessageManager().GetFlag(player, owner, flagItem);
  69. }
  70. protected void FindMaxThreatTarget()
  71. {
  72. if(map.HasFlag())
  73. {
  74. FlagItem myFlag = map.GetFlag(owner.team);
  75. if(myFlag.linkedCraftId != -1)
  76. {
  77. owner.target = map.GetBattleObject(myFlag.linkedCraftId);
  78. return;
  79. }
  80. }
  81. else if(map.id == MapData.MapID.Challenge)
  82. {
  83. List<CrystalBase> list = battleController.GetMap().GetCrystalBaseList();
  84. for(int i=0; i<list.Count; i++)
  85. {
  86. Station station = list[i].GetStation();
  87. if(station != null && station.team != owner.team)
  88. {
  89. owner.target = station;
  90. return;
  91. }
  92. }
  93. }
  94. int id = owner.GetThreadManager().GetMaxThreatId();
  95. BattleObject battleObj = battleController.GetMap().GetBattleObject(id);
  96. if(battleObj != null)
  97. {
  98. if(battleObj.IsDead())
  99. {
  100. owner.GetThreadManager().RemoveThreat(id);
  101. }
  102. else
  103. {
  104. owner.target = battleObj;
  105. }
  106. }
  107. }
  108. }