Advertisement
Guest User

Sprite.cs

a guest
Sep 5th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Content;
  4.  
  5. namespace Gamecodeur_JAM_1
  6. {
  7.     public class Sprite
  8.     {
  9.         // VARIABLE
  10.         private Texture2D texture;
  11.         private Rectangle destinationRectangle;
  12.         private Rectangle sourceRectangle;
  13.         private Color color;
  14.         private Vector2 origin;
  15.         private Point currentFrame;
  16.  
  17.         private SpriteEffects effect;
  18.  
  19.         private float rotation;
  20.  
  21.         private int spriteWidth;
  22.         private int spriteHeight;
  23.  
  24.         // GETTER & SETTERS
  25.         public void SetCurrentFrame(int x, int y)
  26.         {
  27.             this.UpdateCurrentFrame();
  28.         }
  29.  
  30.         // CONSTRUCTOR
  31.         public Sprite(string imgKey, int x, int y, int columns = 1, int rows = 1, int frameX = 0, int frameY = 0)
  32.         {
  33.             this.texture = Resources.Images[imgKey];
  34.  
  35.             int textureWidth = this.texture.Width;
  36.             int textureHeight = this.texture.Height;
  37.  
  38.             this.spriteWidth = textureWidth / columns;
  39.             this.spriteHeight = textureHeight / rows;
  40.  
  41.             this.destinationRectangle = new Rectangle(x, y, this.spriteWidth, this.spriteHeight);
  42.  
  43.             this.currentFrame = new Point(frameX, frameY);
  44.  
  45.             this.sourceRectangle = new Rectangle(this.currentFrame.X * this.spriteWidth, this.currentFrame.Y * this.spriteHeight, this.spriteWidth, this.spriteHeight);
  46.  
  47.             this.color = Color.White;
  48.             this.rotation = 0.0f;
  49.             this.origin = Vector2.Zero;
  50.             this.effect = SpriteEffects.None;
  51.         }
  52.         // UPDATE & DRAW
  53.  
  54.         public void Update(int x, int y)
  55.         {
  56.             this.UpdatePosition(x, y);
  57.         }
  58.  
  59.         public void Draw(SpriteBatch spriteBatch)
  60.         {
  61.             spriteBatch.Draw(this.texture, this.destinationRectangle, this.sourceRectangle, this.color, this.rotation, this.origin, this.effect, 0f);
  62.         }
  63.  
  64.         // FONCTION
  65.  
  66.         public void UpdateCurrentFrame()
  67.         {
  68.             this.sourceRectangle.X = this.currentFrame.X * this.spriteWidth;
  69.             this.sourceRectangle.Y = this.currentFrame.Y * this.spriteHeight;
  70.         }
  71.  
  72.         public void UpdatePosition(int x, int y)
  73.         {
  74.             this.destinationRectangle.X = x;
  75.             this.destinationRectangle.Y = y;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement