Guest User

Ball + bat collision

a guest
Jun 29th, 2012
27
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. namespace Pong
  3. {
  4.     #region Using Statements
  5.     using System;
  6.     using System.Collections.Generic;
  7.  
  8.     using Microsoft.Xna.Framework;
  9.     using Microsoft.Xna.Framework.Audio;
  10.     using Microsoft.Xna.Framework.Content;
  11.     using Microsoft.Xna.Framework.Graphics;
  12.     using Microsoft.Xna.Framework.Input;
  13.     #endregion
  14.    
  15.     public class Ball
  16.     {
  17.         #region Fields
  18.  
  19.         private readonly Random rand;
  20.         private readonly Texture2D texture;
  21.         private readonly SoundEffect warp;
  22.         private double direction;
  23.         private bool isVisible;
  24.         private Vector2 position;
  25.         private Vector2 resetPos;
  26.         private Rectangle size;
  27.         public float speed;
  28.         private bool isResetting;
  29.         private bool collided;
  30.         private Vector2 oldPos;
  31.         private ParticleEngine particleEngine;
  32.         private ContentManager contentManager;
  33.         private SpriteBatch spriteBatch;
  34.         private bool hasHitLeftBat;
  35.         private bool hasHitRightBat;
  36.         public Vector2 Origin;
  37.         public float RotationAngle;
  38.         private AIBat rightBat;
  39.         private Bat leftBat;
  40.         KeyboardState current, previous;
  41.  
  42.  
  43.         /// <summary>
  44.         /// Constructor for the ball
  45.         /// </summary>
  46.         public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
  47.         {
  48.             speed = 15f;
  49.             texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
  50.             direction = 0;
  51.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  52.             resetPos = new Vector2(ScreenSize.X / 2, ScreenSize.Y / 2);
  53.             position = resetPos;
  54.             rand = new Random();
  55.             isVisible = true;
  56.             hasHitLeftBat = false;
  57.             Origin = new Vector2(texture.Width / 2 , texture.Height / 2);
  58.             leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
  59.             rightBat = aiBat;// Creates a new instance of leftBat so that I can access Position.X/Y for RightBatPatcicles()
  60.  
  61.           }
  62.  
  63.         public Ball(Bat myBat)
  64.         {
  65.             leftBat = myBat;      // this assigns and instantiates the member bat
  66.                              // with myBat which was passed from the constructor
  67.         }
  68.                    
  69.         /// <summary>
  70.         /// Draws the ball on the screen
  71.         /// </summary>
  72.         public void Draw(SpriteBatch spriteBatch)
  73.         {
  74.             if (isVisible)
  75.             {
  76.  
  77.                 // ideally, we'd just draw at position, but the collision code is a offset,
  78.                 // so I'm offsetting by the origin
  79.                 //spriteBatch.Draw(texture, position + Origin, null, Color.White,
  80.                 //    RotationAngle, Origin, 1.0f, SpriteEffects.None, 0);
  81.                 spriteBatch.Draw(texture, position, null, Color.White,
  82.     RotationAngle, Origin, 1.0f, SpriteEffects.None, 0);
  83.  
  84.                 //spriteBatch.Draw(texture, size, Color.White);
  85.  
  86.                 // Draws sprites for particles when contact is made
  87.                 particleEngine.Draw(spriteBatch);
  88.             }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  93.         /// </summary>
  94.         public void UpdatePosition(GameTime gameTime)
  95.         {
  96.  
  97.             size.X = (int)position.X;
  98.             size.Y = (int)position.Y;
  99.             oldPos.X = position.X;
  100.             oldPos.Y = position.Y;
  101.  
  102.             previous = current;
  103.             current = Keyboard.GetState();
  104.             if (current.IsKeyDown(Keys.Z))
  105.             {
  106.                 SlowMoBall();
  107.             }
  108.        
  109.             position.X += speed * ((float)Math.Cos(direction));
  110.  
  111.             position.Y += speed * ((float)Math.Sin(direction));
  112.             bool collided = CheckWallHit();
  113.             // Updates particles for when the ball and bat make contact
  114.             particleEngine.Update();
  115.  
  116.             // Stops the issue where ball was oscillating on the ceiling or floor
  117.             if (collided)
  118.             {
  119.                 position.X = oldPos.X + speed * (float)Math.Cos(direction);
  120.                 position.Y = oldPos.Y + speed * (float)Math.Sin(direction);
  121.             }
  122.  
  123.             // The time since Update was called last.
  124.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  125.  
  126.             RotationAngle += elapsed;
  127.             float circle = MathHelper.Pi * 2;
  128.             RotationAngle = RotationAngle % circle;
  129.         }
  130.        
  131.         /// <summary>
  132.         /// Checks for the collision between the bat and the ball. Sends ball in the appropriate
  133.         /// direction
  134.         /// </summary>
  135.         public void BatHit(int block)
  136.         {
  137.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  138.             {
  139.                 hasHitRightBat = true;
  140.                 RightBatParticles(hasHitRightBat);
  141.                 switch (block)
  142.                 {
  143.                     case 1:
  144.                         direction = MathHelper.ToRadians(200);
  145.                         break;
  146.                     case 2:
  147.                         direction = MathHelper.ToRadians(195);
  148.                         break;
  149.                     case 3:
  150.                         direction = MathHelper.ToRadians(180);  
  151.                         break;
  152.                     case 4:
  153.                         direction = MathHelper.ToRadians(180);
  154.                         break;
  155.                     case 5:
  156.                         direction = MathHelper.ToRadians(165);
  157.                         break;
  158.                 }
  159.             }
  160.             else
  161.             {
  162.                 hasHitLeftBat = true;
  163.                 LeftBatParticles(hasHitLeftBat);
  164.                 switch (block)
  165.                 {
  166.                     case 1:
  167.                         direction = MathHelper.ToRadians(310);
  168.                         break;
  169.                     case 2:
  170.                         direction = MathHelper.ToRadians(345);
  171.                         break;
  172.                     case 3:
  173.                         direction = MathHelper.ToRadians(0);
  174.                         break;
  175.                     case 4:
  176.                         direction = MathHelper.ToRadians(15);
  177.                         break;
  178.                     case 5:
  179.                         direction = MathHelper.ToRadians(50);
  180.                         break;
  181.                 }
  182.             }
  183.  
  184.             if (rand.Next(2) == 0)
  185.             {
  186.                 direction += MathHelper.ToRadians(rand.Next(3));
  187.             }
  188.                 else
  189.             {
  190.                 direction -= MathHelper.ToRadians(rand.Next(3));
  191.             }
  192.            
  193.             // Plays the sound effect for when the ball and bat collide
  194.             AudioManager.Instance.PlaySoundEffect("hit");
  195.         }
  196.                
  197.      
  198.         /// <summary>
  199.         /// Grows the size of the ball when the GrowBall powerup is used.
  200.         /// </summary>
  201.         public void GrowBall()
  202.         {
  203.             size = new Rectangle(0, 0, texture.Width * 2, texture.Height * 2);
  204.         }
  205.  
  206.          /// <summary>
  207.         /// Check for the ball to return normal size after the Powerup has expired
  208.         /// </summary>
  209.         public void NormalBallSize()
  210.         {
  211.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  212.         }
  213.  
  214.  
  215.  
  216.         /// <summary>
  217.         /// Checks for collision with the ceiling or floor. 2*Math.pi = 360 degrees
  218.         /// </summary>
  219.         private bool CheckWallHit()
  220.         {
  221.             while (direction > 2 * Math.PI)
  222.             {
  223.                 direction -= 2 * Math.PI;
  224.                 return true;
  225.             }
  226.  
  227.             while (direction < 0)
  228.             {
  229.                 direction += 2 * Math.PI;
  230.                 return true;
  231.             }
  232.  
  233.             if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
  234.             {
  235.                 direction = 2 * Math.PI - direction;
  236.                 return true;
  237.             }
  238.             return true;
  239.         }
  240.  
  241.        // Sets off particles when the ball and right bat collide
  242.         public void LeftBatParticles(bool hasHitLeftBat)
  243.         {
  244.             if (hasHitLeftBat == true)
  245.             {
  246.                 particleEngine.EmitterLocation = new Vector2(leftBat.Position.X +30  , leftBat.Position.Y  +40);
  247.             }
  248.         }
  249.  
  250.         // Sets off particles when the ball and left bat collide
  251.         public void RightBatParticles(bool hasHitRightBat)
  252.         {
  253.             if (hasHitRightBat == true)
  254.             {
  255.                 particleEngine.EmitterLocation = new Vector2(rightBat.Position.X +20, rightBat.Position.Y +40);
  256.             }
  257.         }
  258.     }
  259. }
RAW Paste Data