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