2022-02-22 18:03:09 +03:00
|
|
|
using System.Collections;
|
|
|
|
using Movement;
|
2022-02-22 11:25:25 +03:00
|
|
|
using Pausable;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
namespace AI
|
|
|
|
{
|
|
|
|
public class ShootingEnemyAI : MonoBehaviour, IPausable
|
|
|
|
{
|
2022-02-22 18:03:09 +03:00
|
|
|
[SerializeField] protected float focusingTime = .5f;
|
2022-02-22 11:25:25 +03:00
|
|
|
[SerializeField] protected float attacksPerSecond = 1f;
|
|
|
|
[SerializeField] protected float attackRange = 5f;
|
|
|
|
[SerializeField] protected float timeForProjectileToHit = .25f;
|
|
|
|
|
|
|
|
protected float cooldownPerShoot = 0f;
|
|
|
|
protected float remainingCooldown = 0f;
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected float attackRangeSquared = 5f;
|
|
|
|
protected Transform target = null;
|
|
|
|
protected bool isShooting = false;
|
|
|
|
protected IMovement movement = null;
|
2022-02-22 11:25:25 +03:00
|
|
|
|
2022-02-25 13:49:11 +03:00
|
|
|
protected bool canShoot => target != null && (target.transform.position - transform.position).sqrMagnitude < attackRangeSquared;
|
2022-02-22 11:25:25 +03:00
|
|
|
|
|
|
|
public UnityEvent OnShoot { get; protected set; } = null;
|
|
|
|
|
|
|
|
#region IPausable
|
|
|
|
public bool IsPaused { get; protected set; } = false;
|
|
|
|
public virtual void Pause() => IsPaused = true;
|
|
|
|
public virtual void Resume() => IsPaused = false;
|
|
|
|
#endregion
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected virtual void Awake()
|
2022-02-22 11:25:25 +03:00
|
|
|
{
|
|
|
|
cooldownPerShoot = 1f / attacksPerSecond;
|
|
|
|
attackRangeSquared = attackRange * attackRange;
|
|
|
|
OnShoot = new UnityEvent();
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected virtual void Start()
|
2022-02-25 13:49:11 +03:00
|
|
|
{
|
|
|
|
movement = transform.GetComponentInParent<IMovement>();
|
|
|
|
UpdateTarget(FindObjectOfType<Player.PlayerController>()?.transform);
|
|
|
|
}
|
2022-02-22 18:03:09 +03:00
|
|
|
|
|
|
|
protected virtual void Update()
|
2022-02-22 11:25:25 +03:00
|
|
|
{
|
|
|
|
remainingCooldown -= Time.deltaTime;
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
if (canShoot && !isShooting)
|
|
|
|
StartCoroutine(ShootCoroutine());
|
2022-02-22 11:25:25 +03:00
|
|
|
}
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected IEnumerator ShootCoroutine()
|
2022-02-22 11:25:25 +03:00
|
|
|
{
|
2022-02-22 18:03:09 +03:00
|
|
|
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;
|
|
|
|
}
|
2022-02-22 11:25:25 +03:00
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected virtual void Shoot()
|
|
|
|
{
|
2022-02-22 11:25:25 +03:00
|
|
|
Projectile projectile = ProjectilePool.Instance.Get();
|
|
|
|
projectile.transform.position = transform.position;
|
|
|
|
|
|
|
|
Vector3 velocity = GetVelocityForProjectile(timeForProjectileToHit);
|
|
|
|
|
|
|
|
RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, target.position - transform.position, attackRange);
|
|
|
|
if (raycastHit2D.transform != target)
|
|
|
|
velocity = GetVelocityForProjectile(timeForProjectileToHit * 2);
|
|
|
|
|
|
|
|
projectile.SetVelocity(velocity);
|
|
|
|
|
|
|
|
remainingCooldown = cooldownPerShoot;
|
|
|
|
OnShoot?.Invoke();
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected Vector3 GetVelocityForProjectile(float time)
|
2022-02-22 11:25:25 +03:00
|
|
|
{
|
|
|
|
Vector3 vector3 = target.position - transform.position;
|
|
|
|
vector3.z = 0f;
|
|
|
|
vector3.y -= (Physics2D.gravity.y / 2) * (time * time);
|
|
|
|
vector3 /= time;
|
|
|
|
return vector3;
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
public virtual void UpdateTarget(Transform transform) => target = transform;
|
2022-02-22 11:25:25 +03:00
|
|
|
|
2022-02-22 18:03:09 +03:00
|
|
|
protected virtual void OnDrawGizmosSelected()
|
2022-02-22 11:25:25 +03:00
|
|
|
{
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
Gizmos.DrawWireSphere(transform.position, 0.125f);
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
Gizmos.DrawWireSphere(transform.position, attackRange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|