Hyluss

[MonoGame] TextureAtlas

Oct 23rd, 2018
198
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework;
  7.  
  8. namespace TextureAtlas
  9. {
  10.     public class AnimatedSprite
  11.     {
  12.         public Texture2D Texture { get; set; }
  13.         public int Rows { get; set; }
  14.         public int Columns { get; set; }
  15.         private int currentFrame;
  16.         private int totalFrames;
  17.  
  18.         public AnimatedSprite(Texture2D texture, int rows, int columns)
  19.         {
  20.             Texture = texture;
  21.             Rows = rows;
  22.             Columns = columns;
  23.             currentFrame = 0;
  24.             totalFrames = Rows * Columns;
  25.         }
  26.  
  27.         public void Update()
  28.         {
  29.             currentFrame++;
  30.             if (currentFrame == totalFrames)
  31.                 currentFrame = 0;
  32.         }
  33.  
  34.         public void Draw(SpriteBatch spriteBatch, Vector2 location)
  35.         {
  36.             int width = Texture.Width / Columns;
  37.             int height = Texture.Height / Rows;
  38.             int row = (int)((float)currentFrame / (float)Columns);
  39.             int column = currentFrame % Columns;
  40.  
  41.             Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
  42.             Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
  43.  
  44.             spriteBatch.Begin();
  45.             spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
  46.             spriteBatch.End();
  47.         }
  48.     }
  49. }
Comments
  • User was banned
Add Comment
Please, Sign In to add comment