103 lines
2.4 KiB
C#
103 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
public class Enemy : Character
|
|
{
|
|
[Header("Enemy Settings")]
|
|
public string enemyName = "Enemy";
|
|
public int contactDamage = 1;
|
|
public float aggroRange = 6f;
|
|
public float attackRange = 1.5f;
|
|
public float attackCooldown = 1f;
|
|
public bool autoAcquirePlayer = true;
|
|
|
|
|
|
|
|
private float nextAttackTime = 0f;
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
UpdateTargetAndCombat();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when player interacts with this enemy
|
|
/// </summary>
|
|
public override void OnInteract(GameObject player)
|
|
{
|
|
// Call base implementation (handles look-at and dialogue)
|
|
base.OnInteract(player);
|
|
}
|
|
|
|
|
|
|
|
private void UpdateTargetAndCombat()
|
|
{
|
|
if (autoAcquirePlayer && target == null)
|
|
{
|
|
TryAcquirePlayer();
|
|
}
|
|
|
|
if (target == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float distance = Vector3.Distance(transform.position, target.transform.position);
|
|
if (distance > aggroRange)
|
|
{
|
|
target = null;
|
|
if (MovementController != null)
|
|
{
|
|
MovementController.Stop();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (MovementController != null)
|
|
{
|
|
if (distance > attackRange)
|
|
{
|
|
MovementController.MoveTo(target.transform.position);
|
|
}
|
|
else
|
|
{
|
|
MovementController.Stop();
|
|
MovementController.SetLookTarget(target.transform.position);
|
|
}
|
|
}
|
|
|
|
if (distance <= attackRange && Time.time >= nextAttackTime)
|
|
{
|
|
nextAttackTime = Time.time + attackCooldown;
|
|
TriggerAttack();
|
|
target.TakeDamage(contactDamage);
|
|
}
|
|
}
|
|
|
|
private void TryAcquirePlayer()
|
|
{
|
|
if (Player.current == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float distance = Vector3.Distance(transform.position, Player.current.transform.position);
|
|
if (distance <= aggroRange)
|
|
{
|
|
SetTarget(Player.current);
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
// Draw aggro range
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(transform.position, aggroRange);
|
|
|
|
// Draw attack range
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireSphere(transform.position, attackRange);
|
|
}
|
|
}
|