public void ChangeState(GameStates newState)
    {
        bool isCurrentState = (GameState == newState);

        if (!isCurrentState)
        {
            // send new state to server. Only change state in Unity app once server responds
            SocketIO.ChangeGameState(GameStateToSocketMessageString(newState), response =>
            {
                // check for error in the response
                var responseJSON = JSONObject.Parse(response);
                if (responseJSON["error"] != null)
                {
                    Debug.LogError("Server error when changing game state: " + responseJSON["error"]);
                    return;
                }

                // get any carry-over data from the current state controller and delete the controller
                JSONObject betweenStateControllerData = null;
                if (currentStateController != null)
                {
                    betweenStateControllerData = currentStateController.ReturnDataForNextGamePhase();
                    Destroy(currentStateController.gameObject);
                }

                // instantiate a new state controller and pass it the carry-over data
                GamePhaseController newStateController = InstantiateControllerForState(newState);
                currentStateController = newStateController;

                currentStateController.UseCarryOverData(betweenStateControllerData);

                GameState = newState;
            });
        }
    }
Exemple #2
0
    private void ChangeState(GameStates newState)
    {
        bool isCurrentState = (GameState == newState);

        if (!isCurrentState)
        {
            // send new state to server. Only change state in Unity app once server responds
            SocketIO.ChangeGameState(newState.ToString(), response =>
            {
                // check for error in the response
                var responseJSON = JSONObject.Parse(response);
                if (responseJSON["error"] != null)
                {
                    Debug.LogError("Server error when changing game state: " + responseJSON["error"]);
                    return;
                }

                // change game state and call listeners
                GameState = newState;
                if (StateActions.ContainsKey(newState))
                {
                    Debug.Log("action: " + StateActions[newState].Count);
                    foreach (Action action in StateActions[newState])
                    {
                        action.Invoke();
                    }
                }
            });
        }
    }
Exemple #3
0
    public void OnNewGameButtonClicked()
    {
        socketIO.StartNewGame(response =>
        {
            var jsonResponse       = JSONObject.Parse(response);
            GameIDTextElement.text = "Game ID: <b>" + jsonResponse["gameID"] + "</b>";

            socketIO.ChangeGameState("build");
        });
    }