2 using System.Collections.Generic;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Content;
5 using Microsoft.Xna.Framework.Graphics;
6 using Microsoft.Xna.Framework.Input;
10 public class AnimatedTexture
12 private int framecount;
13 private Texture2D myTexture;
14 private float TimePerFrame;
15 private int Frame, Row;
16 private float TotalElapsed;
17 private bool Paused, loop;
19 public float Rotation, Scale, Depth;
20 public Vector2 Origin;
22 //rotation is pretty much always zero, scale determines size, depth is 0.5f
23 public AnimatedTexture(Vector2 Origin, float Rotation, float Scale, float Depth)
26 this.Rotation = Rotation;
30 //asset is the file name w/o the extension, framecount is the # of frames per row, framespersec determines speed of the animation
31 //loop will determine if the animation repeats, row chooses which row of the animation to play starting with 0
32 public void Load(GraphicsDevice device, ContentManager content, string asset, int FrameCount, int FramesPerSec, bool Loop, int row)
34 framecount = FrameCount;
35 myTexture = content.Load<Texture2D>(asset);
36 TimePerFrame = (float)1 / FramesPerSec;
44 // class AnimatedTexture
45 public void UpdateFrame(float elapsed)
49 TotalElapsed += elapsed;
50 if (TotalElapsed > TimePerFrame)
53 // Keep the Frame between 0 and the total frames, minus one.
54 Frame = Frame % framecount;
55 TotalElapsed -= TimePerFrame;
57 //If loop is false and the current Frame is the last frame
58 //then stop playing the animation
59 if ((Frame == framecount - 1) && loop == false)
64 // class AnimatedTexture
65 public void DrawFrame(SpriteBatch Batch, Vector2 screenpos)
67 DrawFrame(Batch, Frame, screenpos);
69 public void DrawFrame(SpriteBatch Batch, int frame, Vector2 screenpos)
72 int FrameWidth = myTexture.Width / framecount;
73 Rectangle sourcerect = new Rectangle(FrameWidth * Frame, Row * 64,
75 Batch.Draw(myTexture, screenpos, sourcerect, Color.White,
76 Rotation, Origin, Scale, SpriteEffects.None, Depth);
81 get { return Paused; }