Exemple #1
0
    private void OnSuccessfulMove(Slot to, MoveActionTypes action, int stepPlayed)
    {
        var movesPlayedList = GameManager.instance.currentPlayer.movesPlayed;

        Debug.LogWarning(action);

        // log played moves for undo
        movesPlayedList.Add(new Move {
            piece = this, from = currentSlot, to = to, step = stepPlayed, action = action
        });

        //---------------------------------------
        // action events
        //---------------------------------------
        // move enemy to bar
        if ((action & MoveActionTypes.Hit) == MoveActionTypes.Hit)
        {
            var enemyPiece = to.GetComponent <Slot>().pieces.Last();
            var enemyBar   = Slot.GetBar(Piece.GetEnemyType(pieceType));

            enemyPiece.PlaceOn(enemyBar.GetComponent <Slot>());
        }

        // move yourself to outside
        if ((action & MoveActionTypes.Bear) == MoveActionTypes.Bear)
        {
            var slotOutside = Slot.GetOutside(pieceType);

            PlaceOn(slotOutside.GetComponent <Slot>());

            // check round finish
            GameManager.instance.CheckRoundFinish();
        }
        // place on new slot
        else
        {
            PlaceOn(to);
        }
    }
Exemple #2
0
    private void OnPieceRelease()
    {
        BeforeRelease();

        if (multipleSelection)
        {
            collisionSlot = multipleSelectionList.Last().collisionSlot;
        }

        // if collision not happen
        if (collisionSlot == null)
        {
            // reset the position
            ResetToOldPosition();
        }
        else
        {
            // current player
            var currentPlayer = GameManager.instance.currentPlayer;
            // get moves left
            var movesLeft = DiceController.instance.GetMovesLeftList(currentPlayer.movesPlayed.Select(x => x.step));

            MoveActionTypes action     = MoveActionTypes.Move;
            MoveError       error      = MoveError.Unknown;
            int             stepPlayed = -1;

            // loop through dice values
            foreach (var step in movesLeft)
            {
                stepPlayed = step;
                error      = Rule.ValidateMove(this, collisionSlot, step, out action);

                // if the move valid, do not continue
                if (error == MoveError.NoError)
                {
                    break;
                }
            }

            // move to place if move was valid,
            if (error == MoveError.NoError)
            {
                OnSuccessfulMove(action, stepPlayed);
            }
            // else try combining dice values to get there
            else
            {
                ICollection <Move> movesPlayed;

                error = Rule.ValidateCombinedMove(this, collisionSlot, movesLeft, out movesPlayed);

                // if there are any combined move, move
                if (error == MoveError.NoError)
                {
                    foreach (var move in movesPlayed)
                    {
                        OnSuccessfulMove(move.to, move.action, move.step);
                    }
                }
                // roll back to the position you were before
                else
                {
                    OnFailedMove(error);
                }
            }
        }

        AfterRelease();
    }
Exemple #3
0
 private void OnSuccessfulMove(MoveActionTypes action, int stepPlayed)
 {
     OnSuccessfulMove(collisionSlot, action, stepPlayed);
 }
Exemple #4
0
    public static MoveError ValidateMove(Piece piece, Slot requestedSlot, int steps, out MoveActionTypes action)
    {
        action = MoveActionTypes.Move;
        var requiredStep = Slot.GetRequiredStepCount(piece.currentSlot, requestedSlot);

        //---------------------------------------
        // handle errors
        //---------------------------------------
        if (requestedSlot.slotType == SlotType.Outside && !IsAllPiecesHome(piece.pieceType))
        {
            return(MoveError.AllPiecesNotInHome);
        }

        if (requestedSlot.slotType == SlotType.Outside)
        {
            var lastSlot = Slot.GetLastSlotThatHasPiece(piece.pieceType);
            var requiredStepFromLastSlot = Slot.GetRequiredStepCount(lastSlot, requestedSlot);

            if (piece.currentSlot != lastSlot && requiredStep != steps)
            {
                return(MoveError.NotEnoughSteps);
            }

            if (piece.currentSlot == lastSlot && steps < requiredStepFromLastSlot)
            {
                return(MoveError.NotEnoughSteps);
            }
        }

        if (requestedSlot.slotType != SlotType.Outside && requiredStep != steps)
        {
            return(MoveError.NotEnoughSteps);
        }

        if (IsSlotBlockedByEnemy(requestedSlot, Piece.GetEnemyType(piece.pieceType)))
        {
            return(MoveError.BlockedByEnemy);
        }

        if (!IsMovingToHome(piece, requestedSlot))
        {
            return(MoveError.WrongHomeDirection);
        }

        //---------------------------------------
        // handle actions
        //---------------------------------------
        // bear action
        if (requestedSlot.slotType == SlotType.Outside)
        {
            action |= MoveActionTypes.Bear;
        }

        // recover action
        else if (piece.currentSlot.slotType == SlotType.Bar)
        {
            action |= MoveActionTypes.Recover;
        }

        // if requested slot empty
        if (IsSlotEmpty(requestedSlot))
        {
            action |= MoveActionTypes.Move;
        }
        // if requested slot not empty, and requested slot is yours
        else if (IsSlotYours(requestedSlot, piece.pieceType))
        {
            action |= MoveActionTypes.Move;
        }
        // if requested slot not empty, and requested is not yours, and requested slot not blocked by enemy
        else if (!IsSlotBlockedByEnemy(requestedSlot, Piece.GetEnemyType(piece.pieceType)))
        {
            action |= MoveActionTypes.Hit;
        }

        return(MoveError.NoError);
    }