--- /dev/null
+using System;\r
+using System.Collections.Generic;\r
+using Microsoft.Xna.Framework;\r
+using Microsoft.Xna.Framework.Content;\r
+using Microsoft.Xna.Framework.Graphics;\r
+using Microsoft.Xna.Framework.Input;\r
+\r
+namespace CarFire\r
+{\r
+ public class AnimatedTexture\r
+ {\r
+ private int framecount;\r
+ private Texture2D myTexture;\r
+ private float TimePerFrame;\r
+ private int Frame, Row;\r
+ private float TotalElapsed;\r
+ private bool Paused, loop;\r
+\r
+ public float Rotation, Scale, Depth;\r
+ public Vector2 Origin;\r
+\r
+ //rotation is pretty much always zero, scale determines size, depth is 0.5f\r
+ public AnimatedTexture(Vector2 Origin, float Rotation, float Scale, float Depth)\r
+ {\r
+ this.Origin = Origin;\r
+ this.Rotation = Rotation;\r
+ this.Scale = Scale;\r
+ this.Depth = Depth;\r
+ }\r
+ //asset is the file name w/o the extension, framecount is the # of frames per row, framespersec determines speed of the animation\r
+ //loop will determine if the animation repeats, row chooses which row of the animation to play starting with 0\r
+ public void Load(GraphicsDevice device, ContentManager content, string asset, int FrameCount, int FramesPerSec, bool Loop, int row)\r
+ {\r
+ framecount = FrameCount;\r
+ myTexture = content.Load<Texture2D>(asset);\r
+ TimePerFrame = (float)1 / FramesPerSec;\r
+ Frame = 0;\r
+ TotalElapsed = 0;\r
+ Paused = false;\r
+ loop = Loop;\r
+ Row = row;\r
+ }\r
+\r
+ // class AnimatedTexture\r
+ public void UpdateFrame(float elapsed)\r
+ {\r
+ if (Paused)\r
+ return;\r
+ TotalElapsed += elapsed;\r
+ if (TotalElapsed > TimePerFrame)\r
+ {\r
+ Frame++;\r
+ // Keep the Frame between 0 and the total frames, minus one.\r
+ Frame = Frame % framecount;\r
+ TotalElapsed -= TimePerFrame;\r
+ }\r
+ //If loop is false and the current Frame is the last frame \r
+ //then stop playing the animation\r
+ if ((Frame == framecount - 1) && loop == false)\r
+ Pause();\r
+\r
+ }\r
+\r
+ // class AnimatedTexture\r
+ public void DrawFrame(SpriteBatch Batch, Vector2 screenpos)\r
+ {\r
+ DrawFrame(Batch, Frame, screenpos);\r
+ }\r
+ public void DrawFrame(SpriteBatch Batch, int frame, Vector2 screenpos)\r
+ {\r
+\r
+ int FrameWidth = myTexture.Width / framecount;\r
+ Rectangle sourcerect = new Rectangle(FrameWidth * Frame, Row * 64,\r
+ FrameWidth, 64);\r
+ Batch.Draw(myTexture, screenpos, sourcerect, Color.White,\r
+ Rotation, Origin, Scale, SpriteEffects.None, Depth);\r
+ }\r
+\r
+ public bool IsPaused\r
+ {\r
+ get { return Paused; }\r
+ }\r
+ public void Reset()\r
+ {\r
+ Frame = 0;\r
+ TotalElapsed = 0f;\r
+ }\r
+ public void Stop()\r
+ {\r
+ Pause();\r
+ Reset();\r
+ }\r
+ public void Play()\r
+ {\r
+ Paused = false;\r
+ }\r
+ public void Pause()\r
+ {\r
+ Paused = true;\r
+\r
+ }\r
+\r
+ }\r
+}\r