ShotManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class ShotManager : MonoBehaviour {
  5. public Transform[] attackTransArr;
  6. public GameObject[] attackMuzzleArr;
  7. public ParticleSystem[] attackShellArr;
  8. public int attackShotCount;
  9. public float[] attackShotTimeArr;
  10. public float attackShotTime;
  11. public float attackMuzzleDuration = 0.05f;
  12. public float attackSoundDuration = float.MaxValue;
  13. public Animator attackAnimator;
  14. public Transform[] powerTransArr;
  15. public GameObject[] powerMuzzleArr;
  16. public ParticleSystem[] powerShellArr;
  17. public int powerShotCount;
  18. public float[] powerShotTimeArr;
  19. public float powerShotTime;
  20. public float powerMuzzleDuration = 0.05f;
  21. public float powerSoundDuration = float.MaxValue;
  22. public Animator powerAnimator;
  23. private List<Bullet> list = new List<Bullet>();
  24. private bool hasMuzzle = true;
  25. void Awake()
  26. {
  27. if(attackAnimator != null)
  28. attackAnimator.gameObject.SetActive(false);
  29. if(powerAnimator != null)
  30. powerAnimator.gameObject.SetActive(false);
  31. }
  32. public void Fire(Power power, List<Bullet> bulletList)
  33. {
  34. if (bulletList.Count > 0 && power.GetOwner() is Craft && power.GetId () == power.GetOwner ().GetPowerManager ().GetAttack ().GetId ()) {
  35. UAVehicle uav = (power.GetOwner () as Craft).GetUAV ();
  36. if(uav != null)
  37. {
  38. Bullet bullet = bulletList [0];
  39. uav.Attack (bullet.GetTarget() as Craft);
  40. }
  41. }
  42. for(int i=0; i<bulletList.Count; i++)
  43. {
  44. Bullet bullet = bulletList[i];
  45. bullet.gameObject.SetActive(false);
  46. list.Add(bullet);
  47. }
  48. }
  49. void FixedUpdate()
  50. {
  51. CheckBullet();
  52. CheckMuzzle();
  53. }
  54. private void CheckBullet()
  55. {
  56. if(list.Count == 0)
  57. return;
  58. for(int i=list.Count-1; i>=0; i--)
  59. {
  60. Bullet bullet = list[i];
  61. bool isAttack = bullet.GetPower().isAttack;
  62. float lastShotTime = bullet.GetPower().isAttack ? attackShotTime : powerShotTime;
  63. float interval = bullet.GetPower().GetButtetsInterval();
  64. if(GameTime.time >= lastShotTime + interval)
  65. {
  66. Launch(bullet);
  67. list.RemoveAt(i);
  68. }
  69. }
  70. }
  71. private void Launch(Bullet bullet)
  72. {
  73. Vector3 position = new Vector3();
  74. bool isAttack = bullet.GetPower().isAttack;
  75. Transform[] transArr = isAttack ? attackTransArr : powerTransArr;
  76. GameObject[] muzzleArr = isAttack ? attackMuzzleArr : powerMuzzleArr;
  77. ParticleSystem[] shellArr = isAttack ? attackShellArr : powerShellArr;
  78. float[] shotArr = isAttack ? attackShotTimeArr : powerShotTimeArr;
  79. int shotCount = isAttack ? attackShotCount : powerShotCount;
  80. float soundDuration = isAttack ? attackSoundDuration : powerSoundDuration;
  81. Animator animator = isAttack ? attackAnimator : powerAnimator;
  82. Transform shotTrans = null;
  83. if(transArr.Length > 0)
  84. {
  85. int index = shotCount % transArr.Length;
  86. shotTrans = transArr[index];
  87. position = transArr[index].position;
  88. if(index < muzzleArr.Length && muzzleArr[index] != null)
  89. {
  90. muzzleArr[index].SetActive(true);
  91. shotArr[index] = GameTime.time;
  92. }
  93. if(index < shellArr.Length && shellArr[index] != null)
  94. {
  95. shellArr[index].Emit(1);
  96. }
  97. hasMuzzle = true;
  98. }
  99. else
  100. {
  101. position = this.transform.position;
  102. position.y = 1.5f;
  103. }
  104. if(animator != null)
  105. {
  106. animator.gameObject.SetActive(true);
  107. animator.Play(0, 0, 0);
  108. }
  109. if(isAttack)
  110. {
  111. attackShotTime = GameTime.time;
  112. attackShotCount++;
  113. }
  114. else
  115. {
  116. powerShotTime = GameTime.time;
  117. powerShotCount++;
  118. }
  119. bullet.gameObject.SetActive(true);
  120. bullet.transform.position = position;
  121. bullet.InitRotation();
  122. bullet.shotTrans = shotTrans;
  123. bullet.GetPower().PlaySound(soundDuration);
  124. }
  125. private void CheckMuzzle()
  126. {
  127. if(!hasMuzzle)
  128. return;
  129. bool found = false;
  130. for(int i=0; i<attackMuzzleArr.Length; i++)
  131. {
  132. GameObject muzzle = attackMuzzleArr[i];
  133. if(muzzle != null && muzzle.activeInHierarchy)
  134. {
  135. float duration = GameTime.time - attackShotTimeArr[i];
  136. if(duration >= attackMuzzleDuration)
  137. {
  138. muzzle.SetActive(false);
  139. }
  140. else
  141. {
  142. found = true;
  143. }
  144. }
  145. }
  146. for(int i=0; i<powerMuzzleArr.Length; i++)
  147. {
  148. GameObject muzzle = powerMuzzleArr[i];
  149. if(muzzle != null && muzzle.activeInHierarchy)
  150. {
  151. float duration = GameTime.time - powerShotTimeArr[i];
  152. if(duration >= powerMuzzleDuration)
  153. {
  154. muzzle.SetActive(false);
  155. }
  156. else
  157. {
  158. found = true;
  159. }
  160. }
  161. }
  162. if(!found)
  163. hasMuzzle = false;
  164. }
  165. }