]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/AnimatedTexture.cs
git-svn-id: https://bd85.net/svn/cs3505_group@163 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / AnimatedTexture.cs
diff --git a/CarFire/CarFire/CarFire/AnimatedTexture.cs b/CarFire/CarFire/CarFire/AnimatedTexture.cs
new file mode 100644 (file)
index 0000000..f4aab89
--- /dev/null
@@ -0,0 +1,104 @@
+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
This page took 0.018803 seconds and 4 git commands to generate.