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.

37 lines
946 B

  1. using System.Collections.Generic;
  2. namespace SemiColinGames {
  3. public interface IState<T> {
  4. public void Enter();
  5. // Returns the name of the new state, or null if we should stay in the same state.
  6. public string Update(T obj, float modelTime, World world);
  7. }
  8. public class FSM<T> {
  9. Dictionary<string, IState<T>> states;
  10. IState<T> state;
  11. public FSM(Dictionary<string, IState<T>> states, string initial) {
  12. this.states = states;
  13. StateName = initial;
  14. Transition(StateName);
  15. }
  16. public string StateName { get; private set; }
  17. public void Update(T obj, float modelTime, World world) {
  18. string newState = state.Update(obj, modelTime, world);
  19. if (newState != null) {
  20. Transition(newState);
  21. }
  22. }
  23. void Transition(string state) {
  24. StateName = state;
  25. IState<T> newState = states[state];
  26. this.state = newState;
  27. this.state.Enter();
  28. }
  29. }
  30. }