Example #1
0
    private void AfterChange(GameState oldState)
    {
        if (IsFieldUpdated(oldState, CurrentState, "CurrentState"))
        {
            switch (CurrentState.CurrentState)
            {
            case State.ARRANGE_SHIPS:
            {
                Logger.Log("GameManager: ARRANGE_SHIPS");
                if (Connection.Instance.OpId == null)
                {
                    Logger.Log("GameManager: retrieve OpId from CurrentState");
                    Connection.Instance.OpId = isHost ? CurrentState.GuestPlayerId : CurrentState.HostPlayerId;
                }
                SceneManager.LoadSceneAsync(Constants.SCENE_INDEX_PREGAME);
                break;
            }

            case State.BEGIN_BATTLE:
            {
                Logger.Log("GameManager: BEGIN_BATTLE");
                SceneManager.LoadSceneAsync(Constants.SCENE_INDEX_INGAME);
                break;
            }

            case State.PICK_CELL:
            {
                Logger.Log("GameManager: PICK_CELL");
                InGameController controller = GameObject.FindGameObjectWithTag(Tags.SCENE_CONTROLLER).GetComponent <InGameController>();
                controller.StartTurn(IsMyTurn());
                break;
            }

            case State.FIRE:
            {
                Logger.Log("GameManager: FIRE");
                InGameController controller = GameObject.FindGameObjectWithTag(Tags.SCENE_CONTROLLER).GetComponent <InGameController>();
                TargetModel      target     = JsonUtility.FromJson <TargetModel>(CurrentState.Target);
                controller.DisplayAttack(target);
                break;
            }

            case State.CHECK_WIN:
            {
                Logger.Log("GameManager: CHECK_WIN");
                InGameController controller = GameObject.FindGameObjectWithTag(Tags.SCENE_CONTROLLER).GetComponent <InGameController>();
                int MyHp = isHost ? CurrentState.HostPlayerHealth : CurrentState.GuestPlayerHealth;
                int OpHp = isHost ? CurrentState.GuestPlayerHealth : CurrentState.HostPlayerHealth;
                controller.UpdateHPAndCheckWin(MyHp, OpHp);
                break;
            }

            case State.CHANGE_TURN:
            {
                Logger.Log("GameManager: CHANGE_TURN");
                // Short delay before starting new turn
                StartCoroutine(DelayBeginTurn(1));
                break;
            }

            case State.END_GAME:
            {
                Logger.Log("GameManager: END_GAME");
                bool             isWin      = CurrentState.PlayerWin == Connection.Instance.MyId;
                InGameController controller = GameObject.FindGameObjectWithTag(Tags.SCENE_CONTROLLER).GetComponent <InGameController>();
                controller.HandleWinner(isWin);
                break;
            }

            default: break;
            }
        }
        if (
            IsFieldUpdated(oldState, CurrentState, "HostPlayerShips") ||
            IsFieldUpdated(oldState, CurrentState, "GuestPlayerShips")
            )
        {
            if (isHost)
            {
                if (!CurrentState.HostPlayerShips.Equals("") && !CurrentState.GuestPlayerShips.Equals(""))
                {
                    Logger.Log("GameManager: All player are ready");
                    BeginBattle();
                }
            }
        }
    }