-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedSprite.cs
71 lines (61 loc) · 2.18 KB
/
AnimatedSprite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace SpaceAdventure
{
public class AnimatedSprite
{
public Texture2D texture { get; set; } //this is the texture atlas
public int Rows { get; set; }
public int Columns { get; set; }
private int currentFrame;
private int totalFrames;
private int frameDelay { get; set; }
private int frameCounter { get; set; }
public int numberOfLoops { get; private set; }
public AnimatedSprite(Texture2D texture, int rows, int columns, int totalFrames, int frameDelay)
{
this.texture = texture;
this.Rows = rows;
this.Columns = columns;
this.totalFrames = totalFrames;
this.frameDelay = frameDelay;
this.frameCounter = 0;
this.numberOfLoops = 0;
}
public void Update()
{
frameCounter++;
if (frameCounter >= frameDelay)
{
currentFrame++;
frameCounter = 0;
}
if (currentFrame == totalFrames)
{
currentFrame = 0;
numberOfLoops++;
}
}
public void resetNumberOfLoops()
{
numberOfLoops = 0;
}
public void Draw(SpriteBatch spriteBatch, Vector2 location, Color tint)
{
int width = texture.Width / Columns;
int height = texture.Height / Rows;
int row = (int)((float)currentFrame/(float)Columns);
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
//If Game1.cs is already starting and ending the spriteBatch, no need to start them here
//spriteBatch.Begin();
spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, tint);
//spriteBatch.End();
}
}
}