123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class ShotManager : MonoBehaviour {
- public Transform[] attackTransArr;
- public GameObject[] attackMuzzleArr;
- public ParticleSystem[] attackShellArr;
- public int attackShotCount;
- public float[] attackShotTimeArr;
- public float attackShotTime;
- public float attackMuzzleDuration = 0.05f;
- public float attackSoundDuration = float.MaxValue;
- public Animator attackAnimator;
- public Transform[] powerTransArr;
- public GameObject[] powerMuzzleArr;
- public ParticleSystem[] powerShellArr;
- public int powerShotCount;
- public float[] powerShotTimeArr;
- public float powerShotTime;
- public float powerMuzzleDuration = 0.05f;
- public float powerSoundDuration = float.MaxValue;
- public Animator powerAnimator;
- private List<Bullet> list = new List<Bullet>();
- private bool hasMuzzle = true;
- void Awake()
- {
- if(attackAnimator != null)
- attackAnimator.gameObject.SetActive(false);
- if(powerAnimator != null)
- powerAnimator.gameObject.SetActive(false);
- }
- public void Fire(Power power, List<Bullet> bulletList)
- {
- if (bulletList.Count > 0 && power.GetOwner() is Craft && power.GetId () == power.GetOwner ().GetPowerManager ().GetAttack ().GetId ()) {
- UAVehicle uav = (power.GetOwner () as Craft).GetUAV ();
- if(uav != null)
- {
- Bullet bullet = bulletList [0];
- uav.Attack (bullet.GetTarget() as Craft);
- }
- }
- for(int i=0; i<bulletList.Count; i++)
- {
- Bullet bullet = bulletList[i];
- bullet.gameObject.SetActive(false);
- list.Add(bullet);
- }
- }
- void FixedUpdate()
- {
- CheckBullet();
- CheckMuzzle();
- }
- private void CheckBullet()
- {
- if(list.Count == 0)
- return;
-
- for(int i=list.Count-1; i>=0; i--)
- {
- Bullet bullet = list[i];
- bool isAttack = bullet.GetPower().isAttack;
- float lastShotTime = bullet.GetPower().isAttack ? attackShotTime : powerShotTime;
- float interval = bullet.GetPower().GetButtetsInterval();
- if(GameTime.time >= lastShotTime + interval)
- {
- Launch(bullet);
- list.RemoveAt(i);
- }
- }
- }
- private void Launch(Bullet bullet)
- {
- Vector3 position = new Vector3();
- bool isAttack = bullet.GetPower().isAttack;
- Transform[] transArr = isAttack ? attackTransArr : powerTransArr;
- GameObject[] muzzleArr = isAttack ? attackMuzzleArr : powerMuzzleArr;
- ParticleSystem[] shellArr = isAttack ? attackShellArr : powerShellArr;
- float[] shotArr = isAttack ? attackShotTimeArr : powerShotTimeArr;
- int shotCount = isAttack ? attackShotCount : powerShotCount;
- float soundDuration = isAttack ? attackSoundDuration : powerSoundDuration;
- Animator animator = isAttack ? attackAnimator : powerAnimator;
- Transform shotTrans = null;
- if(transArr.Length > 0)
- {
- int index = shotCount % transArr.Length;
- shotTrans = transArr[index];
- position = transArr[index].position;
- if(index < muzzleArr.Length && muzzleArr[index] != null)
- {
- muzzleArr[index].SetActive(true);
- shotArr[index] = GameTime.time;
- }
- if(index < shellArr.Length && shellArr[index] != null)
- {
- shellArr[index].Emit(1);
- }
- hasMuzzle = true;
- }
- else
- {
- position = this.transform.position;
- position.y = 1.5f;
- }
- if(animator != null)
- {
- animator.gameObject.SetActive(true);
- animator.Play(0, 0, 0);
- }
- if(isAttack)
- {
- attackShotTime = GameTime.time;
- attackShotCount++;
- }
- else
- {
- powerShotTime = GameTime.time;
- powerShotCount++;
- }
- bullet.gameObject.SetActive(true);
- bullet.transform.position = position;
- bullet.InitRotation();
- bullet.shotTrans = shotTrans;
- bullet.GetPower().PlaySound(soundDuration);
- }
- private void CheckMuzzle()
- {
- if(!hasMuzzle)
- return;
- bool found = false;
- for(int i=0; i<attackMuzzleArr.Length; i++)
- {
- GameObject muzzle = attackMuzzleArr[i];
- if(muzzle != null && muzzle.activeInHierarchy)
- {
- float duration = GameTime.time - attackShotTimeArr[i];
- if(duration >= attackMuzzleDuration)
- {
- muzzle.SetActive(false);
- }
- else
- {
- found = true;
- }
- }
- }
- for(int i=0; i<powerMuzzleArr.Length; i++)
- {
- GameObject muzzle = powerMuzzleArr[i];
- if(muzzle != null && muzzle.activeInHierarchy)
- {
- float duration = GameTime.time - powerShotTimeArr[i];
- if(duration >= powerMuzzleDuration)
- {
- muzzle.SetActive(false);
- }
- else
- {
- found = true;
- }
- }
- }
- if(!found)
- hasMuzzle = false;
- }
- }
|