ShotManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. for(int i=0; i<bulletList.Count; i++)
  35. {
  36. Bullet bullet = bulletList[i];
  37. bullet.gameObject.SetActive(false);
  38. list.Add(bullet);
  39. }
  40. }
  41. void FixedUpdate()
  42. {
  43. CheckBullet();
  44. CheckMuzzle();
  45. }
  46. private void CheckBullet()
  47. {
  48. if(list.Count == 0)
  49. return;
  50. for(int i=list.Count-1; i>=0; i--)
  51. {
  52. Bullet bullet = list[i];
  53. bool isAttack = bullet.GetPower().isAttack;
  54. float lastShotTime = bullet.GetPower().isAttack ? attackShotTime : powerShotTime;
  55. float interval = bullet.GetPower().GetButtetsInterval();
  56. if(GameTime.time >= lastShotTime + interval)
  57. {
  58. Launch(bullet);
  59. list.RemoveAt(i);
  60. }
  61. }
  62. }
  63. private void Launch(Bullet bullet)
  64. {
  65. Vector3 position = new Vector3();
  66. bool isAttack = bullet.GetPower().isAttack;
  67. Transform[] transArr = isAttack ? attackTransArr : powerTransArr;
  68. GameObject[] muzzleArr = isAttack ? attackMuzzleArr : powerMuzzleArr;
  69. ParticleSystem[] shellArr = isAttack ? attackShellArr : powerShellArr;
  70. float[] shotArr = isAttack ? attackShotTimeArr : powerShotTimeArr;
  71. int shotCount = isAttack ? attackShotCount : powerShotCount;
  72. float soundDuration = isAttack ? attackSoundDuration : powerSoundDuration;
  73. Animator animator = isAttack ? attackAnimator : powerAnimator;
  74. Transform shotTrans = null;
  75. if(transArr.Length > 0)
  76. {
  77. int index = shotCount % transArr.Length;
  78. shotTrans = transArr[index];
  79. position = transArr[index].position;
  80. if(index < muzzleArr.Length && muzzleArr[index] != null)
  81. {
  82. muzzleArr[index].SetActive(true);
  83. shotArr[index] = GameTime.time;
  84. }
  85. if(index < shellArr.Length && shellArr[index] != null)
  86. {
  87. shellArr[index].Emit(1);
  88. }
  89. hasMuzzle = true;
  90. }
  91. else
  92. {
  93. position = this.transform.position;
  94. position.y = 1.5f;
  95. }
  96. if(animator != null)
  97. {
  98. animator.gameObject.SetActive(true);
  99. animator.Play(0, 0, 0);
  100. }
  101. if(isAttack)
  102. {
  103. attackShotTime = GameTime.time;
  104. attackShotCount++;
  105. }
  106. else
  107. {
  108. powerShotTime = GameTime.time;
  109. powerShotCount++;
  110. }
  111. bullet.gameObject.SetActive(true);
  112. bullet.transform.position = position;
  113. bullet.InitRotation();
  114. bullet.shotTrans = shotTrans;
  115. bullet.GetPower().PlaySound(soundDuration);
  116. }
  117. private void CheckMuzzle()
  118. {
  119. if(!hasMuzzle)
  120. return;
  121. bool found = false;
  122. for(int i=0; i<attackMuzzleArr.Length; i++)
  123. {
  124. GameObject muzzle = attackMuzzleArr[i];
  125. if(muzzle != null && muzzle.activeInHierarchy)
  126. {
  127. float duration = GameTime.time - attackShotTimeArr[i];
  128. if(duration >= attackMuzzleDuration)
  129. {
  130. muzzle.SetActive(false);
  131. }
  132. else
  133. {
  134. found = true;
  135. }
  136. }
  137. }
  138. for(int i=0; i<powerMuzzleArr.Length; i++)
  139. {
  140. GameObject muzzle = powerMuzzleArr[i];
  141. if(muzzle != null && muzzle.activeInHierarchy)
  142. {
  143. float duration = GameTime.time - powerShotTimeArr[i];
  144. if(duration >= powerMuzzleDuration)
  145. {
  146. muzzle.SetActive(false);
  147. }
  148. else
  149. {
  150. found = true;
  151. }
  152. }
  153. }
  154. if(!found)
  155. hasMuzzle = false;
  156. }
  157. }