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

  1. using System;
  2. using System.Collections.Generic;
  3. namespace SemiColinGames {
  4. public interface IState<T> {
  5. public void Enter();
  6. public string? Update(T obj, float modelTime, World world);
  7. }
  8. public class FSM<T> {
  9. float timeInState = 0f;
  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. timeInState += modelTime;
  20. string? newState = state.Update(obj, modelTime, world);
  21. if (newState != null) {
  22. Transition(newState);
  23. }
  24. }
  25. void Transition(string state) {
  26. Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState);
  27. timeInState = 0f;
  28. StateName = state;
  29. IState<T> newState = states[state];
  30. this.state = newState;
  31. this.state.Enter();
  32. }
  33. }
  34. }