FSM: deal with IStates directly, don't parametrize on an input type.

This commit is contained in:
Colin McMillen 2020-03-25 15:16:31 -04:00
parent 3dea13a386
commit ec8c24e5b6
2 changed files with 34 additions and 21 deletions

View File

@ -1,27 +1,29 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
public interface IState<T> { public interface IState {
// Called automatically whenever this state is transitioned to. Should reset whichever
// state-specific variables need resetting.
public void Enter(); public void Enter();
// Returns the name of the new state, or null if we should stay in the same state. // Returns the name of the new state, or null if we should stay in the same state.
public string Update(T obj, float modelTime, World world); public string Update(float modelTime, World world);
} }
public class FSM<T> { public class FSM {
readonly Dictionary<string, IState<T>> states; readonly Dictionary<string, IState> states;
IState<T> state; IState state;
public FSM(Dictionary<string, IState<T>> states, string initial) { public FSM(string initialStateName, Dictionary<string, IState> states) {
this.states = states; this.states = states;
StateName = initial; StateName = initialStateName;
Transition(StateName); Transition(StateName);
} }
public string StateName { get; private set; } public string StateName { get; private set; }
public void Update(T obj, float modelTime, World world) { public void Update(float modelTime, World world) {
string newState = state.Update(obj, modelTime, world); string newState = state.Update(modelTime, world);
if (newState != null) { if (newState != null) {
Transition(newState); Transition(newState);
} }
@ -29,7 +31,7 @@ namespace SemiColinGames {
void Transition(string state) { void Transition(string state) {
StateName = state; StateName = state;
IState<T> newState = states[state]; IState newState = states[state];
this.state = newState; this.state = newState;
this.state.Enter(); this.state.Enter();
} }

View File

@ -3,14 +3,19 @@ using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic; using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
class IdleState : IState<NPC> { class IdleState : IState {
float timeInState = 0; private NPC npc;
private float timeInState = 0;
public IdleState(NPC npc) {
this.npc = npc;
}
public void Enter() { public void Enter() {
timeInState = 0; timeInState = 0;
} }
public string Update(NPC npc, float modelTime, World world) { public string Update(float modelTime, World world) {
timeInState += modelTime; timeInState += modelTime;
if (timeInState > 1.0f) { if (timeInState > 1.0f) {
npc.Facing *= -1; npc.Facing *= -1;
@ -20,10 +25,16 @@ namespace SemiColinGames {
} }
} }
class RunState : IState<NPC> { class RunState : IState {
private NPC npc;
public RunState(NPC npc) {
this.npc = npc;
}
public void Enter() {} public void Enter() {}
public string Update(NPC npc, float modelTime, World world) { public string Update(float modelTime, World world) {
float moveSpeed = 120; float moveSpeed = 120;
float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime; float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime;
float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing; float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing;
@ -52,7 +63,7 @@ namespace SemiColinGames {
private readonly Vector2 spriteCenter; private readonly Vector2 spriteCenter;
private readonly Vector2 eyeOffset = new Vector2(4, -9); private readonly Vector2 eyeOffset = new Vector2(4, -9);
private readonly FSM<NPC> fsm; private readonly FSM fsm;
private readonly Vector2 halfSize = new Vector2(11, 24); private readonly Vector2 halfSize = new Vector2(11, 24);
public NPC(Vector2 position, int facing) { public NPC(Vector2 position, int facing) {
@ -64,10 +75,10 @@ namespace SemiColinGames {
Box = new AABB(Position, halfSize); Box = new AABB(Position, halfSize);
Facing = facing; Facing = facing;
fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> { fsm = new FSM("run", new Dictionary<string, IState> {
{ "idle", new IdleState() }, { "idle", new IdleState(this) },
{ "run", new RunState() } { "run", new RunState(this) }
}, "run"); });
} }
public int Facing; public int Facing;
@ -99,7 +110,7 @@ namespace SemiColinGames {
} }
public void Update(float modelTime, World world) { public void Update(float modelTime, World world) {
fsm.Update(this, modelTime, world); fsm.Update(modelTime, world);
Box = new AABB(Position, halfSize); Box = new AABB(Position, halfSize);
Debug.AddRect(Box, Color.White); Debug.AddRect(Box, Color.White);
} }