A stealth-based 2D platformer where you don't have to kill anyone unless you want to.
https://www.semicolin.games
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.
40 lines
1.1 KiB
40 lines
1.1 KiB
using System.Collections.Generic;
|
|
|
|
namespace SemiColinGames {
|
|
public interface IState<T> {
|
|
// Called automatically whenever this state is transitioned to. Should reset whichever
|
|
// state-specific variables need resetting.
|
|
public void Enter();
|
|
|
|
// Returns the name of the new state, or null if we should stay in the same state.
|
|
public string Update(float modelTime, SneakWorld world, T input);
|
|
}
|
|
|
|
public class FSM<T> {
|
|
readonly Dictionary<string, IState<T>> states;
|
|
|
|
public FSM(string initialStateName, Dictionary<string, IState<T>> states) {
|
|
this.states = states;
|
|
StateName = initialStateName;
|
|
Transition(StateName);
|
|
}
|
|
|
|
public string StateName { get; private set; }
|
|
|
|
public IState<T> State { get; private set; }
|
|
|
|
public void Update(float modelTime, SneakWorld world, T input) {
|
|
string newState = State.Update(modelTime, world, input);
|
|
if (newState != null) {
|
|
Transition(newState);
|
|
}
|
|
}
|
|
|
|
void Transition(string state) {
|
|
StateName = state;
|
|
IState<T> newState = states[state];
|
|
State = newState;
|
|
State.Enter();
|
|
}
|
|
}
|
|
}
|