Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Pong
- {
- #region Using Statements
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- #endregion
- public class Ball
- {
- #region Fields
- private readonly Random rand;
- private readonly Texture2D texture;
- private readonly SoundEffect warp;
- private double direction;
- private bool isVisible;
- private Vector2 position;
- private Vector2 resetPos;
- private Rectangle size;
- public float speed;
- private bool isResetting;
- private bool collided;
- private Vector2 oldPos;
- private ParticleEngine particleEngine;
- private ContentManager contentManager;
- private SpriteBatch spriteBatch;
- private bool hasHitLeftBat;
- private bool hasHitRightBat;
- public Vector2 Origin;
- public float RotationAngle;
- private AIBat rightBat;
- private Bat leftBat;
- KeyboardState current, previous;
- /// <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;
- size = new Rectangle(0, 0, texture.Width, texture.Height);
- resetPos = new Vector2(ScreenSize.X / 2, ScreenSize.Y / 2);
- position = resetPos;
- rand = new Random();
- isVisible = true;
- hasHitLeftBat = false;
- 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()
- }
- public Ball(Bat myBat)
- {
- leftBat = myBat; // this assigns and instantiates the member bat
- // with myBat which was passed from the constructor
- }
- /// <summary>
- /// Draws the ball on the screen
- /// </summary>
- public void Draw(SpriteBatch spriteBatch)
- {
- if (isVisible)
- {
- // ideally, we'd just draw at position, but the collision code is a offset,
- // so I'm offsetting by the origin
- //spriteBatch.Draw(texture, position + Origin, null, Color.White,
- // RotationAngle, Origin, 1.0f, SpriteEffects.None, 0);
- spriteBatch.Draw(texture, position, null, Color.White,
- RotationAngle, Origin, 1.0f, SpriteEffects.None, 0);
- //spriteBatch.Draw(texture, size, Color.White);
- // Draws sprites for particles when contact is made
- particleEngine.Draw(spriteBatch);
- }
- }
- /// <summary>
- /// Updates position of the ball. Used in Update() for GameplayScreen.
- /// </summary>
- public void UpdatePosition(GameTime gameTime)
- {
- size.X = (int)position.X;
- size.Y = (int)position.Y;
- oldPos.X = position.X;
- oldPos.Y = position.Y;
- previous = current;
- current = Keyboard.GetState();
- if (current.IsKeyDown(Keys.Z))
- {
- SlowMoBall();
- }
- position.X += speed * ((float)Math.Cos(direction));
- position.Y += speed * ((float)Math.Sin(direction));
- bool collided = CheckWallHit();
- // Updates particles for when the ball and bat make contact
- particleEngine.Update();
- // Stops the issue where ball was oscillating on the ceiling or floor
- if (collided)
- {
- position.X = oldPos.X + speed * (float)Math.Cos(direction);
- position.Y = oldPos.Y + speed * (float)Math.Sin(direction);
- }
- // The time since Update was called last.
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- 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)
- {
- hasHitRightBat = true;
- RightBatParticles(hasHitRightBat);
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(200);
- break;
- case 2:
- direction = MathHelper.ToRadians(195);
- break;
- case 3:
- direction = MathHelper.ToRadians(180);
- break;
- case 4:
- direction = MathHelper.ToRadians(180);
- break;
- case 5:
- direction = MathHelper.ToRadians(165);
- break;
- }
- }
- else
- {
- hasHitLeftBat = true;
- LeftBatParticles(hasHitLeftBat);
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(310);
- break;
- case 2:
- direction = MathHelper.ToRadians(345);
- break;
- case 3:
- direction = MathHelper.ToRadians(0);
- break;
- case 4:
- direction = MathHelper.ToRadians(15);
- break;
- case 5:
- direction = MathHelper.ToRadians(50);
- break;
- }
- }
- if (rand.Next(2) == 0)
- {
- direction += MathHelper.ToRadians(rand.Next(3));
- }
- else
- {
- direction -= MathHelper.ToRadians(rand.Next(3));
- }
- // Plays the sound effect for when the ball and bat collide
- AudioManager.Instance.PlaySoundEffect("hit");
- }
- /// <summary>
- /// Grows the size of the ball when the GrowBall powerup is used.
- /// </summary>
- public void GrowBall()
- {
- size = new Rectangle(0, 0, texture.Width * 2, texture.Height * 2);
- }
- /// <summary>
- /// Check for the ball to return normal size after the Powerup has expired
- /// </summary>
- public void NormalBallSize()
- {
- size = new Rectangle(0, 0, texture.Width, texture.Height);
- }
- /// <summary>
- /// Checks for collision with the ceiling or floor. 2*Math.pi = 360 degrees
- /// </summary>
- private bool CheckWallHit()
- {
- while (direction > 2 * Math.PI)
- {
- direction -= 2 * Math.PI;
- return true;
- }
- while (direction < 0)
- {
- direction += 2 * Math.PI;
- return true;
- }
- if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
- {
- direction = 2 * Math.PI - direction;
- return true;
- }
- return true;
- }
- // Sets off particles when the ball and right bat collide
- public void LeftBatParticles(bool hasHitLeftBat)
- {
- if (hasHitLeftBat == true)
- {
- particleEngine.EmitterLocation = new Vector2(leftBat.Position.X +30 , leftBat.Position.Y +40);
- }
- }
- // Sets off particles when the ball and left bat collide
- public void RightBatParticles(bool hasHitRightBat)
- {
- if (hasHitRightBat == true)
- {
- particleEngine.EmitterLocation = new Vector2(rightBat.Position.X +20, rightBat.Position.Y +40);
- }
- }
- }
- }
RAW Paste Data