예제 #1
0
    //-------------------------------------------------------------------------
    public void UndoCommand(Command Cmd)
    {
        UChess chess = this[Cmd.To];

        // 记录老坐标
        Point lastChessPoint = chess.point;
        // 计算新坐标
        Point newChessPoint = ToChessPoint(Cmd.From, chess.campType);

        // 将棋子从原始位置移除
        this[chess.ToChessboardPoint()] = null;
        // 设置新坐标
        chess.point = newChessPoint;
        // 移动位置
        this[chess.ToChessboardPoint()] = chess;
        // 坐标还原
        chess.ResetPosition();

        // 如果吃掉了棋子,还原棋子
        if (Cmd.EatedHistory != null)
        {
            AddChess(Cmd.EatedHistory);
            Cmd.EatedHistory = null;
        }

        // 切换回合
        ToggleTun();

        History.RemoveAt(History.Count - 1);
    }
예제 #2
0
 //-------------------------------------------------------------------------
 // 添加一个棋子到棋盘上(如果指定坐标已经有棋子了,吃掉棋子)
 public void AddChess(UChess c)
 {
     if (this[c.ToChessboardPoint()] != null)
     {
         RemoveChess(this[c.ToChessboardPoint()]);
     }
     this[c.ToChessboardPoint()] = c;
     c.onAdded();
 }
예제 #3
0
    //-------------------------------------------------------------------------
    public void DoCommand(Command Cmd)
    {
        UChess chess = this[Cmd.From];

        // 记录老坐标
        Point lastChessPoint = chess.point;
        // 计算新坐标
        Point newChessPoint = ToChessPoint(Cmd.To, chess.campType);

        // 吃掉新坐标的棋子
        if (this[Cmd.To] != null && this[Cmd.To] != chess)
        {
            Cmd.EatedHistory = this[Cmd.To];
            RemoveChess(this[Cmd.To]);

            // 胜负判定
            if (!game_over)
            {
                if (Judge(Cmd.EatedHistory))
                {
                    game_over = true;
                    if (onGameOver != null)
                    {
                        onGameOver(Winner);
                    }
                }
            }
        }

        // 将棋子从原始位置移除
        this[chess.ToChessboardPoint()] = null;

        // 设置新坐标
        chess.point = newChessPoint;
        // 移动位置
        this[chess.ToChessboardPoint()] = chess;
        // 事件通知
        chess.ResetPosition();

        // 切换玩家
        ToggleTun();

        // 记录
        History.Add(Cmd);
    }
예제 #4
0
 //-------------------------------------------------------------------------
 // 从棋盘移除一个棋子
 public void RemoveChess(UChess c)
 {
     this[c.ToChessboardPoint()] = null;
     c.onRemoved();
 }
    public override void Update()
    {
        //H键切换视图
        if (Input.GetKeyDown(KeyCode.H))
        {
            if (ViewType == ECampType.Red)
            {
                ViewType = ECampType.Black;
            }
            else
            {
                ViewType = ECampType.Red;
            }
            Gamer.Chessboard.SetPlayerView(ViewType);
        }

        switch (MyOperateStep)
        {
        case EOpStep.Select:
        {
            //选择棋子
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit HitInfo;
                if (Physics.Raycast(Gamer.Chessboard.GetViewCamera().ScreenPointToRay(Input.mousePosition), out HitInfo))
                {
                    UChess HitChess = Gamer.Chessboard[Gamer.Chessboard.WorldToPos(HitInfo.point)];
                    if (HitChess != null && HitChess.campType == MyCamp)
                    {
                        selectedChess = HitChess;
                        MyOperateStep = EOpStep.Push;
                        ShowMoveTargets();
                    }
                }
            }
        }
        break;

        case EOpStep.Push:
        {
            RaycastHit HitInfo;
            if (Physics.Raycast(Gamer.Chessboard.GetViewCamera().ScreenPointToRay(Input.mousePosition), out HitInfo))
            {
                if (selectedChess.gameObject != null)
                {
                    selectedChess.gameObject.transform.position = HitInfo.point;
                }


                if (Input.GetMouseButtonDown(0))
                {
                    Point NewPoint = Gamer.Chessboard.WorldToPos(HitInfo.point);
                    if (selectedChess.GetAvailablePoints().Contains(Gamer.Chessboard.ToChessPoint(NewPoint, selectedChess.campType)))
                    {
                        Command Cmd = new Command();
                        Cmd.Camp = MyCamp;
                        Cmd.From = selectedChess.ToChessboardPoint();
                        Cmd.To   = NewPoint;
                        Gamer.Chessboard.DoCommand(Cmd);
                    }
                    else
                    {
                        selectedChess.ResetPosition();
                    }

                    ClearMoveTargets();

                    MyOperateStep = EOpStep.Select;
                }
            }
        }
        break;

        default:
            break;
        }
    }