public void fireBallModelAdded(BallModel ball)
 {
     foreach (BallModelsListener listener in this._listeners)
     {
         listener.handleBallAdded(ball);
     }
 }
 void BallModelsListener.handleBallRemoved(BallModel ball)
 {
     int id = ball.ID;
     Ellipse ellipse = balls[id];
     balls.Remove(id);
     MainCanvas.Children.Remove(ellipse);
 }
 public BallModel createBall(double x, double y, double r, double angle, double velocity)
 {
     int id = this.generateNextId();
     BallModel ball = new BallModel(id, x, y, r, angle, velocity);
     ball.addBallModelListener(this._window);
     this._balls.Add(id, ball);
     this.fireBallModelAdded(ball);
     return ball;
 }
 void BallModelsListener.handleBallAdded(BallModel ball)
 {
     int id = ball.ID;
     Ellipse ellipse = generateEllipse(ball);
     balls.Add(id, ellipse);
     if (!MainCanvas.Children.Contains(ellipse))
     {
         MainCanvas.Children.Add(ellipse);
     }
 }
 void BallModelListener.handleBallModelChanged(BallModel ball)
 {
     int id = ball.ID;
     Ellipse ellipse = balls[id];
     if (ellipse != null)
     {
         ellipse.Height = ball.R * 2;
         ellipse.Width = ball.R * 2;
         Canvas.SetLeft(ellipse, ball.X);
         Canvas.SetTop(ellipse, ball.Y);
         Canvas.SetZIndex(ellipse, 100);
     }
 }
 private static Ellipse generateEllipse(BallModel ball)
 {
     var circle = new Ellipse();
     circle.Width = ball.R * 2;
     circle.Height = ball.R * 2;
     circle.Stroke = new SolidColorBrush(Colors.Black);
     circle.StrokeThickness = 1;
     circle.Fill = ball.TargetColor;
     return circle;
 }
        /**
         * Determines whether the ball is in bounds of canvas
        */
        private Boolean isInBounds(BallModel ball)
        {
            double width = this._window.Width;
            double height = this._window.Height;

            if (ball.Velocity > 0)
            {
                double x = ball.X;
                double y = ball.Y;
                if (x < 0 || x > width)
                {
                    return false;
                }
                if (y < 0 || y > height)
                {
                    return false;
                }
            }
            return true;
        }
        private static Boolean ballIntersectsTarget(BallModel ball, Target target)
        {
            double ballX = ball.X;
            double ballY = ball.Y;
            double ballR = ball.R;
            double targetX = target.getXPosition();
            double targetY = target.getYPosition();
            double targetR = target.getTargetRadius();
            double distance = Math.Sqrt(Math.Pow(ballX - targetX,2) + Math.Pow(ballY - targetY,2));

            return distance < targetR+ballR*0.3;
        }
 public Boolean removeBall(BallModel ball)
 {
     int id = ball.ID;
     return this.removeBall(id);
 }
        public override void processSkeletonFrame(SkeletonData skeleton, Dictionary<int, Target> targets)
        {
            // x,y launch angle,
            if (areWristsTogether(skeleton) &&
                isCloseToBody(skeleton))            // charging
            {
                // here is where we should put a non-moving ball at X,Y
                double[] ballCoords = getJointForBall(skeleton);

                double ballX = ballCoords[0];
                double ballY = ballCoords[1];
                double ballR = BallModel.MIN_R;
                if (curBall == null)
                {

                    ballX -= ballR;
                    ballY -= ballR;
                    curBall = _balls.createBall(ballX, ballY, ballR);
                }
                else
                {
                    ballR = computeGrownRadius(curBall.R);
                    ballX -= ballR;
                    ballY -= ballR;
                    curBall.setPosition(ballX, ballY);
                    curBall.R = ballR;
                }
            }
            else if (areArmsStraight(skeleton) &&
                areWristsTogether(skeleton) &&
                !isCloseToBody(skeleton))           // launching
            {
                // get angle

                double angle = computeLaunchAngle(skeleton);
                double velocity = BallModel.MAX_V;
                if (curBall != null)
                {
                    curBall.Velocity = velocity;
                    curBall.Angle = angle;
                    curBall = null;
                    _pastBallLocations.Clear();
                }
            }
            else if (areWristsTogether(skeleton))   // moving around
            {
                if (curBall != null)
                {
                    // here is where we should put a non-moving ball at X,Y
                    double[] ballCoords = getJointForBall(skeleton);
                    double ballR = curBall.R;
                    double ballX = ballCoords[0] - ballR;
                    double ballY = ballCoords[1] - ballR;

                    curBall.setPosition(ballX, ballY);
                }
            }
            else                                    // no ball / cancel
            {
                // if there is a curball cancel it
                if (curBall != null)
                {
                    _balls.removeBall(curBall);
                    curBall = null;
                    _pastBallLocations.Clear();
                }
                // if no ball, keep having no ball
            }
            if (curBall != null)
            {
                this.addPastBallLocation(curBall.X, curBall.Y);
            }

            _balls.animateBalls();
            _balls.removeOutOfBoundsBalls();
            _balls.removeIntersectingBalls(this.validTargets, new ProcessTargetIntersectedDelegate(handleTargetIntersected));
        }