Advertisement
diliupg

ProgrammingAssignment3_Rock.cs

Sep 4th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 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.Graphics;
  8.  
  9. namespace ProgrammingAssignment3
  10. {
  11.     /// <summary>
  12.     /// A rock
  13.     /// </summary>
  14.     public class Rock
  15.     {
  16.         #region Fields
  17.  
  18.         // drawing support
  19.         Texture2D sprite;
  20.         Rectangle drawRectangle;
  21.  
  22.         // moving support
  23.         Vector2 velocity;
  24.  
  25.         // window containment support
  26.         int windowWidth;
  27.         int windowHeight;
  28.         bool outsideWindow = false;
  29.  
  30.         #endregion
  31.  
  32.         #region Constructors
  33.  
  34.         /// <summary>
  35.         /// Constructor
  36.         /// </summary>
  37.         /// <param name="sprite">sprite for the rock</param>
  38.         /// <param name="location">location of the center of the rock</param>
  39.         /// <param name="velocity">velocity of the rock</param>
  40.         /// <param name="windowWidth">window width</param>
  41.         /// <param name="windowHeight">window height</param>
  42.         public Rock(Texture2D sprite, Vector2 location, Vector2 velocity,
  43.             int windowWidth, int windowHeight)
  44.         {
  45.             // save window dimensions
  46.             this.windowWidth = windowWidth;
  47.             this.windowHeight = windowHeight;
  48.  
  49.             // save sprite and set draw rectangle
  50.             this.sprite = sprite;
  51.             drawRectangle = new Rectangle( (int)location.X - sprite.Width / 2,
  52.                 (int)location.Y - sprite.Height / 2, sprite.Width, sprite.Height );
  53.  
  54.             // save velocity
  55.             this.velocity = velocity;
  56.         }
  57.  
  58.         #endregion
  59.  
  60.         #region Properties
  61.  
  62.         /// <summary>
  63.         /// Sets the rock's velocity
  64.         /// </summary>
  65.         public Vector2 Velocity
  66.         {
  67.             set
  68.             {
  69.                 velocity.X = value.X;
  70.                 velocity.Y = value.Y;
  71.             }
  72.         }
  73.  
  74.         /// <summary>
  75.         /// Gets whether or not the rock is outside the window
  76.         /// </summary>
  77.         public bool OutsideWindow
  78.         {
  79.             get { return outsideWindow; }
  80.         }
  81.  
  82.         #endregion
  83.  
  84.         #region Methods
  85.  
  86.         /// <summary>
  87.         /// Updates the rock
  88.         /// </summary>
  89.         /// <param name="gameTime">game time</param>
  90.         public void Update(GameTime gameTime)
  91.         {
  92.             //Only update the rock if it's inside the window
  93.             if(drawRectangle.X < 0 || drawRectangle.X + drawRectangle.Width > windowWidth ||
  94.                 drawRectangle.Y < 0 || drawRectangle.Y + drawRectangle.Height > windowHeight)
  95.             {
  96.                 this.outsideWindow = true;
  97.             }
  98.             else
  99.             {
  100.                 this.outsideWindow = false;
  101.             }
  102.  
  103.             //Update the rock's location
  104.             if(outsideWindow == false)
  105.             {
  106.                 drawRectangle.X += (int)(velocity.X * gameTime.ElapsedGameTime.Milliseconds);
  107.                 drawRectangle.Y += (int)(velocity.Y * gameTime.ElapsedGameTime.Milliseconds);
  108.             }
  109.  
  110.         }
  111.  
  112.         /// <summary>
  113.         /// Draws the rock
  114.         /// </summary>
  115.         /// <param name="spriteBatch">sprite batch</param>
  116.         public void Draw(SpriteBatch spriteBatch)
  117.         {
  118.             // Only draw the rock if it's inside the window
  119.  
  120.             // Draw the rock
  121.             // Caution: Don't include spriteBatch.Begin or spriteBatch.End here
  122.             if (outsideWindow == false)
  123.             {
  124.                 spriteBatch.Draw( sprite, drawRectangle, Color.White );
  125.             }
  126.         }
  127.  
  128.         #endregion
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement