diliupg

GameProject Project Increment2 TeddyBear.cs

Sep 21st, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10.  
  11. namespace GameProject
  12. {
  13.     /// <summary>
  14.     /// A class for a teddy bear
  15.     /// </summary>
  16.     public class TeddyBear
  17.     {
  18.         #region Fields
  19.  
  20.         bool active = true;
  21.  
  22.         // drawing support
  23.         Texture2D sprite;
  24.         Rectangle drawRectangle;
  25.  
  26.         // velocity information
  27.         Vector2 velocity = new Vector2(0, 0);
  28.  
  29.         // shooting support
  30.         int elapsedShotMilliseconds = 0;
  31.         int firingDelay;
  32.  
  33.         // sound effects
  34.         SoundEffect bounceSound;
  35.         SoundEffect shootSound;
  36.  
  37.         #endregion
  38.  
  39.         #region Constructors
  40.  
  41.         /// <summary>
  42.         ///  Constructs a teddy bear centered on the given x and y with the
  43.         ///  given velocity
  44.         /// </summary>
  45.         /// <param name="contentManager">the content manager for loading content</param>
  46.         /// <param name="spriteName">the name of the sprite for the teddy bear</param>
  47.         /// <param name="x">the x location of the center of the teddy bear</param>
  48.         /// <param name="y">the y location of the center of the teddy bear</param>
  49.         /// <param name="bounceSound">the sound the teddy bear plays when bouncing</param>
  50.         /// <param name="shootSound">the sound the teddy bear plays when shooting</param>
  51.         public TeddyBear(ContentManager contentManager, string spriteName, int x, int y,
  52.             Vector2 velocity, SoundEffect bounceSound, SoundEffect shootSound)
  53.         {
  54.             LoadContent(contentManager, spriteName, x, y);
  55.             this.velocity = velocity;
  56.             this.bounceSound = bounceSound;
  57.             this.shootSound = shootSound;
  58.             firingDelay = GetRandomFiringDelay();
  59.         }
  60.  
  61.         #endregion
  62.  
  63.         #region Properties
  64.  
  65.         /// <summary>
  66.         /// Gets and sets whether or not the teddy bear is active
  67.         /// </summary>
  68.         public bool Active
  69.         {
  70.             get { return active; }
  71.             set { active = value; }
  72.         }
  73.  
  74.         /// <summary>
  75.         /// Gets the location of the teddy bear
  76.         /// </summary>
  77.         public Point Location
  78.         {
  79.             get { return drawRectangle.Center; }
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Sets the x location of the center of the teddy bear
  84.         /// </summary>
  85.         public int X
  86.         {
  87.             set { drawRectangle.X = value - drawRectangle.Width / 2; }
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Sets the y location of the center of the teddy bear
  92.         /// </summary>
  93.         public int Y
  94.         {
  95.             set { drawRectangle.Y = value - drawRectangle.Height / 2; }
  96.         }
  97.  
  98.         /// <summary>
  99.         /// Gets the collision rectangle for the teddy bear
  100.         /// </summary>
  101.         public Rectangle CollisionRectangle
  102.         {
  103.             get { return drawRectangle; }
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Gets and sets the velocity of the teddy bear
  108.         /// </summary>
  109.         public Vector2 Velocity
  110.         {
  111.             get { return velocity; }
  112.             set { velocity = value; }
  113.         }
  114.  
  115.         /// <summary>
  116.         /// Gets and sets the draw rectangle for the teddy bear
  117.         /// </summary>
  118.         public Rectangle DrawRectangle
  119.         {
  120.             get { return drawRectangle; }
  121.             set { drawRectangle = value; }
  122.         }
  123.  
  124.         #endregion
  125.  
  126.         #region Public methods
  127.  
  128.         /// <summary>
  129.         /// Updates the teddy bear's location, bouncing if necessary. Also has
  130.         /// the teddy bear fire a projectile when it's time to
  131.         /// </summary>
  132.         /// <param name="gameTime">game time</param>
  133.         public void Update(GameTime gameTime)
  134.         {
  135.             // move the teddy bear
  136.             drawRectangle.X += (int)(velocity.X * gameTime.ElapsedGameTime.Milliseconds);
  137.             drawRectangle.Y += (int)(velocity.Y * gameTime.ElapsedGameTime.Milliseconds);
  138.  
  139.  
  140.             // bounce as necessary
  141.             BounceTopBottom();
  142.             BounceLeftRight();
  143.  
  144.             // fire projectile as appropriate
  145.             // timer concept (for animations) introduced in Chapter 7
  146.             elapsedShotMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  147.             if(elapsedShotMilliseconds > firingDelay)
  148.             {
  149.                 elapsedShotMilliseconds = 0;
  150.                 firingDelay = GetRandomFiringDelay();
  151.  
  152.                 Projectile projectile = new Projectile(ProjectileType.TeddyBear,
  153.                 Game1.GetProjectileSprite(ProjectileType.TeddyBear),
  154.                 drawRectangle.X + drawRectangle.Width / 2,
  155.                 drawRectangle.Y + drawRectangle.Height + GameConstants.TeddyBearProjectileOffset,
  156.                 GameConstants.TeddyBearProjectileSpeed * Convert.ToSingle(Math.Sin(90)));
  157.  
  158.                 Game1.AddProjectile(projectile);
  159.  
  160.  
  161.             }
  162.  
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Draws the teddy bear
  167.         /// </summary>
  168.         /// <param name="spriteBatch">the sprite batch to use</param>
  169.         public void Draw(SpriteBatch spriteBatch)
  170.         {
  171.             spriteBatch.Draw(sprite, drawRectangle, Color.White);
  172.         }
  173.  
  174.         #endregion
  175.  
  176.         #region Private methods
  177.  
  178.         /// <summary>
  179.         /// Loads the content for the teddy bear
  180.         /// </summary>
  181.         /// <param name="contentManager">the content manager to use</param>
  182.         /// <param name="spriteName">the name of the sprite for the teddy bear</param>
  183.         /// <param name="x">the x location of the center of the teddy bear</param>
  184.         /// <param name="y">the y location of the center of the teddy bear</param>
  185.         private void LoadContent(ContentManager contentManager, string spriteName,
  186.             int x, int y)
  187.         {
  188.             // load content and set remainder of draw rectangle
  189.             sprite = contentManager.Load<Texture2D>(spriteName);
  190.             drawRectangle = new Rectangle(x - sprite.Width / 2,
  191.                 y - sprite.Height / 2, sprite.Width,
  192.                 sprite.Height);
  193.         }
  194.  
  195.         /// <summary>
  196.         /// Bounces the teddy bear off the top and bottom window borders if necessary
  197.         /// </summary>
  198.         private void BounceTopBottom()
  199.         {
  200.             if (drawRectangle.Y < 0)
  201.             {
  202.                 // bounce off top
  203.                 drawRectangle.Y = 0;
  204.                 velocity.Y *= -1;
  205.             }
  206.             else if ((drawRectangle.Y + drawRectangle.Height) > GameConstants.WindowHeight)
  207.             {
  208.                 // bounce off bottom
  209.                 drawRectangle.Y = GameConstants.WindowHeight - drawRectangle.Height;
  210.                 velocity.Y *= -1;
  211.             }
  212.         }
  213.         /// <summary>
  214.         /// Bounces the teddy bear off the left and right window borders if necessary
  215.         /// </summary>
  216.         private void BounceLeftRight()
  217.         {
  218.             if (drawRectangle.X < 0)
  219.             {
  220.                 // bounc off left
  221.                 drawRectangle.X = 0;
  222.                 velocity.X *= -1;
  223.             }
  224.             else if ((drawRectangle.X + drawRectangle.Width) > GameConstants.WindowWidth)
  225.             {
  226.                 // bounce off right
  227.                 drawRectangle.X = GameConstants.WindowWidth - drawRectangle.Width;
  228.                 velocity.X *= -1;
  229.             }
  230.         }
  231.  
  232.         /// <summary>
  233.         /// Gets a random firing delay between MIN_FIRING_DELAY and
  234.         /// MIN_FIRING_DELY + FIRING_RATE_RANGE
  235.         /// </summary>
  236.         /// <returns>the random firing delay</returns>
  237.         private int GetRandomFiringDelay()
  238.         {
  239.             return GameConstants.BearMinFiringDelay +
  240.                 RandomNumberGenerator.Next(GameConstants.BearFiringRateRange);
  241.         }
  242.  
  243.         /// <summary>
  244.         /// Gets the y velocity for the projectile being fired
  245.         /// </summary>
  246.         /// <returns>the projectile y velocity</returns>
  247.         private float GetProjectileYVelocity()
  248.         {
  249.             if (velocity.Y > 0)
  250.             {
  251.                 return velocity.Y + GameConstants.TeddyBearProjectileSpeed;
  252.             }
  253.             else
  254.             {
  255.                 return GameConstants.TeddyBearProjectileSpeed;
  256.             }
  257.         }
  258.  
  259.         #endregion
  260.     }
  261. }
Add Comment
Please, Sign In to add comment