예제 #1
0
        private PositionableModel GetClosestClickedModel()
        {
            PositionableModel closestModel = null;
            Cursor            cursor       = this.ScreenManager.Game.Components.OfType <Cursor>().First();
            Ray cursorRay =
                cursor.CalculateCursorRay(BaseCamera.ActiveCamera.Projection, BaseCamera.ActiveCamera.View);

            // Keep track of the closest object we have seen so far, so we can
            // choose the closest one if there are several models under the cursor.
            float closestIntersection = float.MaxValue;

            List <PositionableModel> piecesAndSelectedSquares = new List <PositionableModel>();

            foreach (Square sq in squares)
            {
                if (sq.Colour == Game.StatusCurrent.ColorSelectedSquares)
                {
                    piecesAndSelectedSquares.Add(sq);
                }
                if (sq.OccupyingPiece != null)
                {
                    piecesAndSelectedSquares.Add(sq.OccupyingPiece);
                }
            }

            // Loop over all our models.
            foreach (PositionableModel mdl in piecesAndSelectedSquares)
            {
                Vector3 vertex1, vertex2, vertex3;

                // Perform the ray to model intersection test.
                float?intersection = Utilities.RayIntersectsModel(cursorRay, mdl.Model,
                                                                  mdl.WorldTransform,
                                                                  out vertex1, out vertex2,
                                                                  out vertex3);
                // Do we have a per-triangle intersection with this model?
                // If so, is it closer than any other model we might have
                // previously intersected?

                if ((intersection != null) && (intersection < closestIntersection))
                {
                    // Store information about this model.
                    closestIntersection = intersection.Value;
                    closestModel        = mdl;
                }
            }
            return(closestModel);
        }
예제 #2
0
        public override void HandleInput(InputState input)
        {
            KeyboardState keybState = Keyboard.GetState();

            if ((keybState.IsKeyUp(Keys.T) && (IsTKeyDown)))
            {
                IsTKeyDown = false;
            }
            if (((keybState.IsKeyDown(Keys.T))
                 &&
                 ((keybState.IsKeyDown(Keys.LeftControl)) || (keybState.IsKeyDown(Keys.RightControl))))
                &&
                (!IsTKeyDown))
            {
                IsTKeyDown = true;

                //facciamo in modo che se si tira indietro tutti i giocatori diventino umani
                foreach (Side s in Game.StatusCurrent.Sides)
                {
                    s.PlayerType = PlayerType.Human;
                }

                //ricarichiamo la form in modo che mostri i dati aggiornati
                Game.FormCollection["frmGamePlayScreen"].Show();

                ChessboardLogics.Takeback();
                UpdateSquares();
            }

            if (!ChessboardLogics.IsComputerThinking() && (Mouse.GetState().LeftButton == ButtonState.Pressed))
            {
                PositionableModel closestClickedModel = GetClosestClickedModel();

                if (closestClickedModel != null)
                {
                    if (closestClickedModel.GetType() == typeof(Square)) //it is a selected square for sure
                    {
                        Square sq   = closestClickedModel as Square;
                        string move = SelectedPiece.OccupiedSquare.ToString() +
                                      sq.ToString();

                        if (ChessboardLogics.IsPromo(move))
                        {
                            screenManager.AddScreen(
                                new PromotionSelectionMenuScreen(move, SelectedPiece.OccupiedSquare, sq),
                                this.controllingPlayer
                                );
                        }
                        else
                        {
                            ChessboardLogics.MakeMove(move);
                            Game.StatusCurrent.Result = (PossibleResult)ChessboardLogics.GetResult();
                            //move the piece
                            SelectedPiece.OccupiedSquare.OccupyingPiece = null;
                            sq.OccupyingPiece = SelectedPiece;
                            Game.FormCollection["frmGamePlayScreen"]["lblTurn"].Text = "It's " + Game.StatusCurrent.SideToMove + " turn";

                            //UpdateSquares();
                            //if (Game.StatusCurrent.SideToMove == SideType.White)
                            //    Game.StatusCurrent.SideToMove = SideType.Black;
                            //else
                            //    Game.StatusCurrent.SideToMove = SideType.White;
                        }
                        SelectedPiece.IsSelected = false;
                        SelectedPiece            = null;
                        foreach (Square sqr in squares)
                        {
                            sqr.IsSelected = false;
                        }
                    }
                    else //else it's a piece
                    {
                        Piece clickedPiede = closestClickedModel as Piece;
                        if (clickedPiede.Side == Game.StatusCurrent.SideToMove)
                        {
                            if (SelectedPiece != null)
                            {
                                SelectedPiece.IsSelected = false;
                            }
                            //select all moveable squares
                            SelectedPiece            = closestClickedModel as Piece;
                            SelectedPiece.IsSelected = true;
                            string from = SelectedPiece.OccupiedSquare.ToString();

                            foreach (Square sq in squares)
                            {
                                string to = sq.ToString();
                                if (ChessboardLogics.CanMove(from + to) != 0)
                                {
                                    sq.IsSelected = true;
                                }
                                else
                                {
                                    sq.IsSelected = false;
                                }
                            }
                        }
                    }
                }
            }

            base.HandleInput(input);
        }