Exemple #1
0
        // Player constructor
        public ChessMove(Square initialSquare, Square targetSquare, float rating)
        {
            InitialSquare = initialSquare;
            TargetSquare  = targetSquare;
            if (ChessGrid.Pieces.ContainsKey(targetSquare))
            {
                Attack = (ChessGrid.GetPosition(targetSquare) != null);
            }
            Rating = rating;

            if (!Attack)
            {
                return;
            }
            ChessPiece piece = ChessGrid.GetPosition(initialSquare);

            if (piece.type == "knight")
            {
                ChessPiece        target       = ChessGrid.GetPosition(targetSquare);
                List <ChessPiece> surroundings = MovementUtil.SurroundingPieces(ChessGrid.Pieces, initialSquare);
                if (!surroundings.Contains(target))
                {
                    AddOne = true;
                }
            }
        }
Exemple #2
0
    public static string BuildNotation(string type, Square initial, Square target, bool capture)
    {
        string notation = GetPieceAbbr(type, initial);

        if (capture)
        {
            ChessPiece targetPiece = ChessGrid.GetPosition(target);
            notation += " x " + GetPieceAbbr(targetPiece.type, target);
        }
        else
        {
            notation += " -> " + GetPosNotation(target);
        }

        return(notation);
    }
Exemple #3
0
        private void Update()
        {
            Color color;

            // Determine raycasts - if the mouse is hovering over the object and released, then the piece that is being dragged will be moved here
            RaycastHit2D[] hits     = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            bool           hovering = false;

            if (hits.Any(ray => ray.collider.gameObject == gameObject))
            {
                if (Input.GetMouseButtonUp(0) && !Game.Controller.die.rolling)  // make sure die is not rolling
                {
                    StartCoroutine(MovementUtil.Instance.MovePiece(_chessMove, attackOnly));
                }
                hovering = true;

                if (_chessMove.Attack && !Game.Controller.die.rolling)
                {
                    ChessPiece initialPiece = ChessGrid.GetPosition(_chessMove.InitialSquare);
                    ChessPiece targetPiece  = ChessGrid.GetPosition(_chessMove.TargetSquare);
                    Game.Controller.uiManager.UpdateAttack(UIManager.BuildNotation(initialPiece.type, _chessMove.InitialSquare, _chessMove.TargetSquare, true) + " - "
                                                           + CaptureMatrix.GetMin(initialPiece.type, targetPiece.type, _chessMove.AddOne) + "+ needed");
                }
            }

            // Interpolate color based on mouse input
            if (hovering)
            {
                color = _highlighted;
            }
            else
            {
                color = _chessMove.Attack ? _attacking : _initial;
            }
            _spriteRenderer.color = Color.Lerp(_spriteRenderer.color, color, 20 * Time.deltaTime);
        }
Exemple #4
0
        // Move a piece on the board
        public IEnumerator MovePiece(ChessMove chessMove, bool attackOnly)
        {
            ChessPiece initialPiece = ChessGrid.GetPosition(chessMove.InitialSquare);
            string     notation     = UIManager.BuildNotation(initialPiece.type, chessMove.InitialSquare, chessMove.TargetSquare, chessMove.Attack);
            Color      color        = Color.white;

            // Assume it will succeed if not an attack
            bool success = true;

            if (chessMove.Attack)
            {
                // Update attacking text
                ChessPiece targetPiece = ChessGrid.GetPosition(chessMove.TargetSquare);
                int        minNeeded   = CaptureMatrix.GetMin(initialPiece.type, targetPiece.type, chessMove.AddOne);
                Game.Controller.uiManager.UpdateAttack(UIManager.BuildNotation(initialPiece.type,
                                                                               chessMove.InitialSquare, chessMove.TargetSquare, true) + " - " + minNeeded + "+ needed.");

                // Only do the die roll if it is not a king attacking a pawn (this move is automatic)
                if (!(initialPiece.type == "king" && targetPiece.type == "pawn"))
                {
                    yield return(Game.Controller.die.Roll());

                    int num = Game.Controller.die.GetResult();
                    success = num >= minNeeded;
                }

                // Set color of move history text
                color = success ? Color.green : Color.red;
            }

            // set commander as having moved
            initialPiece.GetCommander().SetMoved();

            // Add move history record
            Game.Controller.uiManager.InsertMoveRecord(notation, false, color);

            if (success)
            {
                if (chessMove.Attack)
                {
                    // Play sounds
                    SoundManger.PlayAttackSuccessSound();
                    yield return(new WaitForSeconds(0.7f));

                    SoundManger.PlayCaptureSound();

                    ChessPiece targetPiece = ChessGrid.GetPosition(chessMove.TargetSquare);
                    // if king was taken, a player has won the game
                    if (targetPiece.type == "king")
                    {
                        Game.Winner(targetPiece.Owner.Name == "player1" ? "player2" : "player1");
                    }
                    targetPiece.transform.GetChild(0).GetComponent <SpriteRenderer>().enabled = false;   // Disable commander icon on this piece

                    // transition command authority to king if bishop was captured
                    if (targetPiece.type == "bishop")
                    {
                        Player player = targetPiece.Owner;
                        player.RemainingMoves--;
                        if (targetPiece.Owner == PovManager.Instance.Pov)
                        {
                            CommandDelegation.Instance.DisableButton(targetPiece.GetDivision());
                        }
                        player.TransitionCommand(targetPiece.GetDivision());
                    }

                    // move piece to graveyard
                    Graveyard grave = targetPiece.Owner.Name == "player1" ? player1Graveyard : player2Graveyard;
                    targetPiece.moveable = false;
                    targetPiece.dragging = true;
                    ChessGrid.Captured.Add(targetPiece);
                    grave.AddToGrave(targetPiece.gameObject.transform);
                }

                // If piece moves to the square it attacked (not an archer)
                if (!attackOnly)
                {
                    SoundManger.PlayMoveSound();
                    ChessGrid.SetPositionEmpty(initialPiece.Position);
                    ChessGrid.SetPosition(chessMove.TargetSquare, initialPiece);
                    initialPiece.SpriteRenderer.sortingOrder = 1;
                    initialPiece.Position = chessMove.TargetSquare;
                }
                else
                {
                    ChessGrid.SetPositionEmpty(chessMove.TargetSquare);
                }
            }
            else
            { // Attack Failed
                Game.Controller.lastMoveFailed = true;
                SoundManger.PlayAtackFailedSound();
            }

            // Increment turn regardless of fail/success
            DestroyMovePlates();
            Game.Controller.IncrementTurnCounter();
        }