Example #1
0
        public void Update()
        {
            Random random = new Random();

            isInGoal = false;//needs to be cleared if ball is updating before scoreboard

            if (!isHit)
            {
                position += rate;                                                         //move

                if (position.Y >= graphics.Viewport.Height || Math.Abs(position.Y) < 20f) //on hitting ceiling
                {
                    rate.Y *= -1;                                                         //invert slope
                }

                if (position.X >= graphics.Viewport.Width || position.X <= 0)//offscreen
                {
                    isInGoal = true;
                }
            }
            else//hit
            {
                wasHit   = true;//update for AI
                wasHitby = hitBy;

                Console.WriteLine("hit: " + rate.Y);
                rate.X *= -1;

                if (Math.Abs((hitBy.position.Y - position.Y) - 45) > 46)//hit in top third
                {
                    rate.Y -= stdRate + (int)((hitBy.position.Y - position.Y - 45) * 0.1) + random.Next(5);

                    if (hitBy.position.X > position.X) //is left paddle
                    {
                        position.X -= 10;              //shift to avoid being stuck in hitbox
                    }
                    else//is right paddle
                    {
                        position.X += 10;//shift to avoid being stuck in hitbox
                    }
                }

                else if (Math.Abs(hitBy.position.Y - position.Y - 45) < 46 && Math.Abs(hitBy.position.Y - position.Y - 45) > 23)//hit in middle TODO fix where middle is
                {
                    rate.Y -= stdRate + (int)((hitBy.position.Y - position.Y - 45) * 0.1) + random.Next(5);

                    if (hitBy.position.X > position.X) //is left paddle
                    {
                        position.X -= 10;              //shift to avoid being stuck in hitbox
                    }
                    else//is right paddle
                    {
                        position.X += 10;//shift to avoid being stuck in hitbox
                    }
                }

                else if (Math.Abs((hitBy.position.Y - position.Y) - 45) < 23)//hit in bottom
                {
                    rate.Y += stdRate + (int)((hitBy.position.Y - position.Y - 45) * 0.1) + random.Next(5);

                    if (hitBy.position.X > position.X) //is left paddle
                    {
                        position.X -= 10;              //shift to avoid being stuck in hitbox
                    }
                    else//is right paddle
                    {
                        position.X += 10;//shift to avoid being stuck in hitbox
                    }
                }

                isHit = false;//reset
            }

            //hitBy = null;
            hitBox.X = (int)position.X;
            hitBox.Y = (int)position.Y;
        }