コード例 #1
0
    /// <summary>
    /// Attempts to carry out the desired move.
    /// </summary>
    /// <returns></returns>
    public bool Execute()
    {
        // check if valid move
        if (!CheckValid())
        {
            result = ResultType.Failed;
            return(false);
        }

        bool forcedWayMove;
        bool srcIsWay  = src.type == TileNode.Type.Way || src.type == TileNode.Type.WayCross;
        bool destIsWay = dest.type == TileNode.Type.Way || dest.type == TileNode.Type.WayCross;

        // determine inital type
        #region determine move type
        // same position
        if (src == dest)
        {
            type = MoveType.Move;

            // move to empty tile
        }
        else if (dest.tower == null)
        {
            type = MoveType.Move;
            src.tower.MoveTo(destMove);

            // attacking move on existing tower
        }
        else
        {
            PieceTower attacker = src.tower;
            PieceTower victim   = dest.tower;
            attacker.owningPlayer.capture += victim.pieces.Count;

            // check attack type
            if ((dest.type == TileNode.Type.Way || dest.type == TileNode.Type.WayCross) &&
                (src.type == TileNode.Type.Way || src.type == TileNode.Type.WayCross))
            {
                type = MoveType.WayAttack;
            }
            else
            {
                type = MoveType.Attack;
            }

            // Check if this move results in a win
            if (CheckWin())
            {
                result = ResultType.Win;
                for (int i = 0; i < victim.pieces.Count; i++)
                {
                    attacker.AddPiece(victim.pieces[i]);
                }
                victim.Die();
                attacker.MoveTo(destMove);
                attacker.owningPlayer.updateScore();
                victim.owningPlayer.updateScore();
                return(true);
            }

            // trigger overstack UI if too many pieces
            if (victim.pieces.Count + attacker.pieces.Count > 6 || (victim.GetHook() != null && attacker.GetHook() != null))
            {
                // Start the overstack UI and return immediately
                GameObject.Find("UICanvas").transform.Find("Overstack").GetComponent <OverstackUI>().Construct(attacker, victim);
                attacker.owningPlayer.HighestTower = 6;
            }
            else
            {
                // transfer all pieces to attacker
                for (int i = 0; i < victim.pieces.Count; i++)
                {
                    attacker.AddPiece(victim.pieces[i]);
                }
                victim.Die();
                attacker.MoveTo(destMove);
            }
            //update tower info
            attacker.owningPlayer.HighestTower = attacker.pieces.Count;

            // update scores
            attacker.owningPlayer.updateScore();
            victim.owningPlayer.updateScore();
        }
        #endregion

        #region checking waymove
        forcedWayMove = true;

        // if not moving to way
        if (!destIsWay)
        {
            forcedWayMove = false;
        }

        // endturn if moving along way (after way move)
        if ((srcIsWay && destIsWay) && (src.wayLine == dest.wayLine) && !destMove.hookInvolved)
        {
            forcedWayMove = false;
        }

        // check for proper forced way
        if (forcedWayMove)
        {
            Debug.Assert(forcedWayMove && destIsWay, "Way conditions did not match");
        }
        #endregion

        if (result == ResultType.Undetermined)
        {
            if (forcedWayMove)
            {
                if (dest.type == TileNode.Type.Way)
                {
                    result = ResultType.ForcedWay;
                }
                else
                {
                    result = ResultType.ForcedWayCross;
                }
            }
            else
            {
                result = ResultType.Normal;
            }
        }

        return(true);
    }