private void HandlePushEvent(SkeletonPoint pt, bool isPlayerOne) { if (selectingBall) { return; } SeesawObject pushedBall = null; PointCanvas ballHolder = null; var heldBalls = isPlayerOne ? game.PlayerOneHeldBalls : game.PlayerTwoHeldBalls; var ballHolders = isPlayerOne ? _playerOneBallHolders : _playerTwoBallHolders; foreach (var holder in ballHolders) { Rect rect = holder.GetBoundaryRect(); if (rect.Contains(pt.To2DPoint())) { if (holder.Children.Count > 0) { pushedBall = (SeesawObject)holder.Children[0]; ballHolder = holder; } } } if (pushedBall != null) { int index = heldBalls.IndexOf(pushedBall); HitBall(index, new Vector(0, 0), isPlayerOne); } }
private void HitBall(int index, Vector velocity, bool isPlayerOne) { var heldBalls = isPlayerOne ? game.PlayerOneHeldBalls : game.PlayerTwoHeldBalls; var ballHolders = isPlayerOne ? _playerOneBallHolders : _playerTwoBallHolders; var pushedBall = heldBalls[index]; // Check if any running animations to avoid conflicts if (this.gameActive && runningAnimations.Count == 0 && game.PushBall(pushedBall)) { var ballHolder = ballHolders[index]; uxMainCanvas.Children.Add(pushedBall); // TODO2: Trigger animation for ball and after animation is triggered PointAnimationUsingPath ballAnimation = new PointAnimationUsingPath(); PathGeometry animationPath = new PathGeometry(); PathFigure pFigure = new PathFigure(); pFigure.StartPoint = PointCanvas.GetTopLeft(ballHolder); //PathFigureCollection pfc = FindResource("RectanglePathFigureCollection") as PathFigureCollection; UIElement targetPanel = isPlayerOne ? ((UIElement)seesaw.leftBallPanel) : seesaw.rightBallPanel; Point endPoint = new Point( Canvas.GetLeft(seesaw) + Canvas.GetLeft(seesaw.uxBalanceCanvas) + Canvas.GetLeft(targetPanel), Canvas.GetTop(seesaw) + Canvas.GetTop(seesaw.uxBalanceCanvas) + Canvas.GetTop(targetPanel) ); pFigure.Segments.Add(ComputeCurve(pFigure.StartPoint, endPoint, velocity)); animationPath.Figures.Add(pFigure); // Freeze the PathGeometry for performance benefits. animationPath.Freeze(); ballAnimation.PathGeometry = animationPath; ballAnimation.BeginTime = TimeSpan.FromSeconds(0); ballAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.75)); ballAnimation.AutoReverse = false; Storyboard.SetTarget(ballAnimation, pushedBall); Storyboard.SetTargetProperty(ballAnimation, new PropertyPath("(TopLeft)")); Storyboard ballMove = new Storyboard(); ballMove.Children.Add(ballAnimation); runningAnimations.Add(ballMove); ballMove.Completed += delegate { selectingBall = false; uxMainCanvas.Children.Remove(pushedBall); runningAnimations.Remove(ballMove); (pushedBall as Bird).IsFlying = false; // Reset ball move animation pushedBall.BeginAnimation(SeesawObject.TopLeftProperty, null); game.AddBallToBalance(pushedBall, isPlayerOne); }; selectingBall = true; ballMove.Begin(); // TODO2: Trigger animation for ball and after animation is triggered } }
private const int HIT_ROUGHNESS = 10; // The amount of rough distance they can hit in between to make it easier to hit private void SetupBallHolders(int numHolders, bool isPlayerOne) { var ballHolders = isPlayerOne ? _playerOneBallHolders : _playerTwoBallHolders; var hitGesture = isPlayerOne ? _playerOneHitGesture : _playerTwoHitGesture; var rect = isPlayerOne ? uxPlayerOneRect : uxPlayerTwoRect; if (ballHolders == null || ballHolders.Length != numHolders) { // Remove all the ball holders if (ballHolders != null) { foreach (var holder in ballHolders) { holder.Children.Clear(); uxMainCanvas.Children.Remove(holder); } } ballHolders = new PointCanvas[numHolders]; // Points relative to the uxPersonCanvas space Point[] holderPositions = new Point[] { new Point(0.15, 0.3), new Point(0.35, 0.05), new Point(0.65, 0.05), new Point(0.85, 0.3) }; if (numHolders > holderPositions.Length) { throw new InvalidOperationException("You must define the locations of all holders"); } _hitZones.Clear(); for (int i = 0; i < numHolders; i++) { PointCanvas canvas = new PointCanvas(); canvas.Width = 80; canvas.Height = 80; uxMainCanvas.Children.Add(canvas); PointCanvas.SetTopLeft(canvas, new Point( holderPositions[i].X * rect.ActualWidth + Canvas.GetLeft(rect) - canvas.Width / 2, holderPositions[i].Y * rect.ActualHeight + Canvas.GetTop(rect) - canvas.Height / 2) ); ballHolders[i] = canvas; Rect boundaryRect = canvas.GetBoundaryRect(); boundaryRect.Inflate(HIT_ROUGHNESS, HIT_ROUGHNESS); _hitZones.Add(boundaryRect); } if (hitGesture != null) { hitGesture.HitRectangles.Clear(); hitGesture.HitRectangles.AddRange(_hitZones); } } // Reassign at the end if (isPlayerOne) { _playerOneBallHolders = ballHolders; } else { _playerTwoBallHolders = ballHolders; } }
private void SetupBallHolders(int numHolders, bool isPlayerOne) { var ballHolders = isPlayerOne ? _playerOneBallHolders : _playerTwoBallHolders; var hitGesture = isPlayerOne ? _playerOneHitGesture : _playerTwoHitGesture; var rect = isPlayerOne ? uxPlayerOneRect : uxPlayerTwoRect; if (ballHolders == null || ballHolders.Length != numHolders) { // Remove all the ball holders if (ballHolders != null) { foreach (var holder in ballHolders) { holder.Children.Clear(); uxMainCanvas.Children.Remove(holder); } } ballHolders = new PointCanvas[numHolders]; // Points relative to the uxPersonCanvas space Point[] holderPositions = new Point[] { new Point(0.15, 0.3), new Point(0.35, 0.05), new Point(0.65, 0.05), new Point(0.85, 0.3) }; if (numHolders > holderPositions.Length) throw new InvalidOperationException("You must define the locations of all holders"); _hitZones.Clear(); for (int i = 0; i < numHolders; i++) { PointCanvas canvas = new PointCanvas(); canvas.Width = 80; canvas.Height = 80; uxMainCanvas.Children.Add(canvas); PointCanvas.SetTopLeft(canvas, new Point( holderPositions[i].X * rect.ActualWidth + Canvas.GetLeft(rect) - canvas.Width / 2, holderPositions[i].Y * rect.ActualHeight + Canvas.GetTop(rect) - canvas.Height / 2) ); ballHolders[i] = canvas; Rect boundaryRect = canvas.GetBoundaryRect(); boundaryRect.Inflate(HIT_ROUGHNESS, HIT_ROUGHNESS); _hitZones.Add(boundaryRect); } if (hitGesture != null) { hitGesture.HitRectangles.Clear(); hitGesture.HitRectangles.AddRange(_hitZones); } } // Reassign at the end if (isPlayerOne) _playerOneBallHolders = ballHolders; else _playerTwoBallHolders = ballHolders; }