StateControl.cs 918 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Utility;
  2. namespace Core.State
  3. {
  4. public class StateControl
  5. {
  6. public Map<string, IState> States
  7. {
  8. get { return _states; }
  9. }
  10. private Map<string, IState> _states = new Map<string, IState>();
  11. private IState _currIState;
  12. public void AddState(string name, IState state)
  13. {
  14. _states[name] = state;
  15. }
  16. public void Update(float t)
  17. {
  18. if (_currIState != null)
  19. {
  20. _currIState.Update(t);
  21. }
  22. }
  23. public void ChangeState(string name)
  24. {
  25. if (!_states.TryGetValue(name,out IState state))
  26. {
  27. return;
  28. }
  29. if (_currIState != null)
  30. {
  31. _currIState.Exit();
  32. }
  33. _currIState = state;
  34. _currIState.Enter();
  35. }
  36. }
  37. }