remove unneeded timeInState calc & redundant "string?" annotation

This commit is contained in:
Colin McMillen 2020-03-06 14:20:17 -05:00
parent 0f8d9c2814
commit 392495a61e
2 changed files with 6 additions and 8 deletions

View File

@ -4,11 +4,12 @@ using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
public interface IState<T> { public interface IState<T> {
public void Enter(); public void Enter();
public string? Update(T obj, float modelTime, World world);
// 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 class FSM<T> { public class FSM<T> {
float timeInState = 0f;
Dictionary<string, IState<T>> states; Dictionary<string, IState<T>> states;
IState<T> state; IState<T> state;
@ -21,16 +22,13 @@ namespace SemiColinGames {
public string StateName { get; private set; } public string StateName { get; private set; }
public void Update(T obj, float modelTime, World world) { public void Update(T obj, float modelTime, World world) {
timeInState += modelTime; string newState = state.Update(obj, modelTime, world);
string? newState = state.Update(obj, modelTime, world);
if (newState != null) { if (newState != null) {
Transition(newState); Transition(newState);
} }
} }
void Transition(string state) { void Transition(string state) {
Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState);
timeInState = 0f;
StateName = state; StateName = state;
IState<T> newState = states[state]; IState<T> newState = states[state];
this.state = newState; this.state = newState;

View File

@ -10,7 +10,7 @@ namespace SemiColinGames {
timeInState = 0; timeInState = 0;
} }
public string? Update(NPC npc, float modelTime, World world) { public string Update(NPC npc, float modelTime, World world) {
timeInState += modelTime; timeInState += modelTime;
if (timeInState > 1.0f) { if (timeInState > 1.0f) {
npc.Facing *= -1; npc.Facing *= -1;
@ -23,7 +23,7 @@ namespace SemiColinGames {
class RunState : IState<NPC> { class RunState : IState<NPC> {
public void Enter() {} public void Enter() {}
public string? Update(NPC npc, float modelTime, World world) { public string Update(NPC npc, float modelTime, World world) {
int moveSpeed = 120; int moveSpeed = 120;
int desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime); int desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime);
// TODO: define the box modularly & correctly. // TODO: define the box modularly & correctly.