Ejemplo n.º 1
0
        /// <summary>
        /// Takes user interface input in the form of a mouse pointer location and the containing
        /// 3D viewport and attempts to "select" the piece that exists at that location.  If a piece is
        /// found, the appropriate game operation is performed, such as highlighting a valid source, or
        /// executing a move.
        /// </summary>
        /// <param name="location"></param>
        /// <param name="viewport"></param>
        public void SelectPiece(Point location, Viewport3D viewport)
        {
            if (this.GameOver || this.showingOnlyPlacedPieces)
            {
                return;
            }

            ModelVisual3D   hitTestResult = GameBoardController.GetHitTestResult(viewport, location);
            GamePieceVisual targetPiece   = GameBoardController.ConvertHitTestResultToGamePieceVisual(hitTestResult);

            if (this.currentShowValidMoveOperation != null)
            {
                if (targetPiece != null && this.currentShowValidMoveOperation.ValidDestinationPieces.Contains(targetPiece))
                {
                    //execute move
                    ExecuteMoveOperation operation = new ExecuteMoveOperation(this, this.currentShowValidMoveOperation.SourcePiece, targetPiece);
                    this.currentShowValidMoveOperation.Hide();
                    this.currentShowValidMoveOperation = null;
                    operation.Execute();
                    this.moveOperations.Add(operation);
                    this.BuildBoardFromMoveHistory();
                    this.OnMoveMade(operation);
                    this.CheckForEngineMove();
                    this.isDirty = true;
                }
            }
            else
            {
                if (targetPiece != null)
                {
                    if (targetPiece.IsValidSource)
                    {
                        this.currentShowValidMoveOperation = new ShowValidMoveOperation(this, targetPiece);
                        this.currentShowValidMoveOperation.Show();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to highlight a game piece at the specified mouse pointer location.
        /// </summary>
        /// <remarks>
        /// This method is primarily used for mouse "rollover" actions.
        /// </remarks>
        /// <param name="location"></param>
        /// <param name="viewport"></param>
        public void HighlightPiece(Point location, Viewport3D viewport)
        {
            if (this.GameOver || this.currentShowValidMoveOperation != null || this.showingOnlyPlacedPieces)
            {
                return;
            }

            ModelVisual3D selectedItem = GameBoardController.GetHitTestResult(viewport, location);

            if (selectedItem == null)
            {
                if (this.highlightedPiece != null)
                {
                    this.highlightedPiece.Highlighted = false;
                }
                return;
            }

            GamePieceVisual targetPiece = GameBoardController.ConvertHitTestResultToGamePieceVisual(selectedItem);

            if (targetPiece != null)
            {
                if (targetPiece != this.highlightedPiece)
                {
                    if (this.highlightedPiece != null)
                    {
                        this.highlightedPiece.Highlighted = false;
                    }
                }

                this.highlightedPiece = targetPiece;
                if (targetPiece.IsValidSource)
                {
                    this.highlightedPiece.Highlighted = true;
                }
            }
        }