UAVehicle.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class UAVehicle : BattleObject, IPowerOwner
  5. {
  6. public const float MAX_ATTACK_RANGE = 10f;
  7. public const float MIN_FOLLOW_DISTANCE = 2f;
  8. private PowerManager powerManager;
  9. private Craft owner;
  10. private UAVItem uavItem;
  11. private float moveSpeed = 1f;
  12. public void Init (Map map, Craft owner, UAVItem uavItem)
  13. {
  14. this.map = map;
  15. this.owner = owner;
  16. this.userId = owner.userId;
  17. position = owner.position;
  18. map.AddUAVehicle (this);
  19. this.uavItem = uavItem;
  20. if (powerManager == null) {
  21. powerManager = new PowerManager (this, uavItem.GetData().attack, null, null);
  22. } else {
  23. powerManager.UpdatePower (uavItem.GetData().attack, null);
  24. }
  25. }
  26. public void Attack(Craft target)
  27. {
  28. if(target != null)
  29. {
  30. Power attack = GetPowerManager ().GetAttack ();
  31. if (target.team == owner.team) {
  32. if (TargetType.IsContainTarget (TargetType.ALLY, attack.targetType)) {
  33. this.target = target;
  34. return;
  35. }
  36. } else if (target.team != owner.team) {
  37. if (TargetType.IsContainTarget (TargetType.ENEMY, attack.targetType)) {
  38. this.target = target;
  39. return;
  40. }
  41. }
  42. }
  43. this.target = null;
  44. }
  45. public float GetDamage()
  46. {
  47. float value = uavItem.GetEquipment ().GetDmg ();
  48. if (value > 0)
  49. return value;
  50. value = uavItem.GetEquipment ().GetHp ();
  51. return value;
  52. }
  53. void Update()
  54. {
  55. if(IsDead())
  56. {
  57. return;
  58. }
  59. if(owner.IsDead())
  60. {
  61. Dead ();
  62. return;
  63. }
  64. if(target == null)
  65. {
  66. target = owner;
  67. }
  68. if (Vector3.Distance (target.position, this.position) > MIN_FOLLOW_DISTANCE) {
  69. this.position = Vector3.Lerp (this.position, owner.position, GameTime.deltaTime * moveSpeed);
  70. } else {
  71. if(CanAttack()){
  72. GetPowerManager ().AttempUserPower (GetPowerManager ().GetAttack ().GetId (),
  73. Session.GetInstance ().GetBattleSession ().GetBattleController ());
  74. }
  75. }
  76. if(Vector3.Distance(owner.position, this.position) > MAX_ATTACK_RANGE)
  77. {
  78. target = owner;
  79. }
  80. Rotate ();
  81. }
  82. private bool CanAttack()
  83. {
  84. if (owner.IsCtrl () || (owner.userId < 0 && Session.GetInstance ().GetBattleSession ().GetBattleController ().IsAITaker ())) {
  85. Craft craft = target as Craft;
  86. Power attack = GetPowerManager ().GetAttack ();
  87. uint targetType = TargetType.NONE;
  88. if (craft.team == owner.team)
  89. targetType = TargetType.ALLY;
  90. else if (craft.team != owner.team)
  91. targetType = TargetType.ENEMY;
  92. if(TargetType.IsContainTarget(targetType, attack.targetType)){
  93. if(attack.GetPowerEffect() == Power.PowerEffect.Heal && craft.hp == craft.maxHp)
  94. {
  95. return false;
  96. }
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. private void Rotate()
  103. {
  104. if (target == null)
  105. return;
  106. Vector3 pos = target.position - this.position;
  107. pos.y = 0;
  108. if (pos.magnitude > 0) {
  109. Quaternion newRotation = Quaternion.LookRotation (pos);
  110. this.transform.rotation = newRotation;
  111. }
  112. }
  113. public PowerManager GetPowerManager()
  114. {
  115. return powerManager;
  116. }
  117. public void Teleport(int step)
  118. {
  119. }
  120. public override void Dead ()
  121. {
  122. if(_isDead)
  123. return;
  124. deadPos = position;
  125. _isDead = true;
  126. deadTime = GameTime.time;
  127. map.RemoveUAVehicle (this);
  128. Destroy (this.gameObject);
  129. }
  130. }