Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Pong
- {
- public class Ball : IGameEntity
- {
- #region Fields
- private Random rand; // Random var
- private Texture2D texture; // Texture for the ball
- private double direction; // Directon the ball is traveling in
- private bool isVisible;
- private bool hasHitLeftBat; // Checked to see if the ball and bat have just collided
- private bool hasHitRightBat; // Checked to see if the ball and bat have just collided
- private Vector2 ballPosition, resetBallPos, oldBallPos;
- private Rectangle ballRect;
- public float Speed;
- private SpriteBatch spriteBatch; // Spritebatch
- private bool isBallStopped;
- private Vector2 origin; // Locate the mid-point of the ball
- public float RotationAngle;
- private AIBat rightBat; // Player's Bad
- private Bat leftBat; // AI Bat
- private float ballRelativePos;
- private Rectangle rectangle3; // Used to draw the collison rectangle
- private Texture2D blank; // Texture to be drawn on the collision rectangle
- GameplayScreen gameplayScreen; // Creates an instance of the GameplayScreen
- Game1 gameInstance; // Creates an instance of the Game1 class
- int selectedStage; // Pass this into GameplayScreen for selecting easy, medium, or hard
- // Mercury Particle Engine
- private Renderer myRenderer; // Renderer that draws particles to screen
- private ParticleEffect magicTrail; // Particle effect object to store the info about particle
- private GraphicsDeviceManager graphics;
- #endregion
- #region Constructors and Destructors
- /// <summary>
- /// Constructor for the ball
- /// </summary>
- public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
- {
- Speed = 15f;
- texture = contentManager.Load<Texture2D>(@"gfx/balls/ball1");
- direction = 0;
- origin = new Vector2(texture.Width / 2, texture.Height / 2);
- 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;
- 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 can access Position.X/Y for RightBatPatcicles()
- gameplayScreen = new GameplayScreen(null, selectedStage);
- gameInstance = new Game1();
- Rectangle rectangle3 = new Rectangle();
- blank = contentManager.Load<Texture2D>(@"gfx/blank");
- // Merury particle effects
- spriteBatch = new SpriteBatch(gameInstance.GraphicsDevice);
- myRenderer = new SpriteBatchRenderer // Create new renderer and set its graphics devide to "this" device
- {
- GraphicsDeviceService = (IGraphicsDeviceService)gameInstance.Services.GetService(typeof(IGraphicsDeviceService))
- };
- magicTrail = new ParticleEffect(); // The actual particle effect
- // Content for Mercury
- magicTrail = contentManager.Load<ParticleEffect>("BasicExplosion");
- magicTrail.LoadContent(contentManager);
- magicTrail.Initialise();
- myRenderer.LoadContent(contentManager);
- }
- // this assigns and instantiates the member bat with myBat which was passed from the constructor
- public Ball(Bat myBat)
- {
- leftBat = myBat;
- }
- #endregion
- #region Methods
- /// <summary>
- /// Draws the ball on the screen
- /// </summary>
- public void Draw(SpriteBatch spriteBatch)
- {
- if (isVisible)
- {
- // Draws the collision rectangle
- spriteBatch.Draw(blank, rectangle3, Color.LightCoral);
- spriteBatch.Draw(texture, ballPosition, null, Color.White,
- RotationAngle, origin, 1f, SpriteEffects.None, 0);
- myRenderer.RenderEffect(magicTrail); // Draws Mercury. Must remain outside of SpriteBatch
- }
- }
- /// <summary>
- /// Updates position of the ball. Used in Update() for GameplayScreen.
- /// </summary>
- public void UpdatePosition(GameTime gameTime)
- {
- .......
- // 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;
- gameInstance.update();
- // Draws Mercury Particle Engine
- magicTrail.Trigger(new Vector2(leftBat.BatPosition.X, leftBat.BatPosition.Y));
- }
- #region BatHit
- /// Checks for the collision between the bat and the ball. Sends ball in the appropriate
- /// direction
- /// </summary>
- public void BatHit(int block, ParticleEmitterService emitter) // Put this here to save me adding Game to this class too :S
- {
- magicTrail.Trigger(new Vector2(500, 500)); // Draws Mercury Particle Engine
- if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
- {
- RightBatParticles();
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(220);
- break;
- ...........
- }
- }
- else
- {
- LeftBatParticles();
- switch (block)
- {
- case 1:
- direction = MathHelper.ToRadians(290);
- break;
- .............
- }
- }
- }
RAW Paste Data