// The first definition should be the root public GameStateDefinitionGraph(GameStateDefinition[] gameStateDefinitions) { this.gameStateDefinitions = gameStateDefinitions; nodeArray = new GameStateNode[gameStateDefinitions.Length]; for (int i = 0; i < gameStateDefinitions.Length; i++) { GameStateDefinition definition = gameStateDefinitions[i]; if (definition == null) { continue; } GameStateNode newNode = new GameStateNode(definition, i); if (root == null) { root = newNode; // only first node will apply this } nodeArray[i] = newNode; nodeDictionary.Add(definition, newNode); } foreach (var node in nodeArray) { node.PopulateNeighbors(FindGameStateNode); } }
void Internal_TransitionToState(GameStateDefinition gameStateSettings, GameStateParam[] parameters) { if (gameStateSettings == null) { Log.Error("[GameStateManager] Trying to transition to a null GameState"); return; } _parameters = parameters; GameState newGameState = _gameStateFactory.CreateGameState(gameStateSettings); newGameState.SetDefinition(gameStateSettings); if (newGameState == null) { Log.Error("[GameStateManager] Failed to create a GameState from gameStateSettings: " + gameStateSettings.GetType() + ". You may have forgot to register it in the factory."); return; } _targetGameState = newGameState; if (log) #pragma warning disable CS0162 // Unreachable code detected { Log.Info("[GameStateManager] Transitioning from " + GetPrintGameStateName(_currentGameState) + " to " + GetPrintGameStateName(_targetGameState) + "..."); } #pragma warning restore CS0162 // Unreachable code detected _currentGameState?.BeginExit(_parameters); }
public GameStateNode FindGameStateNode(GameStateDefinition definition) { if (definition == null) { return(null); } return(nodeDictionary[definition]); }
public override void SetDefinition(GameStateDefinition definition) { base.SetDefinition(definition); _specificDefinition = (GameStateDefinitionLobbyBase)definition; }
public override void SetDefinition(GameStateDefinition definition) { base.SetDefinition(definition); _specificDefinition = (GameStateDefinitionInGameOnline)definition; }
public GameStateNode(GameStateDefinition gameStateDefinition, int index) { this.gameStateDefinition = gameStateDefinition; this.index = index; }
public List <GameStateDefinition> FindPathToGameState(GameStateDefinition current, GameStateDefinition destination) { if (current == null || destination == null) { throw new NullReferenceException("Trying to find path to game state, but the method was called with null arguments"); } // ___________________________________________ Variable setup ___________________________________________ // GameStateNode startNode = nodeDictionary[destination]; int[] distances = new int[nodeArray.Length]; GameStateNode[] previous = new GameStateNode[nodeArray.Length]; List <GameStateNode> queue = new List <GameStateNode>(nodeArray); for (int i = 0; i < distances.Length; i++) { distances[i] = int.MaxValue - 1; } distances[startNode.index] = 0; // ___________________________________________ Helper methods ___________________________________________ // int DistanceTo(GameStateNode node) { return(distances[node.index]); } void SetDistanceTo(GameStateNode node, int dist) { distances[node.index] = dist; } void SetPrevTo(GameStateNode node, GameStateNode prev) { previous[node.index] = prev; } GameStateNode PopNodeWithLowestDist() { int record = int.MaxValue; GameStateNode recordHolder = null; int recordI = -1; for (int i = 0; i < queue.Count; i++) { if (DistanceTo(queue[i]) < record) { record = DistanceTo(queue[i]); recordHolder = queue[i]; recordI = i; } } queue.RemoveAt(recordI); return(recordHolder); } // ___________________________________________ Algo ___________________________________________ // while (queue.Count > 0) { GameStateNode u = PopNodeWithLowestDist(); int fullPathLengthToNeighborsOfU = DistanceTo(u) + 1; for (int i = 0; i < u.neighbors.Count; i++) { if (fullPathLengthToNeighborsOfU < DistanceTo(u.neighbors[i])) { SetDistanceTo(u.neighbors[i], fullPathLengthToNeighborsOfU); SetPrevTo(u.neighbors[i], u); } } } // ___________________________________________ Build path ___________________________________________ // GameStateNode toAdd = nodeDictionary[current]; List <GameStateDefinition> path = new List <GameStateDefinition>(); while (toAdd != null) { path.Add(toAdd.gameStateDefinition); toAdd = previous[toAdd.index]; } return(path); }
public override void SetDefinition(GameStateDefinition definition) { base.SetDefinition(definition); _specificDefinition = (GameStateDefinitionRootMenu)definition; }
public GameState CreateGameState(GameStateDefinition settings) { return(_factory.CreateValue(settings.GetType())); }
static public void TransitionToState(GameStateDefinition gameStateSettings, params GameStateParam[] parameters) { Instance.Internal_TransitionToState(gameStateSettings, parameters); }
public virtual void SetDefinition(GameStateDefinition definition) { this.Definition = definition; }