sneak/Shared/FSM.cs

41 lines
1.0 KiB
C#

using System;
using System.Collections.Generic;
namespace SemiColinGames {
public interface IState {
public void Enter();
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets);
}
public class FSM {
float timeInState = 0f;
Dictionary<string, IState> states;
IState state;
public FSM(Dictionary<string, IState> states, string initial) {
this.states = states;
StateName = initial;
Transition(StateName);
}
public string StateName { get; private set; }
public void Update(NPC npc, float modelTime, AABB[] collisionTargets) {
timeInState += modelTime;
string? newState = state.Update(npc, modelTime, collisionTargets);
if (newState != null) {
Transition(newState);
}
}
void Transition(string state) {
Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState);
timeInState = 0f;
StateName = state;
IState newState = states[state];
this.state = newState;
this.state.Enter();
}
}
}