Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 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 - sprite.Width / 2, (int)location.Y - sprite.Height / 2, sprite.Width, sprite.Height);
  54.  
  55.             // +STUDENTS: set halfDrawRectangleWidth and halfDrawRectangleHeight for efficiency
  56.             halfDrawRectangleWidth = drawRectangle.Width / 2;
  57.             halfDrawRectangleHeight = drawRectangle.Height / 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.X = (int)location.X - halfDrawRectangleWidth;
  100.                 drawRectangle.Y = (int)location.Y - halfDrawRectangleHeight;
  101.             }
  102.            
  103.             // check for mouse over teddy
  104.             if (drawRectangle.Contains(mouse.X, mouse.Y))
  105.             {
  106.                 // check for left click started on teddy
  107.                 if (mouse.LeftButton == ButtonState.Pressed &&
  108.                     leftButtonReleased)
  109.                 {
  110.                     leftClickStarted = true;
  111.                     leftButtonReleased = false;
  112.                 }
  113.                 else if (mouse.LeftButton == ButtonState.Released)
  114.                 {
  115.                     leftButtonReleased = true;
  116.  
  117.                     // if click finished on teddy, start collecting if target set
  118.                     if (leftClickStarted)
  119.                     {
  120.                         if (targetSet)
  121.                         {
  122.                             collecting = true;
  123.                         }
  124.                         leftClickStarted = false;
  125.                     }
  126.                 }
  127.             }
  128.             else
  129.             {
  130.                 // no clicking on teddy
  131.                 leftClickStarted = false;
  132.                 leftButtonReleased = false;
  133.             }
  134.         }
  135.  
  136.         /// <summary>
  137.         /// Draws the teddy
  138.         /// </summary>
  139.         /// <param name="spriteBatch">sprite batch</param>
  140.         public void Draw(SpriteBatch spriteBatch)
  141.         {
  142.             // +STUDENTS: use the sprite batch to draw the teddy
  143.             spriteBatch.Draw(sprite, drawRectangle, Color.White);
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Sets a target for the teddy to move toward
  148.         /// </summary>
  149.         /// <param name="target">target</param>
  150.         public void SetTarget(Vector2 target)
  151.         {
  152.             targetSet = true;
  153.  
  154.             // +STUDENTS: set teddy velocity based on teddy center location and target
  155.             velocity.X = BaseSpeed * (target.X - location.X) /
  156.                 (float)Math.Sqrt((target.X - location.X) * (target.X - location.X) + (target.Y - location.Y) * (target.Y - location.Y));
  157.             velocity.Y = BaseSpeed * (target.Y - location.Y) /
  158.                 (float)Math.Sqrt((target.X - location.X) * (target.X - location.X) + (target.Y - location.Y) * (target.Y - location.Y));
  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
Advertisement