TheGadgetCatTumblr

Sprite Class

Jan 23rd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace CatTetris
  7. {
  8.     using System;
  9.     using System.Collections.Generic;
  10.     using System.Linq;
  11.     using System.Text;
  12.     using Microsoft.Xna.Framework;
  13.     using Microsoft.Xna.Framework.Content;
  14.     using Microsoft.Xna.Framework.Graphics;
  15.  
  16.  
  17.         public class Sprite
  18.         {
  19.             public Vector2 Position = new Vector2(0, 0);
  20.             public Texture2D mSpriteTexture;
  21.             public string AssetName;
  22.             public Rectangle Size;
  23.             private float mScale = 1.0f;
  24.  
  25.             private Texture2D pixel;
  26.  
  27.             public Vector2 origin;
  28.  
  29.             public Sprite()
  30.             {
  31.  
  32.             }
  33.  
  34.             //Load content
  35.             public void LoadContent(ContentManager theContentManager, String theAssetName)
  36.             {
  37.                 mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
  38.                 AssetName = theAssetName;
  39.  
  40.                 pixel = theContentManager.Load<Texture2D>("pixel");
  41.  
  42.                 Size = new Rectangle((int)Position.X, (int)Position.Y, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
  43.             }
  44.  
  45.             public void Draw(SpriteBatch theSpriteBatch)
  46.             {
  47.                 theSpriteBatch.Draw(mSpriteTexture, Position,
  48.                     Size, Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
  49.            
  50.                 //draw a "pixel" texture at the origin of the sprite
  51.                 //theSpriteBatch.Draw(pixel, origin, new Rectangle(0,0,10,10), Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
  52.             }
  53.  
  54.             public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
  55.             {
  56.                 Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
  57.  
  58.                 //Update the origin of the sprite
  59.                 origin = new Vector2((int)(Position.X + mSpriteTexture.Width/2), (int)(Position.Y + mSpriteTexture.Height/2));
  60.             }
  61.  
  62.             public float Scale
  63.             {
  64.                 get { return mScale; }
  65.                 set
  66.                 {
  67.                     mScale = value;
  68.                     Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
  69.                 }
  70.             }
  71.             public Rectangle BoundingBox()
  72.             {
  73.                  return new Rectangle(
  74.                  (int)Position.X + 1,
  75.                  (int)Position.Y + 1,
  76.                  mSpriteTexture.Width + 1,
  77.                  mSpriteTexture.Height + 1);
  78.             }
  79.  
  80.          
  81.         }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment