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.

39 lines
1.0 KiB

  1. using System.Collections.Generic;
  2. namespace SemiColinGames {
  3. public interface IState {
  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, World world);
  9. }
  10. public class FSM {
  11. readonly Dictionary<string, IState> states;
  12. IState state;
  13. public FSM(string initialStateName, Dictionary<string, IState> states) {
  14. this.states = states;
  15. StateName = initialStateName;
  16. Transition(StateName);
  17. }
  18. public string StateName { get; private set; }
  19. public void Update(float modelTime, World world) {
  20. string newState = state.Update(modelTime, world);
  21. if (newState != null) {
  22. Transition(newState);
  23. }
  24. }
  25. void Transition(string state) {
  26. StateName = state;
  27. IState newState = states[state];
  28. this.state = newState;
  29. this.state.Enter();
  30. }
  31. }
  32. }