Advertisement
diliupg

Programming Assignment 4-TeddyBear,cs

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