public virtual void Primary() { if (!canUse) { return; } if (state == RangedWeaponState.Idle) { if (Ammo != null && Ammo.AmmoLeft != 0) { if (OnPrimary != null) { OnPrimary(this, new EventArgs()); } state = RangedWeaponState.Firing; StartCoroutine(FiringBehavior()); if (useAmmo) { Ammo.UseAmmo(consumedAmmoPerShot); } } else { Debug.Log("Out of ammo !"); } } }
private IEnumerator FiringBehavior() { float halfDeviation = projectileDeviation / 2.0f; IDamageable dmg = null; Vector3 worldhp = Vector3.zero; if (aimGatherer == null) { Debug.LogError("No aim gatherer present for " + this.name); } else { worldhp = aimGatherer.GetAimHitpoint(ref dmg); } bool isAngleValid = Vector3.Dot(fireMuzzle.forward, (worldhp - fireMuzzle.position).normalized) >= 0.0f; for (int i = 0; i < projectilePerShot; i++) { if (!isAngleValid && dmg == null) { Debug.Log("Invalid shot, breaking ..."); break; } IRangedWeaponProjectile p = Ammo.InstantiateProjectile(fireMuzzle); p.Initialize(this); p.IgnoreTag = weaponManager.gameObject.tag; Ammo.ApplyEffect(p); GameObject.Destroy((p as Behaviour).gameObject, projectileLifetime); if (!isAngleValid && !(dmg as Behaviour).gameObject.transform.IsChildOf(weaponManager.transform)) { p.Hit(dmg); continue; } p.Direction = (worldhp - (p as Behaviour).transform.position).normalized; p.Direction = Quaternion.Euler(UnityEngine.Random.Range(-halfDeviation, halfDeviation), UnityEngine.Random.Range(-halfDeviation, halfDeviation), UnityEngine.Random.Range(-halfDeviation, halfDeviation)) * p.Direction; } yield return(new WaitForSeconds(animationTime)); if (OnEndPrimary != null) { OnEndPrimary(this, new EventArgs()); } state = RangedWeaponState.Idle; }