Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ball
- {
- private Rectangle ballRect;
- private AIBat rightBat;
- private Bat leftBat;
- private Rectangle rectangle3;
- /// <summary>
- /// Constructor for the ball
- /// </summary>
- public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
- {
- speed = 15f;
- texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
- direction = 0;
- ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
- resetBallPos = new Vector2(ScreenSize.X / 2 + origin.X, ScreenSize.Y / 2 + origin.Y);
- ballPosition = resetBallPos;
- rand = new Random();
- isVisible = true;
- origin = new Vector2(texture.Width / 2 , texture.Height / 2);
- leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
- rightBat = aiBat;// Creates a new instance of leftBat so that I can access Position.X/Y for RightBatPatcicles()
- Rectangle rectangle3 = new Rectangle();
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- if (isVisible)
- {
- // Draws the rotaing ball
- spriteBatch.Draw(texture, ballPosition, ballRect, Color.White,
- RotationAngle, origin, 1.0f, SpriteEffects.None, 0);
- // Draws the short particle
- // shortParticle.Draw(spriteBatch);
- spriteBatch.Draw(texture, rectangle3, Color.Yellow);
- }
- }
- /// <summary>
- /// Updates position of the ball. Used in Update() for GameplayScreen.
- /// </summary>
- public void UpdatePosition(GameTime gameTime)
- {
- ballRect.X = (int)ballPosition.X;
- ballRect.Y = (int)ballPosition.Y;
- oldBallPos.X = ballPosition.X;
- oldBallPos.Y = ballPosition.Y;
- ballPosition.X += speed * ((float)Math.Cos(direction));
- ballPosition.Y += speed * ((float)Math.Sin(direction));
- bool collided = CheckWallHit();
- // Stops the issue where ball was oscillating on the ceiling or floor
- if (collided)
- {
- ballPosition.X = oldBallPos.X + speed * (float)Math.Cos(direction);
- ballPosition.Y = oldBallPos.Y + speed * (float)Math.Sin(direction);
- }
- // When the ball and bat collide, draw the rectangle where they intersect
- BatCollisionRect();
- // The time since Update was called last.
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- // Rotation for the ball
- RotationAngle += elapsed;
- float circle = MathHelper.Pi * 2;
- RotationAngle = RotationAngle % circle;
- }
- /// <summary>
- /// Checks for the collision between the bat and the ball. Sends ball in the appropriate
- /// direction
- /// </summary>
- public void BatHit(int block)
- {
- if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
- {
- RightBatParticles();
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(220);
- break;
- case 2:
- direction = MathHelper.ToRadians(215);
- break;
- case 3:
- direction = MathHelper.ToRadians(200);
- break;
- case 4:
- direction = MathHelper.ToRadians(195);
- break;
- case 5:
- direction = MathHelper.ToRadians(180);
- break;
- case 6:
- direction = MathHelper.ToRadians(180);
- break;
- case 7:
- direction = MathHelper.ToRadians(165);
- break;
- case 8:
- direction = MathHelper.ToRadians(130);
- break;
- case 9:
- direction = MathHelper.ToRadians(115);
- break;
- case 10:
- direction = MathHelper.ToRadians(110);
- break;
- }
- }
- else
- {
- LeftBatParticles();
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(290);
- break;
- case 2:
- direction = MathHelper.ToRadians(295);
- break;
- case 3:
- direction = MathHelper.ToRadians(310);
- break;
- case 4:
- direction = MathHelper.ToRadians(345);
- break;
- case 5:
- direction = MathHelper.ToRadians(0);
- break;
- case 6:
- direction = MathHelper.ToRadians(0);
- break;
- case 7:
- direction = MathHelper.ToRadians(15);
- break;
- case 8:
- direction = MathHelper.ToRadians(50);
- break;
- case 9:
- direction = MathHelper.ToRadians(65);
- break;
- case 10:
- direction = MathHelper.ToRadians(70);
- break;
- }
- }
- if (rand.Next(2) == 0)
- {
- direction += MathHelper.ToRadians(rand.Next(3));
- }
- else
- {
- direction -= MathHelper.ToRadians(rand.Next(3));
- }
- }
- /// <summary>
- /// Used to determine the location where the particles will initialize when the ball and bat collide
- /// </summary>
- public void BatCollisionRect()
- {
- if (ballRect.Intersects(leftBat.batRect))
- {
- rectangle3 = Rectangle.Intersect(ballRect, leftBat.batRect);
- }
- }
- ///<summary>
- /// Sets off particles when the ball and left bat collide
- ///</summary>
- public void LeftBatParticles()
- {
- // If the ball is stopped (what happens when the ball resets), then particles do not go off when the bat hit it.
- // Used to prevent a bug when the particles and sound would go off while the ball is resetting.
- if (speed > 0)
- {
- // particleEngine.EmitterLocation = new Vector2(leftBat.Position.X + 30, leftBat.Position.Y + 40);
- // shortParticle.EmitterLocation = new Vector2(leftBat.Position.X + 30, leftBat.Position.Y + 40);
- AudioManager.Instance.PlaySoundEffect("hit2");
- }
- }
- ///<summary>
- /// Sets off particles when the ball and right bat collide
- ///</summary>
- public void RightBatParticles()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement