Advertisement
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 Random rand;
- private Texture2D texture;
- private double direction;
- private bool isVisible;
- private Vector2 position, resetPos, oldPos;
- private Rectangle size;
- public float speed;
- private ParticleEngine particleEngine;
- private ContentManager contentManager;
- private SpriteBatch spriteBatch;
- public bool isBallStopped;
- public Vector2 Origin;
- public float RotationAngle;
- private AIBat rightBat;
- private Bat leftBat;
- KeyboardState current, previous;
- #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/redBall");
- direction = 0;
- size = new Rectangle(0, 0, texture.Width, texture.Height);
- resetPos = new Vector2(ScreenSize.X / 2 + Origin.X, ScreenSize.Y / 2 + Origin.Y);
- position = resetPos;
- 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()
- // Everything to do with particles
- List<Texture2D> textures = new List<Texture2D>();
- textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/circle"));
- textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/star"));
- textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/diamond"));
- particleEngine = new ParticleEngine(textures, new Vector2());
- }
- public Ball(Bat myBat)
- {
- leftBat = myBat; // this assigns and instantiates the member bat
- // with myBat which was passed from the constructor
- }
- #endregion
- #region Methods
- /// <summary>
- /// Draws the ball on the screen
- /// </summary>
- public void Draw(SpriteBatch spriteBatch)
- {
- if (isVisible)
- {
- // Draws the rotaing ball
- spriteBatch.Draw(texture, position, size, Color.White,
- RotationAngle, Origin, 1.0f, SpriteEffects.None, 0);
- // 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();
- // 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;
- // Rotation for the ball
- RotationAngle += elapsed;
- float circle = MathHelper.Pi * 2;
- RotationAngle = RotationAngle % circle;
- // Updates particles for when the ball and bat make contact
- particleEngine.Update();
- }
- /// <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>
- /// Check for the ball to return normal speed after the Powerup has expired
- /// </summary>
- public void NormalSpeed()
- {
- speed += 15f;
- }
- /// <summary>
- /// Check for the ball to return normal speed after the Powerup has expired
- /// </summary>
- public float GetSpeed()
- {
- return speed;
- }
- ///<summary>
- ///Used for slowing down the ball when SlowMo is active
- ///</summary>
- public void SlowMoBall()
- {
- speed = 2f;
- }
- public void ReturnToNormalSpeed()
- {
- //if (position.X <= GameplayScreen.Instance.screenWidth / 2)
- //{
- // speed = 15f;
- //}
- //else
- //{
- // speed = -15f;
- //}
- speed = 15f;
- }
- /// <summary>
- /// Checks for the current direction of the ball
- /// </summary>
- public double GetDirection()
- {
- return direction;
- }
- /// <summary>
- /// Checks for the current position of the ball
- /// </summary>
- public Vector2 GetPosition()
- {
- return position;
- }
- /// <summary>
- /// Checks for the current size of the ball (for the powerups)
- /// </summary>
- public Rectangle GetSize()
- {
- return size;
- }
- /// <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 to see if ball went out of bounds, and triggers warp sfx. Used in GameplayScreen.
- /// </summary>
- public void OutOfBounds()
- {
- AudioManager.Instance.PlaySoundEffect("warp");
- }
- /// <summary>
- /// Speed for the ball when Speedball powerup is activated
- /// </summary>
- public void PowerupSpeed()
- {
- speed += 20.0f;
- }
- /// <summary>
- /// Check for where to reset the ball after each point is scored
- /// </summary>
- public void Reset(bool left)
- {
- if (left)
- {
- direction = 0;
- }
- else
- {
- direction = Math.PI;
- }
- position = resetPos; // Resets the ball to the center of the screen
- isVisible = true;
- speed = 15f; // Returns the ball back to the default speed, in case the speedBall was active
- if (rand.Next(2) == 0)
- {
- direction += MathHelper.ToRadians(rand.Next(30));
- }
- else
- {
- direction -= MathHelper.ToRadians(rand.Next(30));
- }
- }
- /// <summary>
- /// Shrinks the ball when the ShrinkBall powerup is activated
- /// </summary>
- public void ShrinkBall()
- {
- size = new Rectangle(0, 0, texture.Width / 2, texture.Height / 2);
- }
- /// <summary>
- /// Stops the ball each time it is reset. Ex: Between points / rounds
- /// </summary>
- public void Stop()
- {
- isVisible = true;
- speed = 0;
- isBallStopped = true;
- }
- /// <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;
- }
- ///<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);
- AudioManager.Instance.PlaySoundEffect("hit");
- }
- }
- ///<summary>
- /// Sets off particles when the ball and right bat collide
- ///</summary>
- public void RightBatParticles()
- {
- // 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(rightBat.Position.X +20, rightBat.Position.Y +40);
- AudioManager.Instance.PlaySoundEffect("hit");
- }
- }
- #endregion
- }
- }
- //////////////////////////////////////////////////////////////////////////
- namespace Pong
- {
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- public class Bat
- {
- public Vector2 Position;
- public float moveSpeed, elapsedTime;
- public Rectangle size;
- private int points, yHeight;
- private Texture2D leftBat;
- public float turbo, recharge, interval;
- public bool isTurbo;
- private KeyboardState current, previous;
- /// <summary>
- /// Constructor for the bat
- /// </summary>
- public Bat(ContentManager contentManager, Vector2 screenSize, bool side)
- {
- moveSpeed = 7f;
- turbo = 15f;
- recharge = 100f;
- points = 0;
- interval = 5f;
- leftBat = contentManager.Load<Texture2D>(@"gfx/bats/batGrey");
- size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
- // True means left bat, false means right bat.
- if (side) Position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
- else Position = new Vector2(screenSize.X - 30, screenSize.Y / 2 - size.Height / 2);
- yHeight = (int)screenSize.Y;
- }
- public void IncreaseSpeed()
- {
- moveSpeed += .5f;
- }
- /// <summary>
- /// The speed of the bat when Turbo is activated
- /// </summary>
- public void Turbo()
- {
- moveSpeed += 8.0f;
- }
- /// <summary>
- /// Returns the speed of the bat back to normal after Turbo is deactivated
- /// </summary>
- public void DisableTurbo()
- {
- moveSpeed = 7.0f;
- isTurbo = false;
- }
- /// <summary>
- /// Returns the bat to the nrmal size after the Grow/Shrink powerup has expired
- /// </summary>
- public void NormalSize()
- {
- size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
- }
- /// <summary>
- /// Checks for the size of the bat
- /// </summary>
- public Rectangle GetSize()
- {
- return size;
- }
- /// <summary>
- /// Used for the Shrinkbat powerup
- /// </summary>
- public void ShrinkBat()
- {
- // 1/2 the size of the bat collision
- size = new Rectangle(0, 0, leftBat.Width / 2, leftBat.Height / 2);
- }
- /// <summary>
- /// Adds point to the player or the AI after scoring. Currently Disabled.
- /// </summary>
- public void IncrementPoints()
- {
- points++;
- }
- /// <summary>
- /// Checks for the number of points at the moment
- /// </summary>
- public int GetPoints()
- {
- return points;
- }
- /// <summary>
- /// Sets thedefault starting position for the bats
- /// </summary>
- /// <param name="position"></param>
- public void SetPosition(Vector2 position)
- {
- if (position.Y < 0)
- {
- position.Y = 0;
- }
- if (position.Y > yHeight - size.Height)
- {
- position.Y = yHeight - size.Height;
- }
- this.Position = position;
- }
- /// <summary>
- /// Checks for the current position of the bat
- /// </summary>
- public Vector2 GetPosition()
- {
- return Position;
- }
- /// <summary>
- /// Controls the bat moving up the screen
- /// </summary>
- public void MoveUp()
- {
- SetPosition(Position + new Vector2(0, -moveSpeed * elapsedTime));
- }
- /// <summary>
- /// Controls the bat moving down the screen
- /// </summary>
- public void MoveDown()
- {
- SetPosition(Position + new Vector2(0, moveSpeed * elapsedTime));
- }
- /// <summary>
- /// Updates the position of the AI bat, in order to track the ball
- /// </summary>
- public virtual void UpdatePosition(Ball ball, GameTime gameTime)
- {
- size.X = (int)Position.X;
- size.Y = (int)Position.Y;
- elapsedTime = 50.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
- // Just here for debugging. Hitting Z WORKS FINE and slows the bats down
- previous = current;
- current = Keyboard.GetState();
- if (current.IsKeyDown(Keys.Z))
- {
- elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
- }
- }
- /// <summary>
- /// Resets the bat to the center location after a new game starts
- /// </summary>
- public void ResetPosition()
- {
- SetPosition(new Vector2(GetPosition().X, yHeight / 2 - size.Height));
- }
- /// <summary>
- /// Returns the ball to normal speed
- /// </summary>
- public void ReturnToNormalSpeed()
- {
- moveSpeed = 7f;
- }
- /// <summary>
- /// Used for the Growbat powerup
- /// </summary>
- public void GrowBat()
- {
- // Doubles the size of the bat collision
- size = new Rectangle(0, 0, leftBat.Width * 2, leftBat.Height * 2);
- }
- /// <summary>
- /// Draws the bats
- /// </summary>
- public virtual void Draw(SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(leftBat, size, Color.White);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- namespace Pong
- {
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- public class AIBat : Bat
- {
- public Texture2D rightBat;
- /// <summary>
- /// Constructor for the AIBat
- /// </summary>
- public AIBat(ContentManager contentManager, Vector2 screenSize, bool side) : base(contentManager, screenSize, side)
- {
- rightBat = contentManager.Load<Texture2D>(@"gfx/bats/batBlack");
- }
- /// <summary>
- /// Updates the position of the ball in the GameplayScreen
- /// </summary>
- public override void UpdatePosition(Ball ball, GameTime gameTime)
- {
- if (ball.GetDirection() > 1.5 * Math.PI || ball.GetDirection() < 0.5 * Math.PI)
- {
- if (ball.GetPosition().Y - 5 > GetPosition().Y + GetSize().Height / 2)
- {
- MoveDown();
- }
- else if (ball.GetPosition().Y == GetPosition().Y + GetSize().Height / 2)
- {
- }
- else if (ball.GetPosition().Y + 5 < GetPosition().Y + GetSize().Height / 2)
- {
- MoveUp();
- }
- }
- base.UpdatePosition(ball, gameTime);
- }
- /// <summary>
- /// Draws the AIBat
- /// </summary>
- public override void Draw(SpriteBatch batch)
- {
- batch.Draw(rightBat, size, Color.White);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement