예제 #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);
        }
예제 #2
0
        public async void OurMoveWasFinished(ChessMove ourMove)
        {
            clock.StartMe();

            foreach (Vector2Int pos in Board.IteratePositions())
            {
                PieceId pieceId = board.GetPieceIdAt(pos);

                if (pieceId == null)
                {
                    continue;
                }

                if (pieceId.color != color)
                {
                    continue;
                }

                await Task.Delay(Random.Range(500, 5_000));

                float duration = clock.StopMe();

                if (clock.IsTimeOverForMe())
                {
                    OnOutOfTime?.Invoke();
                    return;
                }

                OnMoveFinish?.Invoke(new ChessMove {
                    origin   = pos,
                    target   = FindFreeSpot(),
                    duration = duration
                });
                return;
            }

            throw new Exception();
        }