Merge remote-tracking branch 'origin/Syntriax' into Over

This commit is contained in:
OverflowNarhoym 2022-02-22 18:39:43 +01:00
commit 05b9d76576
1 changed files with 36 additions and 11 deletions

View File

@ -1,3 +1,5 @@
using System.Collections;
using Movement;
using Pausable; using Pausable;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
@ -6,6 +8,7 @@ namespace AI
{ {
public class ShootingEnemyAI : MonoBehaviour, IPausable public class ShootingEnemyAI : MonoBehaviour, IPausable
{ {
[SerializeField] protected float focusingTime = .5f;
[SerializeField] protected float attacksPerSecond = 1f; [SerializeField] protected float attacksPerSecond = 1f;
[SerializeField] protected float attackRange = 5f; [SerializeField] protected float attackRange = 5f;
[SerializeField] protected float timeForProjectileToHit = .25f; [SerializeField] protected float timeForProjectileToHit = .25f;
@ -13,10 +16,12 @@ namespace AI
protected float cooldownPerShoot = 0f; protected float cooldownPerShoot = 0f;
protected float remainingCooldown = 0f; protected float remainingCooldown = 0f;
private float attackRangeSquared = 5f; protected float attackRangeSquared = 5f;
private Transform target = null; 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; public UnityEvent OnShoot { get; protected set; } = null;
@ -26,7 +31,7 @@ namespace AI
public virtual void Resume() => IsPaused = false; public virtual void Resume() => IsPaused = false;
#endregion #endregion
protected void Awake() protected virtual void Awake()
{ {
cooldownPerShoot = 1f / attacksPerSecond; cooldownPerShoot = 1f / attacksPerSecond;
attackRangeSquared = attackRange * attackRange; attackRangeSquared = attackRange * attackRange;
@ -34,17 +39,37 @@ namespace AI
UpdateTarget(FindObjectOfType<Player.PlayerController>()?.transform); UpdateTarget(FindObjectOfType<Player.PlayerController>()?.transform);
} }
protected void Update() protected virtual void Start()
=> movement = transform.GetComponentInParent<IMovement>();
protected virtual void Update()
{ {
remainingCooldown -= Time.deltaTime; remainingCooldown -= Time.deltaTime;
if (remainingCooldown <= 0f && canShoot) if (canShoot && !isShooting)
Shoot(); 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 projectile = ProjectilePool.Instance.Get();
projectile.transform.position = transform.position; projectile.transform.position = transform.position;
@ -60,7 +85,7 @@ namespace AI
OnShoot?.Invoke(); OnShoot?.Invoke();
} }
private Vector3 GetVelocityForProjectile(float time) protected Vector3 GetVelocityForProjectile(float time)
{ {
Vector3 vector3 = target.position - transform.position; Vector3 vector3 = target.position - transform.position;
vector3.z = 0f; vector3.z = 0f;
@ -69,9 +94,9 @@ namespace AI
return vector3; 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.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, 0.125f); Gizmos.DrawWireSphere(transform.position, 0.125f);