Added AI code for enemies utilizing State Management Created simple enemy prefab with the AI
35 lines
815 B
C#
35 lines
815 B
C#
using Unity.IO.LowLevel.Unsafe;
|
|
using UnityEngine;
|
|
|
|
namespace AI.Base {
|
|
abstract public class StateManager : MonoBehaviour {
|
|
protected IState CurrentState;
|
|
|
|
virtual protected void Update() {
|
|
CurrentState?.Tick();
|
|
IState next = GetNextState();
|
|
if (next != null && next != CurrentState) {
|
|
CurrentState.Stop();
|
|
CurrentState = next;
|
|
CurrentState.Start();
|
|
}
|
|
}
|
|
|
|
abstract protected IState GetNextState();
|
|
}
|
|
|
|
public class StateNode : ScriptableObject, IState {
|
|
protected Transform Owner;
|
|
|
|
virtual public void Initialize(Transform ownerTransform) {
|
|
Owner = ownerTransform;
|
|
}
|
|
|
|
virtual public void Start() {}
|
|
virtual public void Stop() {}
|
|
virtual public void Tick() {}
|
|
|
|
virtual public IState GetNextState() => this;
|
|
}
|
|
}
|