Advertisement
DaveVoyles

Bat, Ball, AiBat

Jul 2nd, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.12 KB | None | 0 0
  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 Random rand;
  20.         private Texture2D texture;
  21.         private double direction;
  22.         private bool isVisible;
  23.         private Vector2 position, resetPos, oldPos;
  24.         private Rectangle size;
  25.         public float speed;
  26.         private ParticleEngine particleEngine;
  27.         private ContentManager contentManager;
  28.         private SpriteBatch spriteBatch;
  29.         public bool isBallStopped;
  30.         public Vector2 Origin;
  31.         public float RotationAngle;
  32.         private AIBat rightBat;
  33.         private Bat leftBat;
  34.         KeyboardState current, previous;
  35.  
  36.          #endregion
  37.  
  38.         #region Constructors and Destructors
  39.  
  40.         /// <summary>
  41.         /// Constructor for the ball
  42.         /// </summary>
  43.         public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
  44.         {
  45.             speed = 15f;
  46.             texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
  47.             direction = 0;
  48.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  49.             resetPos = new Vector2(ScreenSize.X / 2 + Origin.X, ScreenSize.Y / 2 + Origin.Y);
  50.             position = resetPos;
  51.             rand = new Random();
  52.             isVisible = true;
  53.             Origin = new Vector2(texture.Width / 2 , texture.Height / 2);
  54.             leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
  55.             rightBat = aiBat;// Creates a new instance of leftBat so that I can access Position.X/Y for RightBatPatcicles()
  56.  
  57.             // Everything to do with particles
  58.             List<Texture2D> textures = new List<Texture2D>();
  59.             textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/circle"));
  60.             textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/star"));
  61.             textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/diamond"));
  62.             particleEngine = new ParticleEngine(textures, new Vector2());
  63.  
  64.         }
  65.  
  66.         public Ball(Bat myBat)
  67.         {
  68.             leftBat = myBat;      // this assigns and instantiates the member bat
  69.                                   // with myBat which was passed from the constructor
  70.         }
  71.                    
  72.         #endregion
  73.  
  74.         #region Methods
  75.  
  76.         /// <summary>
  77.         /// Draws the ball on the screen
  78.         /// </summary>
  79.         public void Draw(SpriteBatch spriteBatch)
  80.         {
  81.             if (isVisible)
  82.             {
  83.                 // Draws the rotaing ball
  84.                 spriteBatch.Draw(texture, position, size, Color.White,
  85.                                   RotationAngle, Origin, 1.0f, SpriteEffects.None, 0);
  86.  
  87.                 // Draws sprites for particles when contact is made
  88.                 particleEngine.Draw(spriteBatch);
  89.             }
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Updates position of the ball. Used in Update() for GameplayScreen.
  94.         /// </summary>
  95.         public void UpdatePosition(GameTime gameTime)
  96.         {
  97.  
  98.             size.X = (int)position.X;
  99.             size.Y = (int)position.Y;
  100.             oldPos.X = position.X;
  101.             oldPos.Y = position.Y;
  102.  
  103.             previous = current;
  104.             current = Keyboard.GetState();
  105.             if (current.IsKeyDown(Keys.Z))
  106.             {
  107.                 SlowMoBall();
  108.             }
  109.        
  110.             position.X += speed * ((float)Math.Cos(direction));
  111.  
  112.             position.Y += speed * ((float)Math.Sin(direction));
  113.             bool collided = CheckWallHit();
  114.  
  115.             // Stops the issue where ball was oscillating on the ceiling or floor
  116.             if (collided)
  117.             {
  118.                 position.X = oldPos.X + speed * (float)Math.Cos(direction);
  119.                 position.Y = oldPos.Y + speed * (float)Math.Sin(direction);
  120.             }
  121.  
  122.             // The time since Update was called last.
  123.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  124.  
  125.             // Rotation for the ball
  126.             RotationAngle += elapsed;
  127.             float circle = MathHelper.Pi * 2;
  128.             RotationAngle = RotationAngle % circle;
  129.  
  130.             // Updates particles for when the ball and bat make contact
  131.             particleEngine.Update();
  132.         }
  133.        
  134.         /// <summary>
  135.         /// Checks for the collision between the bat and the ball. Sends ball in the appropriate
  136.         /// direction
  137.         /// </summary>
  138.         public void BatHit(int block)
  139.         {
  140.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  141.             {
  142.                 RightBatParticles();
  143.                 switch (block)
  144.                 {
  145.                     case 1:
  146.                         direction = MathHelper.ToRadians(220);
  147.                         break;
  148.                     case 2:
  149.                         direction = MathHelper.ToRadians(215);
  150.                         break;
  151.                     case 3:
  152.                         direction = MathHelper.ToRadians(200);
  153.                         break;
  154.                     case 4:
  155.                         direction = MathHelper.ToRadians(195);
  156.                         break;
  157.                     case 5:
  158.                         direction = MathHelper.ToRadians(180);
  159.                         break;
  160.                     case 6:
  161.                         direction = MathHelper.ToRadians(180);
  162.                         break;
  163.                     case 7:
  164.                         direction = MathHelper.ToRadians(165);
  165.                         break;
  166.                     case 8:
  167.                         direction = MathHelper.ToRadians(130);
  168.                         break;
  169.                     case 9:
  170.                         direction = MathHelper.ToRadians(115);
  171.                         break;
  172.                     case 10:
  173.                         direction = MathHelper.ToRadians(110);
  174.                         break;
  175.                 }
  176.             }
  177.             else
  178.             {
  179.                 LeftBatParticles();
  180.                 switch (block)
  181.                 {
  182.                     case 1:
  183.                         direction = MathHelper.ToRadians(290);
  184.                         break;
  185.                     case 2:
  186.                         direction = MathHelper.ToRadians(295);
  187.                         break;
  188.                     case 3:
  189.                         direction = MathHelper.ToRadians(310);
  190.                         break;
  191.                     case 4:
  192.                         direction = MathHelper.ToRadians(345);
  193.                         break;
  194.                     case 5:
  195.                         direction = MathHelper.ToRadians(0);
  196.                         break;
  197.                     case 6:
  198.                         direction = MathHelper.ToRadians(0);
  199.                         break;
  200.                     case 7:
  201.                         direction = MathHelper.ToRadians(15);
  202.                         break;
  203.                     case 8:
  204.                         direction = MathHelper.ToRadians(50);
  205.                         break;
  206.                     case 9:
  207.                         direction = MathHelper.ToRadians(65);
  208.                         break;
  209.                     case 10:
  210.                         direction = MathHelper.ToRadians(70);
  211.                         break;
  212.                 }
  213.             }
  214.  
  215.             if (rand.Next(2) == 0)
  216.             {
  217.                 direction += MathHelper.ToRadians(rand.Next(3));
  218.             }
  219.                 else
  220.             {
  221.                 direction -= MathHelper.ToRadians(rand.Next(3));
  222.             }
  223.         }
  224.      
  225.         /// <summary>
  226.         /// Check for the ball to return normal speed after the Powerup has expired
  227.         /// </summary>
  228.         public void NormalSpeed()
  229.         {
  230.             speed += 15f;
  231.         }
  232.  
  233.         /// <summary>
  234.         /// Check for the ball to return normal speed after the Powerup has expired
  235.         /// </summary>
  236.         public float GetSpeed()
  237.         {
  238.             return speed;
  239.         }
  240.  
  241.  
  242.         ///<summary>
  243.         ///Used for slowing down the ball when SlowMo is active
  244.         ///</summary>
  245.         public void SlowMoBall()
  246.         {
  247.             speed = 2f;
  248.         }
  249.  
  250.         public void ReturnToNormalSpeed()
  251.         {
  252.             //if (position.X <= GameplayScreen.Instance.screenWidth / 2)
  253.             //{
  254.             //    speed = 15f;
  255.             //}
  256.             //else
  257.             //{
  258.             //    speed = -15f;
  259.             //}
  260.             speed = 15f;
  261.         }
  262.  
  263.         /// <summary>
  264.         /// Checks for the current direction of the ball
  265.         /// </summary>
  266.         public double GetDirection()
  267.         {
  268.             return direction;
  269.         }
  270.  
  271.         /// <summary>
  272.         /// Checks for the current position of the ball
  273.         /// </summary>
  274.         public Vector2 GetPosition()
  275.         {
  276.             return position;
  277.         }
  278.  
  279.         /// <summary>
  280.         /// Checks for the current size of the ball (for the powerups)
  281.         /// </summary>
  282.         public Rectangle GetSize()
  283.         {
  284.             return size;
  285.         }
  286.  
  287.         /// <summary>
  288.         /// Grows the size of the ball when the GrowBall powerup is used.
  289.         /// </summary>
  290.         public void GrowBall()
  291.         {
  292.             size = new Rectangle(0, 0, texture.Width * 2, texture.Height * 2);
  293.         }
  294.  
  295.         /// <summary>
  296.         /// Check for the ball to return normal size after the Powerup has expired
  297.         /// </summary>
  298.         public void NormalBallSize()
  299.         {
  300.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  301.         }
  302.  
  303.  
  304.         /// <summary>
  305.         /// Checks to see if ball went out of bounds, and triggers warp sfx. Used in GameplayScreen.
  306.         /// </summary>
  307.         public void OutOfBounds()
  308.         {
  309.                 AudioManager.Instance.PlaySoundEffect("warp");  
  310.         }
  311.  
  312.         /// <summary>
  313.         /// Speed for the ball when Speedball powerup is activated
  314.         /// </summary>
  315.         public void PowerupSpeed()
  316.         {
  317.             speed += 20.0f;
  318.         }
  319.  
  320.         /// <summary>
  321.         /// Check for where to reset the ball after each point is scored
  322.         /// </summary>
  323.         public void Reset(bool left)
  324.         {
  325.             if (left)
  326.             {
  327.                 direction = 0;
  328.             }
  329.             else
  330.             {
  331.                 direction = Math.PI;
  332.             }
  333.  
  334.             position = resetPos; // Resets the ball to the center of the screen
  335.             isVisible = true;
  336.             speed = 15f; // Returns the ball back to the default speed, in case the speedBall was active
  337.             if (rand.Next(2) == 0)
  338.             {
  339.                 direction += MathHelper.ToRadians(rand.Next(30));
  340.             }
  341.             else
  342.             {
  343.                 direction -= MathHelper.ToRadians(rand.Next(30));
  344.             }
  345.         }
  346.  
  347.         /// <summary>
  348.         /// Shrinks the ball when the ShrinkBall powerup is activated
  349.         /// </summary>
  350.         public void ShrinkBall()
  351.         {
  352.             size = new Rectangle(0, 0, texture.Width / 2, texture.Height / 2);
  353.         }
  354.  
  355.         /// <summary>
  356.         /// Stops the ball each time it is reset. Ex: Between points / rounds
  357.         /// </summary>
  358.         public void Stop()
  359.         {
  360.             isVisible = true;
  361.             speed = 0;
  362.             isBallStopped = true;
  363.         }    
  364.  
  365.         /// <summary>
  366.         /// Checks for collision with the ceiling or floor. 2*Math.pi = 360 degrees
  367.         /// </summary>
  368.         private bool CheckWallHit()
  369.         {
  370.             while (direction > 2 * Math.PI)
  371.             {
  372.                 direction -= 2 * Math.PI;
  373.                 return true;
  374.             }
  375.  
  376.             while (direction < 0)
  377.             {
  378.                 direction += 2 * Math.PI;
  379.                 return true;
  380.             }
  381.  
  382.             if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
  383.             {
  384.                 direction = 2 * Math.PI - direction;
  385.                 return true;
  386.             }
  387.             return true;
  388.         }
  389.  
  390.         ///<summary>
  391.         /// Sets off particles when the ball and left bat collide
  392.         ///</summary>
  393.         public void LeftBatParticles()
  394.         {
  395.             // If the ball is stopped (what happens when the ball resets), then particles do not go off when the bat hit it.
  396.             // Used to prevent a bug when the particles and sound would go off while the ball is resetting.
  397.                 if (speed > 0)
  398.                 {
  399.                     particleEngine.EmitterLocation = new Vector2(leftBat.Position.X + 30, leftBat.Position.Y + 40);
  400.                     AudioManager.Instance.PlaySoundEffect("hit");
  401.                 }
  402.         }
  403.  
  404.         ///<summary>
  405.         /// Sets off particles when the ball and right bat collide
  406.         ///</summary>
  407.         public void RightBatParticles()
  408.         {
  409.             // If the ball is stopped (what happens when the ball resets), then particles do not go off when the bat hit it.
  410.             // Used to prevent a bug when the particles and sound would go off while the ball is resetting.
  411.    
  412.              if (speed > 0)
  413.             {
  414.                 particleEngine.EmitterLocation = new Vector2(rightBat.Position.X +20, rightBat.Position.Y +40);
  415.                 AudioManager.Instance.PlaySoundEffect("hit");
  416.             }
  417.         }
  418.  
  419.         #endregion
  420.     }
  421. }
  422.  
  423. //////////////////////////////////////////////////////////////////////////
  424.  
  425.  
  426. namespace Pong
  427. {
  428.     using Microsoft.Xna.Framework;
  429.     using Microsoft.Xna.Framework.Content;
  430.     using Microsoft.Xna.Framework.Graphics;
  431.     using Microsoft.Xna.Framework.Input;
  432.     using System;
  433.  
  434.     public class Bat
  435.     {
  436.         public Vector2 Position;
  437.         public float moveSpeed, elapsedTime;
  438.         public Rectangle size;
  439.         private int points, yHeight;
  440.         private Texture2D leftBat;
  441.         public float turbo, recharge, interval;
  442.         public bool isTurbo;
  443.         private KeyboardState current, previous;
  444.  
  445.         /// <summary>
  446.         /// Constructor for the bat
  447.         /// </summary>
  448.         public Bat(ContentManager contentManager, Vector2 screenSize, bool side)
  449.         {
  450.             moveSpeed = 7f;
  451.             turbo = 15f;
  452.             recharge = 100f;
  453.             points = 0;
  454.             interval = 5f;
  455.             leftBat = contentManager.Load<Texture2D>(@"gfx/bats/batGrey");
  456.             size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  457.  
  458.             // True means left bat, false means right bat.
  459.             if (side) Position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
  460.             else Position = new Vector2(screenSize.X - 30, screenSize.Y / 2 - size.Height / 2);
  461.             yHeight = (int)screenSize.Y;
  462.         }
  463.  
  464.         public void IncreaseSpeed()
  465.         {
  466.             moveSpeed += .5f;
  467.         }
  468.  
  469.         /// <summary>
  470.         /// The speed of the bat when Turbo is activated
  471.         /// </summary>
  472.         public void Turbo()
  473.         {
  474.             moveSpeed += 8.0f;
  475.         }
  476.  
  477.         /// <summary>
  478.         /// Returns the speed of the bat back to normal after Turbo is deactivated
  479.         /// </summary>
  480.         public void DisableTurbo()
  481.         {
  482.             moveSpeed = 7.0f;
  483.             isTurbo = false;
  484.         }
  485.  
  486.         /// <summary>
  487.         /// Returns the bat to the nrmal size after the Grow/Shrink powerup has expired
  488.         /// </summary>
  489.         public void NormalSize()
  490.         {
  491.             size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  492.         }
  493.  
  494.         /// <summary>
  495.         /// Checks for the size of the bat
  496.         /// </summary>
  497.         public Rectangle GetSize()
  498.         {
  499.             return size;
  500.         }
  501.  
  502.         /// <summary>
  503.         /// Used for the Shrinkbat powerup
  504.         /// </summary>
  505.         public void ShrinkBat()
  506.         {
  507.             // 1/2 the size of the bat collision
  508.             size = new Rectangle(0, 0, leftBat.Width / 2, leftBat.Height / 2);
  509.         }
  510.  
  511.         /// <summary>
  512.         /// Adds point to the player or the AI after scoring. Currently Disabled.
  513.         /// </summary>
  514.         public void IncrementPoints()
  515.         {
  516.             points++;
  517.         }
  518.  
  519.         /// <summary>
  520.         /// Checks for the number of points at the moment
  521.         /// </summary>
  522.         public int GetPoints()
  523.         {
  524.             return points;
  525.         }
  526.  
  527.         /// <summary>
  528.         /// Sets thedefault starting position for the bats
  529.         /// </summary>
  530.         /// <param name="position"></param>
  531.         public void SetPosition(Vector2 position)
  532.         {
  533.             if (position.Y < 0)
  534.             {
  535.                 position.Y = 0;
  536.             }
  537.             if (position.Y > yHeight - size.Height)
  538.             {
  539.                 position.Y = yHeight - size.Height;
  540.             }
  541.             this.Position = position;
  542.         }
  543.  
  544.  
  545.         /// <summary>
  546.         /// Checks for the current position of the bat
  547.         /// </summary>
  548.         public Vector2 GetPosition()
  549.         {
  550.             return Position;
  551.         }
  552.  
  553.         /// <summary>
  554.         /// Controls the bat moving up the screen
  555.         /// </summary>
  556.         public void MoveUp()
  557.         {
  558.             SetPosition(Position + new Vector2(0, -moveSpeed * elapsedTime));
  559.  
  560.         }
  561.  
  562.         /// <summary>
  563.         /// Controls the bat moving down the screen
  564.         /// </summary>
  565.         public void MoveDown()
  566.         {
  567.             SetPosition(Position + new Vector2(0, moveSpeed * elapsedTime));
  568.         }
  569.  
  570.         /// <summary>
  571.         /// Updates the position of the AI bat, in order to track the ball
  572.         /// </summary>
  573.         public virtual void UpdatePosition(Ball ball, GameTime gameTime)
  574.         {
  575.             size.X = (int)Position.X;
  576.             size.Y = (int)Position.Y;
  577.  
  578.             elapsedTime = 50.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;
  579.  
  580.             // Just here for debugging. Hitting Z WORKS FINE and slows the bats down
  581.             previous = current;
  582.             current = Keyboard.GetState();
  583.             if (current.IsKeyDown(Keys.Z))
  584.             {
  585.                 elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
  586.             }
  587.         }
  588.  
  589.         /// <summary>
  590.         /// Resets the bat to the center location after a new game starts
  591.         /// </summary>
  592.         public void ResetPosition()
  593.         {
  594.             SetPosition(new Vector2(GetPosition().X, yHeight / 2 - size.Height));
  595.         }
  596.        
  597.         /// <summary>
  598.         /// Returns the ball to normal speed
  599.         /// </summary>
  600.         public void ReturnToNormalSpeed()
  601.         {
  602.             moveSpeed = 7f;
  603.         }
  604.  
  605.         /// <summary>
  606.         /// Used for the Growbat powerup
  607.         /// </summary>
  608.         public void GrowBat()
  609.         {
  610.             // Doubles the size of the bat collision
  611.             size = new Rectangle(0, 0, leftBat.Width * 2, leftBat.Height * 2);
  612.         }
  613.  
  614.         /// <summary>
  615.         /// Draws the bats
  616.         /// </summary>
  617.         public virtual void Draw(SpriteBatch spriteBatch)
  618.         {
  619.             spriteBatch.Draw(leftBat, size, Color.White);
  620.         }
  621.     }
  622. }
  623.  
  624.  
  625. //////////////////////////////////////////////////////////////////////////////
  626.  
  627. namespace Pong
  628. {
  629.     using System;
  630.     using Microsoft.Xna.Framework;
  631.     using Microsoft.Xna.Framework.Content;
  632.     using Microsoft.Xna.Framework.Graphics;
  633.  
  634.     public class AIBat : Bat
  635.     {
  636.         public Texture2D rightBat;
  637.  
  638.         /// <summary>
  639.         /// Constructor for the AIBat
  640.         /// </summary>
  641.         public AIBat(ContentManager contentManager, Vector2 screenSize, bool side) : base(contentManager, screenSize, side)
  642.         {
  643.             rightBat = contentManager.Load<Texture2D>(@"gfx/bats/batBlack");
  644.         }
  645.  
  646.         /// <summary>
  647.         /// Updates the position of the ball in the GameplayScreen
  648.         /// </summary>
  649.         public override void UpdatePosition(Ball ball, GameTime gameTime)
  650.         {
  651.             if (ball.GetDirection() > 1.5 * Math.PI || ball.GetDirection() < 0.5 * Math.PI)
  652.             {
  653.                 if (ball.GetPosition().Y - 5 > GetPosition().Y + GetSize().Height / 2)
  654.                 {
  655.                     MoveDown();
  656.                 }
  657.                 else if (ball.GetPosition().Y == GetPosition().Y + GetSize().Height / 2)
  658.                 {
  659.                 }
  660.                 else if (ball.GetPosition().Y + 5 < GetPosition().Y + GetSize().Height / 2)
  661.                 {
  662.                     MoveUp();
  663.                 }
  664.             }
  665.             base.UpdatePosition(ball, gameTime);
  666.  
  667.         }
  668.  
  669.         /// <summary>
  670.         /// Draws the AIBat
  671.         /// </summary>
  672.         public override void Draw(SpriteBatch batch)
  673.         {
  674.             batch.Draw(rightBat, size, Color.White);
  675.         }
  676.     }
  677. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement