138 lines
3.8 KiB
C#
138 lines
3.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using Code.Gameplay.Player;
|
|
using Code.Gameplay.Character.Controller;
|
|
using Code.Gameplay.Character.Core;
|
|
using Reflex.Attributes;
|
|
using Reflex.Core;
|
|
using Reflex.Injectors;
|
|
|
|
namespace Code.Gameplay.Enemies
|
|
{
|
|
public abstract class BaseEnemy : MonoBehaviour
|
|
{
|
|
[Header("Base enemy configs")]
|
|
public int collisionDamage = 1;
|
|
public int attackDamage = 3;
|
|
|
|
public float detectionRange = 10f;
|
|
public float attackRange = 5f;
|
|
|
|
public float speed = 3f;
|
|
|
|
public float stunDuration = 3f;
|
|
public float attackCooldown = 1.5f;
|
|
|
|
[SerializeField] protected NavMeshAgent agent;
|
|
[SerializeField] protected Collider enemyCollider;
|
|
|
|
[Inject] private IPlayerProvider _playerProvider;
|
|
|
|
protected CharacterProvider Player => _playerProvider?.Character;
|
|
protected PlayerHealth PlayerHealth => _playerProvider?.Health;
|
|
protected CustomCharacterController PlayerController => Player?.CustomCharacterController;
|
|
|
|
protected bool IsStunned;
|
|
protected float CurrentStunTimer;
|
|
|
|
protected bool CanAttack = true;
|
|
protected float AttackCooldownTimer;
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (_playerProvider == null)
|
|
{
|
|
AttributeInjector.Inject(this, Container.ProjectContainer);
|
|
}
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
if (!Player) return;
|
|
|
|
HandleStun();
|
|
HandleAttackCooldown();
|
|
if (IsStunned) return;
|
|
|
|
HandleMovement();
|
|
HandleAttack();
|
|
}
|
|
|
|
protected virtual void HandleMovement()
|
|
{
|
|
if (!Player || !agent) return;
|
|
|
|
var distanceToPlayer = Vector3.Distance(transform.position, Player.transform.position);
|
|
|
|
if (distanceToPlayer <= detectionRange)
|
|
{
|
|
agent.isStopped = false;
|
|
agent.SetDestination(Player.transform.position);
|
|
}
|
|
else
|
|
{
|
|
agent.isStopped = true;
|
|
}
|
|
}
|
|
|
|
protected virtual void HandleAttack() { }
|
|
|
|
protected virtual void HandleStun()
|
|
{
|
|
if (!IsStunned) return;
|
|
|
|
CurrentStunTimer -= Time.deltaTime;
|
|
|
|
if (!(CurrentStunTimer <= 0)) return;
|
|
|
|
IsStunned = false;
|
|
agent.isStopped = false;
|
|
enemyCollider.enabled = true;
|
|
}
|
|
|
|
protected virtual void ApplyStun()
|
|
{
|
|
IsStunned = true;
|
|
CurrentStunTimer = stunDuration;
|
|
agent.isStopped = true;
|
|
enemyCollider.enabled = false;
|
|
}
|
|
|
|
protected virtual void HandleAttackCooldown()
|
|
{
|
|
if (!CanAttack)
|
|
{
|
|
AttackCooldownTimer -= Time.deltaTime;
|
|
if (AttackCooldownTimer <= 0)
|
|
{
|
|
CanAttack = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void DealDamage()
|
|
{
|
|
if (!CanAttack || PlayerHealth == null) return;
|
|
|
|
PlayerHealth.GetDamage(collisionDamage);
|
|
ApplyEffectsToPlayer();
|
|
|
|
CanAttack = false;
|
|
AttackCooldownTimer = attackCooldown;
|
|
}
|
|
|
|
protected virtual void ApplyEffectsToPlayer()
|
|
{
|
|
PlayerController.BlockAbilities(3f);
|
|
}
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(transform.position, detectionRange);
|
|
|
|
Gizmos.color = new Color(0.3f, 0.3f, 0.8f, 0.4f);
|
|
Gizmos.DrawSphere(transform.position, attackRange);
|
|
}
|
|
}
|
|
} |