From 1a94602ada4d13321813fc9a2b7b31b48f755901 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 22 Feb 2022 18:03:09 +0300 Subject: [PATCH] Shooting Enemies now stops for a bit and then starts shooting --- Assets/Scripts/AI/ShootingEnemyAI.cs | 47 +++++++++++++++++++++------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/AI/ShootingEnemyAI.cs b/Assets/Scripts/AI/ShootingEnemyAI.cs index 6c22c96..b1f146e 100644 --- a/Assets/Scripts/AI/ShootingEnemyAI.cs +++ b/Assets/Scripts/AI/ShootingEnemyAI.cs @@ -1,3 +1,5 @@ +using System.Collections; +using Movement; using Pausable; using UnityEngine; using UnityEngine.Events; @@ -6,6 +8,7 @@ namespace AI { public class ShootingEnemyAI : MonoBehaviour, IPausable { + [SerializeField] protected float focusingTime = .5f; [SerializeField] protected float attacksPerSecond = 1f; [SerializeField] protected float attackRange = 5f; [SerializeField] protected float timeForProjectileToHit = .25f; @@ -13,10 +16,12 @@ namespace AI protected float cooldownPerShoot = 0f; protected float remainingCooldown = 0f; - private float attackRangeSquared = 5f; - private Transform target = null; + protected float attackRangeSquared = 5f; + protected Transform target = null; + protected bool isShooting = false; + protected IMovement movement = null; - private bool canShoot => (target.transform.position - transform.position).sqrMagnitude < attackRangeSquared && target != null; + protected bool canShoot => (target.transform.position - transform.position).sqrMagnitude < attackRangeSquared && target != null; public UnityEvent OnShoot { get; protected set; } = null; @@ -26,7 +31,7 @@ namespace AI public virtual void Resume() => IsPaused = false; #endregion - protected void Awake() + protected virtual void Awake() { cooldownPerShoot = 1f / attacksPerSecond; attackRangeSquared = attackRange * attackRange; @@ -34,17 +39,37 @@ namespace AI UpdateTarget(FindObjectOfType()?.transform); } - protected void Update() + protected virtual void Start() + => movement = transform.GetComponentInParent(); + + protected virtual void Update() { remainingCooldown -= Time.deltaTime; - if (remainingCooldown <= 0f && canShoot) - Shoot(); + if (canShoot && !isShooting) + StartCoroutine(ShootCoroutine()); } - protected void Shoot() + protected IEnumerator ShootCoroutine() { + isShooting = true; + float movementBaseSpeed = movement.BaseSpeed; + movement.BaseSpeed = 0f; + yield return new WaitForSeconds(focusingTime); + + while (canShoot) + { + if (remainingCooldown <= 0f) + Shoot(); + yield return null; + } + movement.BaseSpeed = movementBaseSpeed; + isShooting = false; + } + + protected virtual void Shoot() + { Projectile projectile = ProjectilePool.Instance.Get(); projectile.transform.position = transform.position; @@ -60,7 +85,7 @@ namespace AI OnShoot?.Invoke(); } - private Vector3 GetVelocityForProjectile(float time) + protected Vector3 GetVelocityForProjectile(float time) { Vector3 vector3 = target.position - transform.position; vector3.z = 0f; @@ -69,9 +94,9 @@ namespace AI return vector3; } - public void UpdateTarget(Transform transform) => target = transform; + public virtual void UpdateTarget(Transform transform) => target = transform; - private void OnDrawGizmosSelected() + protected virtual void OnDrawGizmosSelected() { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, 0.125f);