/// <summary> /// draws bubbles on the colorFrame /// </summary> /// <param name="bubble"></param> /// <param name="drawingContext"></param> private static void DrawBubble(Bubble bubble, DrawingContext drawingContext) { Color white = new Color(); white = Color.FromArgb(100, 255, 255, 255); Brush brush = new RadialGradientBrush(white, bubble.BubbleColor); Pen pen = new Pen(new SolidColorBrush(Color.FromArgb(150, 0, 0, 0)), 5); drawingContext.DrawEllipse(brush, pen, new Point(bubble.BubblePosition.X, bubble.BubblePosition.Y), bubble.BubbleSize, bubble.BubbleSize); }
/// <summary> /// checks if gesture matches with bubble colors /// </summary> /// <param name="handState"></param> /// <param name="bubble"></param> /// <returns></returns> private bool gestureMatches(HandState handState, Bubble bubble) { switch (handState) { case HandState.Open: return(bubble.BubbleColor.G.Equals(bubbleColorGreen.G)); case HandState.Lasso: return(bubble.BubbleColor.B.Equals(bubbleColorBlue.B)); case HandState.Closed: return(bubble.BubbleColor.R.Equals(bubbleColorRed.R)); default: return(false); } }
/// <summary> /// spwan bubbles with random properties at random locations /// </summary> private void spawnBubbles() { try { DateTime now = DateTime.Now; lock (Bubbles) { if ((lastBubbleSpawnedAt == null || now - lastBubbleSpawnedAt >= new TimeSpan(0, 0, 0, 0, bubbleSpawnRate))) { Bubble bubble = new Bubble(randomColor(), randomPosition(), rnd.Next(bubbleMinRadius, bubbleMaxRadius + 1), rnd.Next(bubbleMinTime, bubbleMaxTime + 1)); this.Bubbles.Add(bubble); lastBubbleSpawnedAt = now; } } } catch (Exception ex) { throw ex; } }
/// <summary> /// detects collisions between bubbles and hands and adds score to the player /// </summary> private bool detectCollision(Player player, Bubble bubble) { Point leftHand = this.window.getPoint(JointType.HandLeft, player.body); Point rightHand = this.window.getPoint(JointType.HandRight, player.body); if ((Point.Subtract(leftHand, bubble.BubblePosition).Length < window.HandSize + bubble.BubbleSize && gestureMatches(player.body.HandLeftState, bubble)) || (Point.Subtract(rightHand, bubble.BubblePosition).Length < window.HandSize + bubble.BubbleSize && gestureMatches(player.body.HandRightState, bubble))) { double bubbleRadiusRange = (this.bubbleMaxRadius - this.bubbleMinRadius); double bubbleTimeRange = (this.bubbleMaxTime - this.bubbleMinTime); player.score += (int)(10 + 45 * (1.0 / bubbleRadiusRange * bubble.BubbleSize - (this.bubbleMinRadius / bubbleRadiusRange)) + 45 * (1.0 / bubbleTimeRange * bubble.TimeToDisappear - (this.bubbleMinTime / bubbleTimeRange))); Console.WriteLine(player.body.TrackingId + ": Score " + player.score); this.popSound.Play(); return(true); } return(false); }
/// <summary> /// checks if the bubbles should disappear /// </summary> /// <param name="bubble"></param> /// <returns></returns> private static bool shouldDespawn(Bubble bubble) { return(DateTime.Now - bubble.Created > new TimeSpan(0, 0, 0, 0, bubble.TimeToDisappear)); }