Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Content;
- namespace Gamecodeur_JAM_1
- {
- public class Sprite
- {
- // VARIABLE
- private Texture2D texture;
- private Rectangle destinationRectangle;
- private Rectangle sourceRectangle;
- private Color color;
- private Vector2 origin;
- private Point currentFrame;
- private SpriteEffects effect;
- private float rotation;
- private int spriteWidth;
- private int spriteHeight;
- // GETTER & SETTERS
- public void SetCurrentFrame(int x, int y)
- {
- this.UpdateCurrentFrame();
- }
- // CONSTRUCTOR
- public Sprite(string imgKey, int x, int y, int columns = 1, int rows = 1, int frameX = 0, int frameY = 0)
- {
- this.texture = Resources.Images[imgKey];
- int textureWidth = this.texture.Width;
- int textureHeight = this.texture.Height;
- this.spriteWidth = textureWidth / columns;
- this.spriteHeight = textureHeight / rows;
- this.destinationRectangle = new Rectangle(x, y, this.spriteWidth, this.spriteHeight);
- this.currentFrame = new Point(frameX, frameY);
- this.sourceRectangle = new Rectangle(this.currentFrame.X * this.spriteWidth, this.currentFrame.Y * this.spriteHeight, this.spriteWidth, this.spriteHeight);
- this.color = Color.White;
- this.rotation = 0.0f;
- this.origin = Vector2.Zero;
- this.effect = SpriteEffects.None;
- }
- // UPDATE & DRAW
- public void Update(int x, int y)
- {
- this.UpdatePosition(x, y);
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(this.texture, this.destinationRectangle, this.sourceRectangle, this.color, this.rotation, this.origin, this.effect, 0f);
- }
- // FONCTION
- public void UpdateCurrentFrame()
- {
- this.sourceRectangle.X = this.currentFrame.X * this.spriteWidth;
- this.sourceRectangle.Y = this.currentFrame.Y * this.spriteHeight;
- }
- public void UpdatePosition(int x, int y)
- {
- this.destinationRectangle.X = x;
- this.destinationRectangle.Y = y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement