Exemple #1
0
        ////////////////////
        // Core game loop //
        ////////////////////

        /// <summary>
        /// Let us perform an action
        /// </summary>
        private async void PerformOurMove()
        {
            // === our turn begins ===

            myTurn = true;

            clock.StartMe();

            ChessMove move;

            try
            {
                move = await board.LetPlayerHaveAMove(playerColor);
            }
            catch (TaskCanceledException)
            {
                // board has been destroyed in our turn,
                // we're probably leaving the scene
                return;
            }

            move.duration = clock.StopMe();

            // === opponent turn begins ===

            myTurn = false;

            clock.StartOpponent();

            opponent.OurMoveWasFinished(move);
        }
Exemple #2
0
        /// <summary>
        /// Start playground in a regular fashion when a match is known
        /// </summary>
        private async void StartRegular(MatchEntity match)
        {
            // which player are we?
            playerColor = match.WhitePlayer == Auth.Player
                ? PieceColor.White : PieceColor.Black;

            // who is the opponent
            UnisavePlayer opponentPlayer = playerColor.IsWhite()
                ? match.BlackPlayer : match.WhitePlayer;

            // connect to the opponent
            if (opponentPlayer == null)
            {
                // AI
                Debug.Log("Starting computer opponent...");
                opponent = new ComputerOpponent(
                    playerColor.Opposite(),
                    board,
                    match.Duration
                    );
            }
            else
            {
                // Real person
                Debug.Log("Connecting to photon opponent...");
                opponent = PhotonOpponent.CreateInstance(match.EntityId);
            }

            // create and setup the board
            board.CreateBoard(
                playerColor.IsWhite(),
                match.WhitePlayerSet,
                match.BlackPlayerSet
                );

            // create the clock
            clock = new Clock(match.Duration);

            // register opponent events
            opponent.OnMoveFinish += OpponentsMoveWasFinished;
            opponent.OnGiveUp     += OnOpponentGaveUp;
            opponent.OnOutOfTime  += OnOpponentTimeOver;

            // wait for the opponent
            Debug.Log("Waiting for the opponent...");
            await opponent.WaitForReady();

            // === start the game ===

            // if we are white, we are the one to start
            Debug.Log("Game has started.");
            if (playerColor.IsWhite())
            {
                PerformOurMove();
            }
            else
            {
                // it's the opponents turn so just wait
                clock.StartOpponent();
            }
        }