/// <summary> /// Queues a piecemove as a animation /// </summary> /// <param name="move">Piecemove to animate</param> /// <param name="team">Team of move</param> /// <param name="time">Elapsed time for animation</param> public void QueueAnimation(PieceMove move, sbyte team, float time) { if (move.IsTakingOutMarble) { Vector2 start = Vector2.Zero; if (!currentPlayer.IsHuman) { for (int j = starts[team].Length - 1; j >= 0; j--) { if (starts[team][j].HasMarble) { start = new Vector2(starts[team][j].Rect.X, starts[team][j].Rect.Y); starts[team][j].HasMarble = false; break; } } } Square homeSquare = new Square(team, 0); Vector2?homeSquareTarget = GetVector(homeSquare); Vector2?target = GetVector(move.To); if (homeSquareTarget.HasValue && target.HasValue) { MarbleView takeOut = new MarbleView(this, move.From, team, start); if (homeSquare.Equals(move.To)) { animations.Enqueue(new MarbleView[1] { takeOut }); takeOut.MoveTo(homeSquareTarget.Value, time); } else { MarbleView moveAnim = new MarbleView(this, move.From, team, homeSquareTarget.Value); animations.Enqueue(new MarbleView[2] { takeOut, moveAnim }); takeOut.MoveTo(homeSquareTarget.Value, time / 2); moveAnim.MoveTo(target.Value, time / 2); } } } else { Vector2?start = GetVector(move.From); Vector2?target = GetVector(move.To); if (start.HasValue && target.HasValue) { MarbleView anim = new MarbleView(this, move.From, team, start.Value); animations.Enqueue(new MarbleView[1] { anim }); anim.MoveTo(target.Value, time); } } }
/// <summary> /// Updates the drag logic /// </summary> /// <param name="mState">Current Mouse State</param> private void UpdateDrag(MouseState mState) { if (currentPlayer.IsHuman && currentRoll != null) { //Update drag if not null if (currentDrag != null) { currentDrag.DragTo(new Vector2(mState.X, mState.Y)); } //Check if drag is done if (mState.LeftButton == ButtonState.Released) { if (currentDrag != null) { Square target = currentDrag.StopDrag(); if (target != null) { PieceMove move = new PieceMove(currentDrag.From, target); if (board.IsPossibleTreeMove(currentRoll, move, currentPlayer.Team)) { currentMove.Add(move); if (PlayMarbleSounds) { PlayRandomPlaceSound(); } if (move.IsTakingOutMarble) { currentDrag.FromView.HasMarble = false; } } else { if (currentDrag.FromView != null) { currentDrag.FromView.HasMarble = true; } } } else { if (currentDrag.FromView != null) { currentDrag.FromView.HasMarble = true; } } currentDrag = null; } } } }
/// <summary> /// Sets the current start square as clicked /// </summary> /// <param name="view">Start Square</param> public void SetStart(SquareView view) { if (view.Team == currentPlayer.Team) { if (currentPlayer.IsHuman && currentRoll != null && IsAllowedToMoveMarbles()) { if (currentDrag == null) { Vector2 pos = view.GetPosition(); view.HasMarble = false; currentDrag = new MarbleView(this, view, currentPlayer.Team, pos); currentDrag.StartDrag(); } } } }
/// <summary> /// Callback when a animation is finished /// </summary> public void OnAnimationDone() { if (PlayMarbleSounds) { PlayRandomPlaceSound(); } //If there are any moves left if (animations.Count > 0) { //Get the move part animations MarbleView[] animationList = animations.Peek(); DoMoveInternals(QueuedMove[currentAnimIndex], QueuedMove.Team, currentAnimViewIndex, animationList.Length); //Increment the current part of the piece move animation we are on currentAnimViewIndex++; //If we are finished animating all parts of the piece move, move on the next piece move if (currentAnimViewIndex >= animationList.Length) { currentAnimViewIndex = 0; animations.Dequeue(); board.PerformMove(QueuedMove[currentAnimIndex], QueuedMove.Team); if (animations.Count == 0) { currentAnimIndex = 0; currentAnimation = null; OnMoveAnimationDone(); } else { currentAnimIndex++; currentAnimation = animations.Peek()[0]; } } else { currentAnimation = animationList[currentAnimViewIndex]; } } }
/// <summary> /// Sets the current start square as clicked /// </summary> /// <param name="view">Start Square</param> public void SetStart(BoardSquareView view) { if (currentPlayer.IsHuman && currentRoll != null && IsAllowedToMoveMarbles()) { Square sq = view.Square; if (!IsPartOfMove(sq)) { if (currentDrag == null && board.Get(sq) == currentPlayer.Team) { Vector2?pos = GetVector(sq); if (pos.HasValue) { currentDrag = new MarbleView(this, sq, currentPlayer.Team, pos.Value); currentDrag.StartDrag(); } } } } }
/// <summary> /// Starts the list of queued animations /// </summary> public void StartAnimations() { currentAnimIndex = 0; currentAnimation = animations.Peek()[0]; }