Shooting Enemies now stops for a bit and then starts shooting

This commit is contained in:
Syntriax 2022-02-22 18:03:09 +03:00
parent 3b89be5041
commit 1a94602ada
1 changed files with 36 additions and 11 deletions

View File

@ -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<Player.PlayerController>()?.transform);
}
protected void Update()
protected virtual void Start()
=> movement = transform.GetComponentInParent<IMovement>();
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);