Advertisement
DaveVoyles

ball bat part

Jul 28th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.59 KB | None | 0 0
  1.  
  2.     public class Ball
  3.     {
  4.         private Rectangle ballRect;  
  5.         private AIBat rightBat;
  6.         private Bat leftBat;
  7.         private Rectangle rectangle3;
  8.  
  9.         /// <summary>
  10.         /// Constructor for the ball
  11.         /// </summary>
  12.         public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
  13.         {
  14.             speed = 15f;
  15.             texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
  16.             direction = 0;
  17.             ballRect = new Rectangle(0, 0, texture.Width, texture.Height);
  18.             resetBallPos = new Vector2(ScreenSize.X / 2 + origin.X, ScreenSize.Y / 2 + origin.Y);
  19.             ballPosition = resetBallPos;
  20.             rand = new Random();
  21.             isVisible = true;
  22.             origin = new Vector2(texture.Width / 2 , texture.Height / 2);
  23.             leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
  24.             rightBat = aiBat;// Creates a new instance of leftBat so that I can access Position.X/Y for RightBatPatcicles()
  25.             Rectangle rectangle3 = new Rectangle();
  26.         }
  27.  
  28.      
  29.         public void Draw(SpriteBatch spriteBatch)
  30.         {
  31.             if (isVisible)
  32.             {
  33.                 // Draws the rotaing ball
  34.                 spriteBatch.Draw(texture, ballPosition, ballRect, Color.White,
  35.                                   RotationAngle, origin, 1.0f, SpriteEffects.None, 0);
  36.                 // Draws the short particle
  37.            //     shortParticle.Draw(spriteBatch);
  38.                 spriteBatch.Draw(texture, rectangle3, Color.Yellow);
  39.  
  40.             }
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  45.         /// </summary>
  46.         public void UpdatePosition(GameTime gameTime)
  47.         {
  48.             ballRect.X = (int)ballPosition.X;
  49.             ballRect.Y = (int)ballPosition.Y;
  50.             oldBallPos.X = ballPosition.X;
  51.             oldBallPos.Y = ballPosition.Y;
  52.        
  53.             ballPosition.X += speed * ((float)Math.Cos(direction));
  54.  
  55.             ballPosition.Y += speed * ((float)Math.Sin(direction));
  56.             bool collided = CheckWallHit();
  57.  
  58.             // Stops the issue where ball was oscillating on the ceiling or floor
  59.             if (collided)
  60.             {
  61.                 ballPosition.X = oldBallPos.X + speed * (float)Math.Cos(direction);
  62.                 ballPosition.Y = oldBallPos.Y + speed * (float)Math.Sin(direction);
  63.             }
  64.            
  65.             // When the ball and bat collide, draw the rectangle where they intersect
  66.             BatCollisionRect();          
  67.  
  68.             // The time since Update was called last.
  69.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  70.  
  71.             // Rotation for the ball
  72.             RotationAngle += elapsed;
  73.             float circle = MathHelper.Pi * 2;
  74.             RotationAngle = RotationAngle % circle;        
  75.         }
  76.  
  77.  
  78.        
  79.         /// <summary>
  80.         /// Checks for the collision between the bat and the ball. Sends ball in the appropriate
  81.         /// direction
  82.         /// </summary>
  83.         public void BatHit(int block)
  84.         {
  85.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  86.             {
  87.                 RightBatParticles();
  88.                 switch (block)
  89.                 {
  90.                     case 1:
  91.                         direction = MathHelper.ToRadians(220);
  92.                         break;
  93.                     case 2:
  94.                         direction = MathHelper.ToRadians(215);
  95.                         break;
  96.                     case 3:
  97.                         direction = MathHelper.ToRadians(200);
  98.                         break;
  99.                     case 4:
  100.                         direction = MathHelper.ToRadians(195);
  101.                         break;
  102.                     case 5:
  103.                         direction = MathHelper.ToRadians(180);
  104.                         break;
  105.                     case 6:
  106.                         direction = MathHelper.ToRadians(180);
  107.                         break;
  108.                     case 7:
  109.                         direction = MathHelper.ToRadians(165);
  110.                         break;
  111.                     case 8:
  112.                         direction = MathHelper.ToRadians(130);
  113.                         break;
  114.                     case 9:
  115.                         direction = MathHelper.ToRadians(115);
  116.                         break;
  117.                     case 10:
  118.                         direction = MathHelper.ToRadians(110);
  119.                         break;
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 LeftBatParticles();
  125.                 switch (block)
  126.                 {
  127.                     case 1:
  128.                         direction = MathHelper.ToRadians(290);
  129.                         break;
  130.                     case 2:
  131.                         direction = MathHelper.ToRadians(295);
  132.                         break;
  133.                     case 3:
  134.                         direction = MathHelper.ToRadians(310);
  135.                         break;
  136.                     case 4:
  137.                         direction = MathHelper.ToRadians(345);
  138.                         break;
  139.                     case 5:
  140.                         direction = MathHelper.ToRadians(0);
  141.                         break;
  142.                     case 6:
  143.                         direction = MathHelper.ToRadians(0);
  144.                         break;
  145.                     case 7:
  146.                         direction = MathHelper.ToRadians(15);
  147.                         break;
  148.                     case 8:
  149.                         direction = MathHelper.ToRadians(50);
  150.                         break;
  151.                     case 9:
  152.                         direction = MathHelper.ToRadians(65);
  153.                         break;
  154.                     case 10:
  155.                         direction = MathHelper.ToRadians(70);
  156.                         break;
  157.                 }
  158.             }
  159.  
  160.             if (rand.Next(2) == 0)
  161.             {
  162.                 direction += MathHelper.ToRadians(rand.Next(3));
  163.             }
  164.                 else
  165.             {
  166.                 direction -= MathHelper.ToRadians(rand.Next(3));
  167.             }
  168.         }
  169.    
  170.  
  171.        
  172.         /// <summary>
  173.         /// Used to determine the location where the particles will initialize when the ball and bat collide
  174.         /// </summary>
  175.         public void BatCollisionRect()
  176.         {
  177.             if (ballRect.Intersects(leftBat.batRect))
  178.             {            
  179.              rectangle3 = Rectangle.Intersect(ballRect, leftBat.batRect);
  180.             }
  181.         }
  182.  
  183.         ///<summary>
  184.         /// Sets off particles when the ball and left bat collide
  185.         ///</summary>
  186.         public void LeftBatParticles()
  187.         {
  188.             // If the ball is stopped (what happens when the ball resets), then particles do not go off when the bat hit it.
  189.             // Used to prevent a bug when the particles and sound would go off while the ball is resetting.
  190.                 if (speed > 0)
  191.                 {
  192.                    // particleEngine.EmitterLocation = new Vector2(leftBat.Position.X + 30, leftBat.Position.Y + 40);
  193.            //         shortParticle.EmitterLocation = new Vector2(leftBat.Position.X + 30, leftBat.Position.Y + 40);
  194.                     AudioManager.Instance.PlaySoundEffect("hit2");
  195.                 }
  196.         }
  197.  
  198.         ///<summary>
  199.         /// Sets off particles when the ball and right bat collide
  200.         ///</summary>
  201.         public void RightBatParticles()
  202.         {
  203.  
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement