Ejemplo n.º 1
0
        public void UpdateForNewPosition()
        {
            const int duration = 400;

            this.UpdatePieceSizes();

            Storyboard animation = new Storyboard();

            animation.Duration = TimeSpan.FromMilliseconds(duration + 25);
            this.Resources.Add("tmpTimline" + this.currentAnimation++, animation);

            for (int i = 0; i < this.position.PieceList.Length; i++)
            {
                byte oldPieceLocation = 0xFF;
                if (this.oldPieceList != null)
                {
                    oldPieceLocation = this.oldPieceList[i];
                }

                byte newPieceLocation = this.position.PieceList[i];

                Piece oldPiece = oldPieceLocation != 0xFF ? this.pieces[this.oldBoard[oldPieceLocation]] : null;
                Piece newPiece = newPieceLocation != 0xFF ? this.pieces[this.position.Board[newPieceLocation]] : null;

                if (oldPieceLocation != newPieceLocation)
                {
                    if (newPieceLocation == 0xFF)
                    {
                        // Piece was captured
                        DoubleAnimation opacityAnimation = new DoubleAnimation();
                        Storyboard.SetTarget(opacityAnimation, oldPiece);
                        Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                        opacityAnimation.Duration = TimeSpan.FromMilliseconds(duration);
                        opacityAnimation.To       = 0;

                        animation.Children.Add(opacityAnimation);
                    }
                    else if (oldPieceLocation == 0xFF)
                    {
                        // Piece was created (game reset, undo)

                        DoubleAnimation opacityAnimation = new DoubleAnimation();
                        Storyboard.SetTarget(opacityAnimation, newPiece);
                        Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                        opacityAnimation.Duration = TimeSpan.FromMilliseconds(duration);
                        opacityAnimation.To       = 1;

                        Grid.SetRow(newPiece, SquareHelper.GetRow(newPieceLocation));
                        Grid.SetColumn(newPiece, SquareHelper.GetColumn(newPieceLocation));

                        animation.Children.Add(opacityAnimation);
                    }
                    else
                    {
                        if (oldPiece == newPiece)
                        {
                            // Piece just moved
                            Grid.SetRow(newPiece, SquareHelper.GetRow(newPieceLocation));
                            Grid.SetColumn(newPiece, SquareHelper.GetColumn(newPieceLocation));

                            DoubleAnimation yAnimation = new DoubleAnimation();
                            Storyboard.SetTarget(yAnimation, ((TransformGroup)newPiece.RenderTransform).Children[0]);
                            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("Y"));

                            yAnimation.Duration = TimeSpan.FromMilliseconds(duration);
                            yAnimation.From     = (SquareHelper.GetRow(oldPieceLocation) - SquareHelper.GetRow(newPieceLocation)) * 100;
                            yAnimation.To       = 0;

                            animation.Children.Add(yAnimation);

                            DoubleAnimation xAnimation = new DoubleAnimation();
                            Storyboard.SetTarget(xAnimation, ((TransformGroup)newPiece.RenderTransform).Children[0]);
                            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("X"));

                            xAnimation.Duration = TimeSpan.FromMilliseconds(duration);
                            xAnimation.From     = (SquareHelper.GetColumn(oldPieceLocation) - SquareHelper.GetColumn(newPieceLocation)) * 100;
                            xAnimation.To       = 0;

                            animation.Children.Add(xAnimation);
                        }
                        else
                        {
                            // Promotion
                            DoubleAnimation opacityAnimation = new DoubleAnimation();
                            Storyboard.SetTarget(opacityAnimation, newPiece);
                            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                            opacityAnimation.Duration = TimeSpan.FromMilliseconds(duration);
                            opacityAnimation.To       = 1;

                            Grid.SetRow(newPiece, SquareHelper.GetRow(newPieceLocation));
                            Grid.SetColumn(newPiece, SquareHelper.GetColumn(newPieceLocation));

                            animation.Children.Add(opacityAnimation);

                            opacityAnimation = new DoubleAnimation();
                            Storyboard.SetTarget(opacityAnimation, oldPiece);
                            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                            opacityAnimation.Duration = TimeSpan.FromMilliseconds(duration);
                            opacityAnimation.To       = 0;

                            animation.Children.Add(opacityAnimation);
                        }
                    }
                }
                else
                {
                    // TODO: what should be done here?
                }
            }

            // Workaround layout bug.
            this.LayoutRoot.Width--;
            this.LayoutRoot.Height++;

            animation.Completed += this.OnAnimationCompleted;
            animation.Begin();

            this.oldBoard     = new List <byte>(this.position.Board);
            this.oldPieceList = new List <byte>(this.position.PieceList);
            this.oldPieces    = new List <Piece>(this.pieces);
        }
Ejemplo n.º 2
0
        public void OnSquareClicked(int row, int column)
        {
            int square = SquareHelper.MakeSquare(row, column);

            List <ushort> validMoves = this.position.GenerateValidMoves();

            if (validMoves.Count == 0)
            {
                return;
            }

            if (this.previousSquareSelected == -1)
            {
                // Select a square
                if (this.position.Board[square] != Pieces.Empty)
                {
                    if ((this.position.Board[square] & 0x8) == position.ToMove)
                    {
                        this.squares[row][column].Highlight = true;
                        this.previousSquareSelected         = square;
//						this.StatusText = "Select square to move to";
                    }
                    else
                    {
//						this.StatusText = String.Format("You must select a {0} piece", position.ToMove == Pieces.White ? "White" : "Red");
                    }
                }
            }
            else if (this.previousSquareSelected == square)
            {
                // Deselect the square
                this.squares[row][column].Highlight = false;
                this.previousSquareSelected         = -1;
//				this.StatusText = "Select piece to move";
            }
            else
            {
                // Find a move
                int from = this.previousSquareSelected;
                int to   = square;

                ushort validMove = 0;
                foreach (ushort move in validMoves)
                {
                    if (MoveHelper.From(move) == from &&
                        MoveHelper.To(move) == to)
                    {
                        validMove = move;
                    }
                }

                if (validMove == 0)
                {
//					this.StatusText = "Invalid move!";
                    return;
                }

                this.squares[SquareHelper.GetRow(from)][SquareHelper.GetColumn(from)].Highlight = false;
                this.previousSquareSelected = -1;

                if (this.UserMove != null)
                {
                    this.UserMove(validMove);
                }
            }
        }