Guest User

Untitled

a guest
Aug 14th, 2012
30
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. namespace Pong
  3. {
  4.     public class Ball : IGameEntity
  5.     {
  6.         #region Fields
  7.  
  8.         private Random rand;                // Random var
  9.         private Texture2D texture;          // Texture for the ball
  10.         private double direction;           // Directon the ball is traveling in               
  11.         private bool isVisible;
  12.         private bool hasHitLeftBat;         // Checked to see if the ball and bat have just collided
  13.         private bool hasHitRightBat;        // Checked to see if the ball and bat have just collided
  14.         private Vector2 ballPosition, resetBallPos, oldBallPos;
  15.         private Rectangle ballRect;
  16.         public float Speed;
  17.         private SpriteBatch spriteBatch;    // Spritebatch
  18.         private bool isBallStopped;
  19.         private Vector2 origin;             // Locate the mid-point of the ball
  20.         public float RotationAngle;
  21.         private AIBat rightBat;             // Player's Bad
  22.         private Bat leftBat;                // AI Bat
  23.         private float ballRelativePos;
  24.         private Rectangle rectangle3;       // Used to draw the collison rectangle
  25.         private Texture2D blank;            // Texture to be drawn on the collision rectangle
  26.  
  27.         GameplayScreen gameplayScreen;      // Creates an instance of the GameplayScreen
  28.         Game1 gameInstance;                 // Creates an instance of the Game1 class
  29.         int selectedStage;                  // Pass this into GameplayScreen for selecting easy, medium, or hard
  30.  
  31.         // Mercury Particle Engine
  32.         private Renderer myRenderer;                    // Renderer that draws particles to screen
  33.         private ParticleEffect magicTrail;              // Particle effect object to store the info about particle
  34.         private GraphicsDeviceManager graphics;
  35.  
  36.  
  37.         #endregion
  38.  
  39.         #region Constructors and Destructors
  40.  
  41.         /// <summary>
  42.         /// Constructor for the ball
  43.         /// </summary>
  44.         public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
  45.                              
  46.         {
  47.             Speed = 15f;
  48.             texture = contentManager.Load<Texture2D>(@"gfx/balls/ball1");
  49.             direction = 0;
  50.             origin = new Vector2(texture.Width / 2, texture.Height / 2);
  51.             ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
  52.             resetBallPos = new Vector2(ScreenSize.X / 2 + origin.X, ScreenSize.Y / 2 + origin.Y);
  53.             ballPosition = resetBallPos;
  54.             rand = new Random();
  55.             isVisible = true;
  56.             leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
  57.             rightBat = aiBat;// Creates a new instance of leftBat so that  can access Position.X/Y for RightBatPatcicles()
  58.             gameplayScreen = new GameplayScreen(null, selectedStage);
  59.             gameInstance = new Game1();
  60.             Rectangle rectangle3 = new Rectangle();
  61.             blank = contentManager.Load<Texture2D>(@"gfx/blank");
  62.  
  63.             // Merury particle effects
  64.             spriteBatch = new SpriteBatch(gameInstance.GraphicsDevice);
  65.             myRenderer = new SpriteBatchRenderer        // Create new renderer and set its graphics devide to "this" device
  66.             {
  67.                 GraphicsDeviceService = (IGraphicsDeviceService)gameInstance.Services.GetService(typeof(IGraphicsDeviceService))
  68.             };
  69.             magicTrail = new ParticleEffect();          // The actual particle effect  
  70.  
  71.             // Content for Mercury
  72.             magicTrail = contentManager.Load<ParticleEffect>("BasicExplosion");
  73.             magicTrail.LoadContent(contentManager);
  74.             magicTrail.Initialise();
  75.             myRenderer.LoadContent(contentManager);
  76.         }
  77.  
  78.         // this assigns and instantiates the member bat with myBat which was passed from the constructor
  79.         public Ball(Bat myBat)
  80.         {
  81.             leftBat = myBat;
  82.         }
  83.  
  84.         #endregion
  85.  
  86.         #region Methods
  87.  
  88.         /// <summary>
  89.         /// Draws the ball on the screen
  90.         /// </summary>
  91.         public void Draw(SpriteBatch spriteBatch)
  92.         {
  93.             if (isVisible)
  94.             {
  95.                 // Draws the collision rectangle
  96.                 spriteBatch.Draw(blank, rectangle3, Color.LightCoral);
  97.  
  98.             spriteBatch.Draw(texture, ballPosition, null, Color.White,
  99.                       RotationAngle, origin, 1f, SpriteEffects.None, 0);
  100.  
  101.             myRenderer.RenderEffect(magicTrail);              // Draws Mercury. Must remain outside of SpriteBatch
  102.  
  103.             }
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  108.         /// </summary>
  109.         public void UpdatePosition(GameTime gameTime)
  110.         {
  111.             .......        
  112.  
  113.             // The time since Update was called last.
  114.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  115.  
  116.             // Rotation for the ball
  117.             RotationAngle += elapsed;
  118.             float circle = MathHelper.Pi * 2;
  119.             RotationAngle = RotationAngle % circle;
  120.             gameInstance.update();
  121.            
  122.             // Draws Mercury Particle Engine
  123.             magicTrail.Trigger(new Vector2(leftBat.BatPosition.X, leftBat.BatPosition.Y));     
  124.         }
  125.  
  126.         #region BatHit
  127.         /// Checks for the collision between the bat and the ball. Sends ball in the appropriate
  128.         /// direction
  129.         /// </summary>
  130.         public void BatHit(int block, ParticleEmitterService emitter) // Put this here to save me adding Game to this class too :S
  131.         {
  132.             magicTrail.Trigger(new Vector2(500, 500));                // Draws Mercury Particle Engine
  133.        
  134.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  135.             {
  136.                 RightBatParticles();
  137.                 switch (block)
  138.                 {
  139.                     case 1:
  140.                         direction = MathHelper.ToRadians(220);
  141.                         break;
  142.                     ...........
  143.                 }
  144.             }
  145.             else
  146.             {
  147.                 LeftBatParticles();
  148.                 switch (block)
  149.                 {
  150.                     case 1:
  151.                         direction = MathHelper.ToRadians(290);
  152.                         break;
  153.                     .............
  154.                 }
  155.  
  156.  
  157.     }
  158. }
RAW Paste Data