Ludum Dare 50 Theme: Delay the inevitable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.2 KiB

using System;
using Enemies.States;
using UnityEngine;
namespace Enemies
{
public class BaseEnemy : Enemy
{
private void Awake()
{
this.health = 100;
this.speed = 4;
this.damage = 10;
this.attackRange = .8f;
SwitchState(States.Idle);
}
private void Update()
{
HandleStates();
state.Execute();
}
private void HandleStates()
{
if (health <= 0)
{
SwitchState(States.Die);
}
if (target == null)
{
SwitchState(States.Target);
}
else
{
float dist = Vector2.Distance(target.transform.position, transform.position);
if (currentState == States.Target)
{
SwitchState(States.Path);
}
if (currentState == States.Path)
{
if (dist <= attackRange)
{
SwitchState(target.CompareTag("Player") ? States.Attack : States.Follow);
}
else if (dist > viewRange)
{
target = null;
SwitchState(States.Target);
}
}
if (currentState == States.Attack)
{
if(dist > attackRange)
{
SwitchState(States.Path);
}
}
}
}
public override void SwitchState(States s)
{
if (currentState == s) return;
if (currentState != null) state.Exit();
state = s switch
{
States.Idle => new IdleState(this),
States.Attack => new AttackState(this),
States.Target => new TargetState(this),
States.Follow => new FollowState(this),
States.Path => new PathState(this),
States.Die => new DieState(this),
_ => new IdleState(this)
};
state.Enter();
}
}
}