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

  1. using System.Collections.Generic;
  2. namespace SemiColinGames {
  3. public interface IState<T> {
  4. // Called automatically whenever this state is transitioned to. Should reset whichever
  5. // state-specific variables need resetting.
  6. public void Enter();
  7. // Returns the name of the new state, or null if we should stay in the same state.
  8. public string Update(float modelTime, SneakWorld world, T input);
  9. }
  10. public class FSM<T> {
  11. readonly Dictionary<string, IState<T>> states;
  12. public FSM(string initialStateName, Dictionary<string, IState<T>> states) {
  13. this.states = states;
  14. StateName = initialStateName;
  15. Transition(StateName);
  16. }
  17. public string StateName { get; private set; }
  18. public IState<T> State { get; private set; }
  19. public void Update(float modelTime, SneakWorld world, T input) {
  20. string newState = State.Update(modelTime, world, input);
  21. if (newState != null) {
  22. Transition(newState);
  23. }
  24. }
  25. void Transition(string state) {
  26. StateName = state;
  27. IState<T> newState = states[state];
  28. State = newState;
  29. State.Enter();
  30. }
  31. }
  32. }