Troy_Martin

Programming Assignment 4 - Teddy

Jan 6th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 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. using Microsoft.Xna.Framework.Input;
  9.  
  10. namespace ProgrammingAssignment4
  11. {
  12.     /// <summary>
  13.     /// A teddy bear
  14.     /// </summary>
  15.     public class TeddyBear
  16.     {
  17.         #region Fields
  18.  
  19.         // collecting support
  20.         bool collecting = false;
  21.         bool targetSet = false;
  22.  
  23.         // drawing support
  24.         Texture2D sprite;
  25.         Rectangle drawRectangle;
  26.         int halfDrawRectangleWidth;
  27.         int halfDrawRectangleHeight;
  28.  
  29.         // moving support
  30.         const float BaseSpeed = 0.3f;
  31.         Vector2 location;
  32.         Vector2 velocity = Vector2.Zero;
  33.  
  34.         // click processing
  35.         bool leftClickStarted = false;
  36.         bool leftButtonReleased = true;
  37.  
  38.         #endregion
  39.  
  40.         #region Constructors
  41.  
  42.         /// <summary>
  43.         /// Constructor
  44.         /// </summary>
  45.         /// <param name="sprite">sprite for the teddy</param>
  46.         /// <param name="location">location of the center of the teddy</param>
  47.         public TeddyBear(Texture2D sprite, Vector2 location)
  48.         {
  49.             this.sprite = sprite;
  50.             this.location = location;
  51.  
  52.             // STUDENTS: set draw rectangle so teddy is centered on location
  53.             drawRectangle = new Rectangle((int)location.X, (int)location.Y, sprite.Width, sprite.Height);
  54.  
  55.             // STUDENTS: set halfDrawRectangleWidth and halfDrawRectangleHeight for efficiency
  56.             halfDrawRectangleHeight = drawRectangle.Height / 2;
  57.             halfDrawRectangleWidth = drawRectangle.Width / 2;
  58.         }
  59.  
  60.         #endregion
  61.  
  62.         #region Properties
  63.  
  64.         /// <summary>
  65.         /// Gets and sets whether or not the teddy is collecting
  66.         /// </summary>
  67.         public bool Collecting
  68.         {
  69.             get { return collecting; }
  70.             set { collecting = value; }
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Gets the collision rectangle for the teddy
  75.         /// </summary>
  76.         public Rectangle CollisionRectangle
  77.         {
  78.             get { return drawRectangle; }
  79.         }
  80.  
  81.         #endregion
  82.  
  83.         #region Public methods
  84.  
  85.         /// <summary>
  86.         /// Updates the teddy
  87.         /// </summary>
  88.         /// <param name="gameTime">game time</param>
  89.         /// <param name="mouse">current mouse state</param>
  90.         public void Update(GameTime gameTime, MouseState mouse)
  91.         {
  92.             // STUDENTS: update location based on velocity if teddy is collecting
  93.             // Be sure to update the location field first, then center the
  94.             // draw rectangle on the location
  95.             if (collecting)
  96.             {
  97.                 location.X += velocity.X * gameTime.ElapsedGameTime.Milliseconds;
  98.                 location.Y += velocity.Y * gameTime.ElapsedGameTime.Milliseconds;
  99.                 drawRectangle = new Rectangle((int)location.X, (int)location.Y, sprite.Width, sprite.Height);
  100.             }
  101.  
  102.             // check for mouse over teddy
  103.             if (drawRectangle.Contains(mouse.X, mouse.Y))
  104.             {
  105.                 // check for left click started on teddy
  106.                 if (mouse.LeftButton == ButtonState.Pressed &&
  107.                     leftButtonReleased)
  108.                 {
  109.                     leftClickStarted = true;
  110.                     leftButtonReleased = false;
  111.                 }
  112.                 else if (mouse.LeftButton == ButtonState.Released)
  113.                 {
  114.                     leftButtonReleased = true;
  115.  
  116.                     // if click finished on teddy, start collecting if target set
  117.                     if (leftClickStarted)
  118.                     {
  119.                         if (targetSet)
  120.                         {
  121.                             collecting = true;
  122.                         }
  123.                         leftClickStarted = false;
  124.                     }
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 // no clicking on teddy
  130.                 leftClickStarted = false;
  131.                 leftButtonReleased = false;
  132.             }
  133.         }
  134.  
  135.         /// <summary>
  136.         /// Draws the teddy
  137.         /// </summary>
  138.         /// <param name="spriteBatch">sprite batch</param>
  139.         public void Draw(SpriteBatch spriteBatch)
  140.         {
  141.             // STUDENTS: use the sprite batch to draw the teddy
  142.             spriteBatch.Draw(sprite, drawRectangle, Color.White);
  143.         }
  144.  
  145.         /// <summary>
  146.         /// Sets a target for the teddy to move toward
  147.         /// </summary>
  148.         /// <param name="target">target</param>
  149.         public void SetTarget(Vector2 target)
  150.         {
  151.             targetSet = true;
  152.  
  153.             // STUDENTS: set teddy velocity based on teddy center location and target
  154.             float velocityX = target.X - location.X - halfDrawRectangleWidth;
  155.             float velocityY = target.Y - location.Y - halfDrawRectangleHeight;
  156.             velocity = new Vector2(velocityX, velocityY);
  157.             velocity = Vector2.Normalize(velocity);
  158.             velocity = velocity * BaseSpeed;
  159.         }
  160.  
  161.         /// <summary>
  162.         /// Clears the target for the teddy (it no longer has a target)
  163.         /// </summary>
  164.         public void ClearTarget()
  165.         {
  166.             targetSet = false;
  167.         }
  168.  
  169.         #endregion
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment