public Game(T gameWorld, GameStateConstructor <T> stateConstructor = null) { _gameWorld = gameWorld; _cachedGameStates = new Dictionary <Type, BaseGameState <T> >(); _gameStateHistory = new Stack <GameStateItem>(); _globalStateSwitchers = new Dictionary <Type, BaseGlobalStateSwitcher <T> >(); _currentGameStateSwitchers = new Dictionary <Type, BaseStateSwitcher <T> >(); _defaultStateConstructor = stateConstructor; SwitchGameState <U>(stateConstructor); }
private void InternalSwitchGameState(Type gameStateType, GameStateConstructor <T> constructor) { BaseGameState <T> gameStateInstance; if (!_cachedGameStates.TryGetValue(gameStateType, out gameStateInstance)) { gameStateInstance = Activator.CreateInstance(gameStateType) as BaseGameState <T>; } if (gameStateInstance == null) { Debug.LogErrorFormat("Can't switch to game state of type {0} because it is was not created for GameWorld type {1} or the constructor has parameters", gameStateType, _gameWorld.GetType()); } else { StateSwitchOperation(gameStateInstance, constructor); } }
public void SwitchGameState(Type gameStateType, GameStateConstructor <T> constructor = null) { InternalSwitchGameState(gameStateType, constructor); }
public void SwitchGameState(GameStateItem gameStateItem) { InternalSwitchGameState(gameStateItem.GameStateType, GameStateConstructor <T> .Create(gameStateItem)); }
public void SwitchGameState <GameState>(GameStateConstructor <T> constructor = null) where GameState : BaseGameState <T>, new() { SwitchGameState(typeof(GameState), constructor); }
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); } }