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.

38 lines
960 B

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