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 {
  5. public void Enter();
  6. public string? Update(NPC npc, float modelTime, AABB[] collisionTargets);
  7. }
  8. public class FSM {
  9. float timeInState = 0f;
  10. Dictionary<string, IState> states;
  11. IState state;
  12. public FSM(Dictionary<string, IState> 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(NPC npc, float modelTime, AABB[] collisionTargets) {
  19. timeInState += modelTime;
  20. string? newState = state.Update(npc, modelTime, collisionTargets);
  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 newState = states[state];
  30. this.state = newState;
  31. this.state.Enter();
  32. }
  33. }
  34. }