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>
- /// 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement