void OnHighlightedAsValidDestinationChanged() { if (this.highlightedAsValidDestination) { if (this.emptyModelShown && this.pieceType == PieceType.Empty) { GamePieceVisual.AnimateHighlightAsValidDestination(this.EmptyPieceMaterialAnimatableBrush, Colors.White, Colors.LawnGreen, GamePieceVisual.BaseEmptyPieceOpacity, 1); } else if (this.xModelShown && this.pieceType == PieceType.X) { GamePieceVisual.AnimateHighlightAsValidDestination(this.XPieceMaterialAnimatableBrush, Colors.Red, Colors.LawnGreen, GamePieceVisual.BasePlacedPieceOpacity, 1); } else if (this.oModelShown && this.pieceType == PieceType.O) { GamePieceVisual.AnimateHighlightAsValidDestination(this.OPieceMaterialAnimatableBrush, Colors.Red, Colors.LawnGreen, GamePieceVisual.BasePlacedPieceOpacity, 1); } } else { if (this.emptyModelShown && this.pieceType == PieceType.Empty) { GamePieceVisual.AnimateHighlightAsValidDestination(this.EmptyPieceMaterialAnimatableBrush, Colors.LawnGreen, Colors.White, 1, GamePieceVisual.BaseEmptyPieceOpacity); } else if (this.xModelShown && this.pieceType == PieceType.X) { GamePieceVisual.AnimateHighlightAsValidDestination(this.XPieceMaterialAnimatableBrush, Colors.LawnGreen, Colors.Red, 1, GamePieceVisual.BasePlacedPieceOpacity); } else if (this.oModelShown && this.pieceType == PieceType.O) { GamePieceVisual.AnimateHighlightAsValidDestination(this.OPieceMaterialAnimatableBrush, Colors.LawnGreen, Colors.Red, 1, GamePieceVisual.BasePlacedPieceOpacity); } } }
/// <summary> /// Walks the visual tree to find a <see cref="GamePieceVisual"/> from a /// <see cref="PlanarText"/> object. /// </summary> /// <param name="text"></param> /// <returns></returns> static GamePieceVisual GetGamePieceVisualFromText(PlanarText text) { bool flag = false; DependencyObject currentChild = text; GamePieceVisual foundPiece = null; int max = 100; int count = 0; while (flag == false) { DependencyObject parent = VisualTreeHelper.GetParent(currentChild); GamePieceVisual parentPiece = parent as GamePieceVisual; if (parentPiece != null) { foundPiece = parentPiece; flag = true; } else { if (parent is BoardAxes) { return(null); } currentChild = parent; } count = count + 1; if (count > max) { throw new GameException("A parent game piece on a text model could not be found."); } } return(foundPiece); }
void InitializeMap() { coordinatePieceMap.Clear(); int maxDimension = Constants.Dimension; for (int x = 0; x < maxDimension; x++) { for (int y = 0; y < maxDimension; y++) { for (int z = 0; z < maxDimension; z++) { Coordinate coordinate = new Coordinate( Convert.ToByte(x), Convert.ToByte(y), Convert.ToByte(z)); GamePieceVisual gamePiece = new GamePieceVisual(); gamePiece.PieceType = PieceType.Empty; gamePiece.PieceState = PieceState.Nothing; TranslateTransform3D transform = new TranslateTransform3D( Convert.ToDouble(x) * ConfigSettings.Instance.CellSpacing, Convert.ToDouble(y) * ConfigSettings.Instance.CellSpacing, Convert.ToDouble(z) * ConfigSettings.Instance.CellSpacing); gamePiece.Transform = transform; coordinatePieceMap.Add(coordinate, gamePiece); } } } }
/// <summary> /// If a piece on the board is currently highlighted, change it to an un-highlighted state. /// </summary> public void ClearHighlight() { if (this.highlightedPiece != null) { this.highlightedPiece.Highlighted = false; } this.highlightedPiece = null; }
/// <summary> /// Displays the valid source and its destinations as highlighted game pieces. /// </summary> public void Show() { sourcePiece.HighlightedAsValidSource = true; foreach (Coordinate destination in ValidMove.Destinations) { GamePieceVisual piece = controller.GetCoordinateGamePiece(destination); piece.HighlightedAsValidDestination = true; } }
void RedrawAllPieces() { //redraw everything foreach (Coordinate key in coordinatePieceMap.Keys) { GamePieceVisual gamePiece = (GamePieceVisual)coordinatePieceMap[key]; gamePiece.Redraw(); } }
/// <summary> /// Returns the valid source and its destinations to a normal, unhighlighted state. /// </summary> public void Hide() { sourcePiece.Reset(); foreach (Coordinate destination in ValidMove.Destinations) { GamePieceVisual piece = controller.GetCoordinateGamePiece(destination); piece.Reset(); } }
internal Coordinate GetCoordinateFromGamePiece(GamePieceVisual gamePiece) { foreach (Coordinate key in coordinatePieceMap.Keys) { if (coordinatePieceMap[key] == gamePiece) { return(key); } } throw new GameException("Coordinate could not be found from game piece."); }
void BuildBoardFromMoveHistory() { GamePieceVisual gamePiece; //reset everything foreach (Coordinate key in this.coordinatePieceMap.Keys) { gamePiece = (GamePieceVisual)this.coordinatePieceMap[key]; gamePiece.IsValidSource = false; gamePiece.HighlightedAsValidDestination = false; gamePiece.HighlightedAsValidSource = false; if (gamePiece.IsEnlarged) { gamePiece.ShrinkToNormalSize(); } Players player = this.board.GetPlayer(key); switch (player) { case Players.None: gamePiece.PieceType = PieceType.Empty; break; case Players.X: gamePiece.PieceType = PieceType.X; break; case Players.O: gamePiece.PieceType = PieceType.O; break; } } if (this.board.Winner == Players.None) { ReadOnlyCollection <ValidMove> validMoves = this.board.GetValidMoves(); foreach (ValidMove move in validMoves) { Coordinate source = move.Source; GamePieceVisual piece = this.coordinatePieceMap[source] as GamePieceVisual; if (piece != null) { piece.IsValidSource = true; } } } else { this.HighlightWinningPieces(); } }
/// <summary> /// Gets the <see cref="Quixo3D.Framework.Coordinate"/> that corresponds to the specified /// <see cref="GamePieceVisual"/>. /// </summary> /// <param name="piece"></param> /// <returns></returns> public Coordinate GetGamePieceCoordinate(GamePieceVisual piece) { foreach (Coordinate key in this.coordinatePieceMap.Keys) { GamePieceVisual foundPiece = this.coordinatePieceMap[key] as GamePieceVisual; if (foundPiece == piece) { return(key); } } throw new GameException("A coordinate for the specified game piece was not found."); }
void OnHighlightedAsValidSourceChanged() { if (this.highlightedAsValidSource && this.emptyModelShown && this.pieceType == PieceType.Empty) { GamePieceVisual.AnimateHighlightAsValidSource(this.EmptyPieceMaterialAnimatableBrush, Colors.White, Colors.Yellow, GamePieceVisual.BaseEmptyPieceOpacity, 1); } else if (this.highlightedAsValidSource == false && this.emptyModelShown && this.pieceType == PieceType.Empty) { GamePieceVisual.AnimateHighlightAsValidSource(this.EmptyPieceMaterialAnimatableBrush, Colors.Yellow, Colors.White, 1, GamePieceVisual.BaseEmptyPieceOpacity); if (this.enlarged) { this.ShrinkToNormalSize(); } } }
void ExecuteMove(GamePieceVisual sourcePiece, GamePieceVisual destinationPiece) { Coordinate source = GetCoordinateFromGamePiece(sourcePiece); Coordinate destination = GetCoordinateFromGamePiece(destinationPiece); Move newMove = new Move(source, destination); Player currentPlayer = board.Current; board.MovePiece(newMove); OnMoveMade(currentPlayer, newMove); BuildBoardFromMoveHistory(); CheckForMoveGeneration(); }
public void ShowValidMovePieces(ValidMove validMove) { Coordinate sourceCoordinate = validMove.Source; GamePieceVisual sourcePiece = coordinatePieceMap[sourceCoordinate] as GamePieceVisual; sourcePiece.PieceState = PieceState.ValidSource; sourcePiece.Highlighted = true; sourcePiece.Redraw(); foreach (Coordinate destinationCoordinate in validMove.Destinations) { GamePieceVisual destinationPiece = coordinatePieceMap[destinationCoordinate] as GamePieceVisual; destinationPiece.PieceState = PieceState.ValidDestination; destinationPiece.Redraw(); } }
public void ShowValidSources() { pendingSourcePiece = null; GamePieceVisual gamePiece; ReadOnlyCollection <ValidMove> validMoves = board.GetValidMoves(); foreach (ValidMove move in validMoves) { Coordinate source = move.Source; gamePiece = (GamePieceVisual)coordinatePieceMap[source]; gamePiece.PieceState = PieceState.ValidSource; gamePiece.Highlighted = false; gamePiece.Redraw(); } RedrawAllPieces(); turnState = TurnState.Idle; }
void ShowDestinations(GamePieceVisual gamePiece) { pendingSourcePiece = gamePiece; ValidMove targetMove = null; ReadOnlyCollection <ValidMove> moves = board.GetValidMoves(); foreach (ValidMove move in moves) { if (coordinatePieceMap[move.Source] == gamePiece) { targetMove = move; break; } } ShowValidMovePieces(targetMove); //RedrawAllPieces(); turnState = TurnState.ShowingDestinations; }
public GamePieceVisualInfo GetPieceInfo(Viewport3D viewport, Point location) { ModelVisual3D selectedItem = GetHitTestResult(viewport, location); if (selectedItem == null) { return(null); } if (selectedItem is GamePieceVisual) { GamePieceVisual gamePiece = selectedItem as GamePieceVisual; GamePieceVisualInfo info = gamePiece.GetInfoObject(this); return(info); } return(null); }
void InitializeMap() { this.coordinatePieceMap = new Hashtable(); ModelVisual3D container = GetPieceContainer(); this.pieces = new Collection <GamePieceVisual>(); container.Children.Clear(); int maxDimension = Constants.Dimension; for (byte x = 0; x < maxDimension; x++) { for (byte y = 0; y < maxDimension; y++) { for (byte z = 0; z < maxDimension; z++) { Coordinate coordinate = new Coordinate(x, y, z); GamePieceVisual gamePiece = new GamePieceVisual(); gamePiece.BaseGridTransform.OffsetX = Convert.ToDouble(x) * ConfigSettings.Instance.CellSpacing; gamePiece.BaseGridTransform.OffsetY = Convert.ToDouble(y) * ConfigSettings.Instance.CellSpacing; gamePiece.BaseGridTransform.OffsetZ = Convert.ToDouble(z) * ConfigSettings.Instance.CellSpacing; container.Children.Add(gamePiece); this.pieces.Add(gamePiece); this.coordinatePieceMap.Add(coordinate, gamePiece); } } } this.axes = new BoardAxes(); this.axes.Visible = true; container.Children.Add(axes); double centerMovement = ConfigSettings.Instance.CellSpacing * 2 * -1; TranslateTransform3D centering = new TranslateTransform3D(centerMovement, centerMovement, centerMovement); container.Transform = centering; }
public void ExecuteHitTest(Viewport3D viewport, Point location) { ModelVisual3D selectedItem = GetHitTestResult(viewport, location); if (selectedItem == null) { return; } if (selectedItem is GamePieceVisual) { GamePieceVisual gamePiece = selectedItem as GamePieceVisual; if (gamePiece.PieceState == PieceState.ValidSource) { if (turnState == TurnState.Idle) { ShowDestinations(gamePiece); } else if (turnState == TurnState.ShowingDestinations) { //turn off destinations, go back to idle ShowValidSources(); } } else if (gamePiece.PieceState == PieceState.ValidDestination) { if (turnState == TurnState.ShowingDestinations) { //execute move if (pendingSourcePiece == null) { throw new GameException("There was no currently selected source piece."); } ExecuteMove(pendingSourcePiece, gamePiece); } } } }
/// <summary> /// Creates a new instance of a <see cref="GamePieceVisual"/>. /// </summary> public GamePieceVisual() { emptyModel = this.CreateEmptyModel(); xModel = this.CreateXModel(); oModel = this.CreateOModel(); wireFrame = GamePieceVisual.CreateCubeWireFrame(); Model3DGroup group = new Model3DGroup(); group.Children.Add(emptyModel); group.Children.Add(oModel); this.Content = group; this.Children.Add(wireFrame); this.Children.Add(xModel); this.transforms = new Transform3DGroup(); this.baseGridTransform = new TranslateTransform3D(); this.transforms.Children.Add(this.baseGridTransform); this.Transform = this.transforms; }
/// <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(); } } } }
/// <summary> /// Sizes the game piece back to its original size. /// </summary> public void ShrinkToNormalSize() { double baseScale = .9; double endScale = baseScale + lastEnlargeAmount; if (emptyModelShown && pieceType == PieceType.Empty) { GamePieceVisual.AnimateScale(this.emptyAnimatedScale, endScale, baseScale); GamePieceVisual.AnimateHighlight(this.EmptyPieceMaterialAnimatableBrush, BaseEmptyPieceOpacity + .1, BaseEmptyPieceOpacity); } else if (xModelShown && pieceType == PieceType.X) { GamePieceVisual.AnimateScale(this.xAnimatedScale, endScale, baseScale); GamePieceVisual.AnimateHighlight(this.XPieceMaterialAnimatableBrush, 1, BasePlacedPieceOpacity); } else if (oModelShown && pieceType == PieceType.O) { GamePieceVisual.AnimateScale(this.oAnimatedScale, endScale, baseScale); GamePieceVisual.AnimateHighlight(this.OPieceMaterialAnimatableBrush, 1, BasePlacedPieceOpacity); } this.enlarged = false; }
/// <summary> /// A helper method used to find a <see cref="GamePieceVisual"/> object by either direct /// casting or walking the visual tree. /// </summary> /// <param name="hitTestResult"></param> /// <returns></returns> static GamePieceVisual ConvertHitTestResultToGamePieceVisual(ModelVisual3D hitTestResult) { if (hitTestResult == null) { return(null); } PlanarText textResult = hitTestResult as PlanarText; if (textResult != null) { return(GameBoardController.GetGamePieceVisualFromText(textResult)); } GamePieceVisual pieceResult = hitTestResult as GamePieceVisual; if (pieceResult != null) { return(pieceResult); } return(null); }
/// <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; } } }
/// <summary> /// Makes the game piece bigger by a specified amount. /// </summary> /// <param name="amount">A value of 1.00 will result in no change. 0.50 will result in a 50% /// decrease in size. 2.00 will result in a 200% increase in size.</param> public void Enlarge(double amount) { double baseScale = .9; double endScale = baseScale + amount; this.lastEnlargeAmount = amount; if (emptyModelShown && pieceType == PieceType.Empty) { GamePieceVisual.AnimateScale(this.emptyAnimatedScale, baseScale, endScale); GamePieceVisual.AnimateHighlight(this.EmptyPieceMaterialAnimatableBrush, BaseEmptyPieceOpacity, BaseEmptyPieceOpacity + .1); } else if (xModelShown && pieceType == PieceType.X) { GamePieceVisual.AnimateScale(this.xAnimatedScale, baseScale, endScale); GamePieceVisual.AnimateHighlight(this.XPieceMaterialAnimatableBrush, BasePlacedPieceOpacity, 1); } else if (oModelShown && pieceType == PieceType.O) { GamePieceVisual.AnimateScale(this.oAnimatedScale, baseScale, endScale); GamePieceVisual.AnimateHighlight(this.OPieceMaterialAnimatableBrush, BasePlacedPieceOpacity, 1); } this.enlarged = true; }
static void AnimateHighlightAsValidDestination(SolidColorBrush targetBrush, Color startColor, Color endColor, double startOpacity, double endOpacity) { GamePieceVisual.AnimateColorAndOpacity(targetBrush, startColor, endColor, startOpacity, endOpacity); }
/// <summary> /// Creates a new instance of a <see cref="ExecuteMoveOperation"/>. /// </summary> /// <param name="controller"></param> /// <param name="sourcePiece"></param> /// <param name="destinationPiece"></param> public ExecuteMoveOperation(GameBoardController controller, GamePieceVisual sourcePiece, GamePieceVisual destinationPiece) { this.controller = controller; this.sourcePiece = sourcePiece; this.destinationPiece = destinationPiece; }
/// <summary> /// Creates a new instance of the class. /// </summary> /// <param name="controller"></param> /// <param name="sourcePiece"></param> public ShowValidMoveOperation(GameBoardController controller, GamePieceVisual sourcePiece) { this.sourcePiece = sourcePiece; this.controller = controller; }