1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using Utility;
- namespace Core.State
- {
- public class StateControl : IDisposable
- {
- public Map<string, IState> States
- {
- get { return _states; }
- }
- private Map<string, IState> _states = new Map<string, IState>();
- public IState CurrIState
- {
- get { return _currIState; }
- }
- private IState _currIState;
- public string CurrStateName;
- public void AddState(string name, IState state)
- {
- _states[name] = state;
- }
- public void Update(float t)
- {
- if (_currIState != null)
- {
- _currIState.Update(t);
- }
- }
- public void ChangeState(string name)
- {
- if (!_states.TryGetValue(name, out IState state))
- {
- return;
- }
- if (_currIState != null)
- {
- _currIState.Exit();
- }
- CurrStateName = name;
- _currIState = state;
- _currIState.Enter();
- }
- public void Dispose()
- {
- for (int i = 0; i < _states.Count; i++)
- {
- IState istate = _states.GetValueByIndex(i);
- if (istate != null)
- {
- istate.Dispose();
- }
- }
- }
- }
- }
|