Advertisement
diliupg

Burger.cs from Game Project

Nov 11th, 2017
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.79 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. using Microsoft.Xna.Framework.Input;
  11.  
  12. namespace GameProject
  13. {
  14.     /// <summary>
  15.     /// A burger
  16.     /// </summary>
  17.     public class Burger
  18.     {
  19.         #region Fields
  20.  
  21.         // graphic and drawing info
  22.         Texture2D sprite;
  23.         Rectangle drawRectangle;
  24.  
  25.         // burger stats
  26.         int health = 100;
  27.  
  28.         // shooting support
  29.         bool canShoot = true;
  30.         int elapsedCooldownMilliseconds = 0;
  31.  
  32.         // sound effect
  33.         SoundEffect shootSound;
  34.  
  35.         #endregion
  36.  
  37.         #region Constructors
  38.  
  39.         /// <summary>
  40.         ///  Constructs a burger
  41.         /// </summary>
  42.         /// <param name="contentManager">the content manager for loading content</param>
  43.         /// <param name="spriteName">the sprite name</param>
  44.         /// <param name="x">the x location of the center of the burger</param>
  45.         /// <param name="y">the y location of the center of the burger</param>
  46.         /// <param name="shootSound">the sound the burger plays when shooting</param>
  47.         public Burger(ContentManager contentManager, string spriteName, int x, int y,
  48.             SoundEffect shootSound)
  49.         {
  50.             LoadContent(contentManager, spriteName, x, y);
  51.             this.shootSound = shootSound;
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         #region Properties
  57.  
  58.         /// <summary>
  59.         /// Gets the collision rectangle for the burger
  60.         /// </summary>
  61.         public Rectangle CollisionRectangle
  62.         {
  63.             get { return drawRectangle; }
  64.         }
  65.  
  66.         public int Health
  67.         {
  68.             get { return health; }
  69.             set
  70.             {
  71.                 health = value;
  72.                 if (health <= 0)
  73.                 {
  74.                     health = 0;
  75.                 }
  76.             }
  77.         }
  78.  
  79.  
  80.  
  81.         #endregion
  82.  
  83.  
  84.         #region Public methods
  85.  
  86.         /// <summary>
  87.         /// Updates the burger's location based on mouse. Also fires
  88.         /// french fries as appropriate
  89.         /// </summary>
  90.         /// <param name="gameTime">game time</param>
  91.         /// <param name="mouse">the current state of the mouse</param>
  92.         //public void Update(GameTime gameTime, MouseState mouse)
  93.         public void Update(GameTime gameTime, KeyboardState keyboard)
  94.         {
  95.             // burger should only respond to input if it still has health
  96.             if (health > 0)
  97.             {
  98.                 // move burger using mouse
  99.                 //drawRectangle.X = mouse.X - drawRectangle.Width / 2;
  100.                 //drawRectangle.Y = mouse.Y - drawRectangle.Height / 2;
  101.  
  102.                 // move burger with WASD keys
  103.                 // move the fish based on the keyboard state
  104.                 if (keyboard.IsKeyDown(Keys.D))
  105.                 {
  106.                     drawRectangle.X += GameConstants.BurgerMovementAmount;
  107.                 }
  108.                 if (keyboard.IsKeyDown(Keys.A))
  109.                 {
  110.                     drawRectangle.X -= GameConstants.BurgerMovementAmount;
  111.                 }
  112.                 if (keyboard.IsKeyDown(Keys.W))
  113.                 {
  114.                     drawRectangle.Y -= GameConstants.BurgerMovementAmount; ;
  115.                 }
  116.                 if (keyboard.IsKeyDown(Keys.S))
  117.                 {
  118.                     drawRectangle.Y += GameConstants.BurgerMovementAmount; ;
  119.                 }
  120.  
  121.  
  122.             }
  123.             // clamp burger in window
  124.             if (drawRectangle.Top < 0)
  125.             {
  126.                 drawRectangle.Y = 0;
  127.             }
  128.             else if ((drawRectangle.Bottom) > GameConstants.WindowHeight)
  129.             {
  130.                 // bounce off bottom
  131.                 drawRectangle.Y = GameConstants.WindowHeight - drawRectangle.Height;
  132.             }
  133.             if (drawRectangle.Left < 0)
  134.             {
  135.                 // bounc off left
  136.                 drawRectangle.X = 0;
  137.             }
  138.             else if ((drawRectangle.Right) > GameConstants.WindowWidth)
  139.             {
  140.                 // bounce off right
  141.                 drawRectangle.X = GameConstants.WindowWidth - drawRectangle.Width;
  142.             }
  143.  
  144.             // update shooting allowed
  145.             if (canShoot == false)
  146.             {
  147.                 elapsedCooldownMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  148.             }
  149.             if (elapsedCooldownMilliseconds > GameConstants.BurgerTotalCooldownMilliseconds)
  150.             {
  151.                 canShoot = true;
  152.                 elapsedCooldownMilliseconds = 0;
  153.             }
  154.  
  155.             if (health > 0 && keyboard.IsKeyDown(Keys.Space) && canShoot)
  156.             {
  157.                 canShoot = false;
  158.  
  159.                 Projectile projectile = new Projectile(ProjectileType.FrenchFries,
  160.                     Game1.GetProjectileSprite(ProjectileType.FrenchFries),
  161.                     drawRectangle.X + drawRectangle.Width / 2,
  162.                     drawRectangle.Y - GameConstants.FrenchFriesProjectileOffset,
  163.                     GameConstants.FrenchFriesProjectileSpeed * Convert.ToSingle(Math.Sin(-90)));
  164.  
  165.                 this.shootSound.Play();
  166.                 Game1.AddProjectile(projectile);
  167.             }
  168.  
  169.             //if (mouse.LeftButton == ButtonState.Released)
  170.             if (keyboard.IsKeyUp(Keys.Space))
  171.             {
  172.                 canShoot = true;
  173.             }
  174.             // timer concept (for animations) introduced in Chapter 7
  175.  
  176.             // shoot if appropriate
  177.  
  178.         }
  179.  
  180.         /// <summary>
  181.         /// Draws the burger
  182.         /// </summary>
  183.         /// <param name="spriteBatch">the sprite batch to use</param>
  184.         public void Draw(SpriteBatch spriteBatch)
  185.         {
  186.             spriteBatch.Draw( sprite, drawRectangle, Color.White );
  187.         }
  188.  
  189.         #endregion
  190.  
  191.         #region Private methods
  192.  
  193.         /// <summary>
  194.         /// Loads the content for the burger
  195.         /// </summary>
  196.         /// <param name="contentManager">the content manager to use</param>
  197.         /// <param name="spriteName">the name of the sprite for the burger</param>
  198.         /// <param name="x">the x location of the center of the burger</param>
  199.         /// <param name="y">the y location of the center of the burger</param>
  200.         private void LoadContent(ContentManager contentManager, string spriteName,
  201.             int x, int y)
  202.         {
  203.             // load content and set remainder of draw rectangle
  204.             sprite = contentManager.Load<Texture2D>(spriteName);
  205.             drawRectangle = new Rectangle(x - sprite.Width / 2,
  206.                 y - sprite.Height / 2, sprite.Width,
  207.                 sprite.Height);
  208.         }
  209.  
  210.         #endregion
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement