예제 #1
0
    private void CancelLockOn(ChessPieceProperties target)
    {
        // cancel lock on
        target.LockOn(false);

        // remove all visualization of valid move
        foreach (Transform validMoveVisual in validMoveVisualList)
        {
            validMoveVisual.DOMove(((Vector2)lockedOnPiece.transform.position) + lockedOnPiece.GraphicPosition, cursorMoveSpeed, false);
            Destroy(validMoveVisual.gameObject, cursorMoveSpeed);
        }

        // remove all visualization of threateningChess
        foreach (ChessPieceProperties attackingChess in threateningChess)
        {
            attackingChess.Threatened(false);
        }
        threateningChess.Clear();

        // reset this list 
        validMoveVisualList.Clear();
        hoveringValidMove = null;

        // clear memory
        lockedOnPiece = null;

        // sound effect
        AudioManager.Instance.PlaySFX("cancellockon", 0.75f);
    }
예제 #2
0
    private void LockOnChess(ChessPieceProperties piece, TileIndex index)
    {
        piece.LockOn(true);
        // store this piece into memory
        lockedOnPiece = piece;
        lockedOnPieceIndex = index;

        // get all valid move for this chess piece
        var validMoves = moveManager.logic.GetValidMoves(index);

        // visualize valid move
        foreach (TileIndex validMove in validMoves)
        {
            var validMoveVisual = new GameObject(lockedOnPiece.gameObject.name + "'s possible move");
            validMoveVisual.transform.position = moveManager.board.GetTileCenter(index.row, index.col) + lockedOnPiece.GraphicPosition;
            validMoveVisual.transform.DOMove(moveManager.board.GetTileCenter(validMove.row, validMove.col) + lockedOnPiece.GraphicPosition, cursorMoveSpeed, false);
            var newRenderer = validMoveVisual.AddComponent<SpriteRenderer>();
            var originRenderer = lockedOnPiece.SpriteRenderer;
            newRenderer.sprite = originRenderer.sprite;
            newRenderer.color = new Color(originRenderer.color.r, originRenderer.color.g, originRenderer.color.b, originRenderer.color.a / 4f);
            newRenderer.sortingLayerID = SpriteRenderer.sortingLayerID; // same layer with cursor
            newRenderer.sortingOrder = originRenderer.sortingOrder;

            newRenderer.transform.localScale = originRenderer.transform.localScale;
            validMoveVisualList.Add(validMoveVisual.transform);
            hoveringValidMove = null;

            // check if this is an attack move
            var attackTarget = moveManager.board.GetTilePiecePropertiesAt(validMove);
            if (attackTarget != null
                && attackTarget.Team != cursorTeam)
            {
                newRenderer.sprite = null;
                attackTarget.Threatened(true);
                threateningChess.Add(attackTarget);
            }
        }
    }
예제 #3
0
    private void MoveChess()
    {
        TileIndex moveTargetIndex = new TileIndex(currentPosition.y, currentPosition.x);

        // check if this is a normal move or attacking
        GameObject targetChess = moveManager.board.GetTilePieceAt(moveTargetIndex.row, moveTargetIndex.col);
        bool isAttackMove = false;

        if (targetChess != null)
        {
            isAttackMove = true;
        }
        else if (IsEnPassant(lockedOnPieceIndex, moveTargetIndex, lockedOnPiece))
        {
            isAttackMove = true;
            targetChess = moveManager.board.GetTilePieceAt(lockedOnPieceIndex.row, moveTargetIndex.col);
        }
        else if (IsCastling(lockedOnPieceIndex, moveTargetIndex, lockedOnPiece))
        {
            //Castling!!!!!!!!!
            Castling(moveTargetIndex);
        }

        if (isAttackMove)
        {
            // this is an attack
            targetChess.GetComponent<ChessPieceProperties>().Attacked(0.15f);
            Destroy(targetChess, 1f);
            AudioManager.Instance.PlaySFX("attack", 0.5f);
            AudioManager.Instance.PlaySFX("compact", 0.5f);
        }

        // move the selected chess piece to this position index
        Vector2 newPiecePosition = moveManager.MoveChessPiece(lockedOnPiece.gameObject, lockedOnPieceIndex, moveTargetIndex);

        // move the chess piece graphic
        lockedOnPiece.Move(newPiecePosition);

        // canccel the lock on
        lockedOnPiece.LockOn(false);

        // remove all visualization of valid move
        foreach (Transform validMoveVisual in validMoveVisualList)
        {
            // remove it immediately only if it's not the moving target. 
            float removeTime = validMoveVisual == hoveringValidMove ? cursorMoveSpeed : 0f;
            Destroy(validMoveVisual.gameObject, removeTime);
        }

        // remove all visualization of threateningChess
        foreach (ChessPieceProperties attackingChess in threateningChess)
        {
            attackingChess.Threatened(false);
        }
        threateningChess.Clear();

        // reset list 
        validMoveVisualList.Clear();
        hoveringValidMove = null;

        // is this a pawn promotion?
        if (lockedOnPiece.GetComponent<ChessPieceProperties>().Type == PieceType.Pawn
            && (moveTargetIndex.row == 7 || moveTargetIndex.row == 0))
        {
            gamestate.Promotion(moveTargetIndex);
        }
        else if (moveManager.logic.IsInCheckmate(cursorTeam == Team.White ? Team.Black : Team.White))
        {
            // checkmate detected. restart the game
            gamestate.GameEnd();
        }
        else
        {
            // pass the turn to other side
            gamestate.Turn();
        }

        // clear memory
        lockedOnPiece = null;
    }