public void Activate(StateParameters parameters) { if (_currentStateParameters != null) { return; } _currentStateParameters = parameters; OnActivated(parameters); }
public GameStateItem?Deactivate(Type[] allSwitchers) { if (_currentStateParameters == null) { return(null); } OnDeactivated(); GameStateItem item = GetGameStateItem(allSwitchers); _currentStateParameters.Clean(); _currentStateParameters = null; return(item); }
public GameStateItem(Type gameStateType, StateParameters parameters, Type[] switcherTypes) { GameStateType = gameStateType; Parameters = parameters; SwitcherTypes = switcherTypes; }
private void StateSwitchOperation(BaseGameState <T> gameStateInstance, GameStateConstructor <T> constructor) { if (_currentGameState != null) { foreach (var p in _currentGameStateSwitchers) { p.Value.Deactivate(_currentGameState); } GameStateItem?i = _currentGameState.Deactivate(_currentSwitcherTypes); if (i.HasValue) { _gameStateHistory.Push(i.Value); } _currentSwitcherTypes = null; _currentGameStateSwitchers.Clear(); } _currentGameState = null; if (gameStateInstance != null) { Type t = gameStateInstance.GetType(); if (!_cachedGameStates.ContainsKey(t)) { _cachedGameStates.Add(t, gameStateInstance); gameStateInstance.Initialize(_gameWorld, this); } else { gameStateInstance = _cachedGameStates[t]; } _currentGameState = gameStateInstance; StateParameters parameters = null; if (constructor != null) { _currentSwitcherTypes = constructor.SwitcherTypes; parameters = constructor.Parameters; if (_currentSwitcherTypes != null && _currentSwitcherTypes.Length > 0) { for (int i = 0; i < _currentSwitcherTypes.Length; i++) { Type switcherType = _currentSwitcherTypes[i]; if (!_currentGameStateSwitchers.ContainsKey(switcherType)) { BaseStateSwitcher <T> switcher = Activator.CreateInstance(switcherType) as BaseStateSwitcher <T>; _currentGameStateSwitchers.Add(_currentSwitcherTypes[i], switcher); switcher.Activate(this, _currentGameState); } } } } if (parameters == null) { parameters = new StateParameters(new IStateParameter[] { }); } _currentGameState.Activate(parameters); } }
private GameStateConstructor <T> Initialize(StateParameters parameters) { Parameters = parameters; _switcherTypes = new List <Type>(); return(this); }
public GameStateConstructor <T> SetStateParameters(params IStateParameter[] parameters) { Parameters = new StateParameters(parameters); return(this); }
public static GameStateConstructor <T> Create(StateParameters parameters) { return(new GameStateConstructor <T>().Initialize(parameters)); }
protected abstract void OnActivated(StateParameters parameters);