git-svn-id: https://bd85.net/svn/cs3505_group@163 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / AnimatedTexture.cs
1 ´╗┐using System;
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;
7
8 namespace CarFire
9 {
10 public class AnimatedTexture
11 {
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;
18
19 public float Rotation, Scale, Depth;
20 public Vector2 Origin;
21
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)
24 {
25 this.Origin = Origin;
26 this.Rotation = Rotation;
27 this.Scale = Scale;
28 this.Depth = Depth;
29 }
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)
33 {
34 framecount = FrameCount;
35 myTexture = content.Load<Texture2D>(asset);
36 TimePerFrame = (float)1 / FramesPerSec;
37 Frame = 0;
38 TotalElapsed = 0;
39 Paused = false;
40 loop = Loop;
41 Row = row;
42 }
43
44 // class AnimatedTexture
45 public void UpdateFrame(float elapsed)
46 {
47 if (Paused)
48 return;
49 TotalElapsed += elapsed;
50 if (TotalElapsed > TimePerFrame)
51 {
52 Frame++;
53 // Keep the Frame between 0 and the total frames, minus one.
54 Frame = Frame % framecount;
55 TotalElapsed -= TimePerFrame;
56 }
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)
60 Pause();
61
62 }
63
64 // class AnimatedTexture
65 public void DrawFrame(SpriteBatch Batch, Vector2 screenpos)
66 {
67 DrawFrame(Batch, Frame, screenpos);
68 }
69 public void DrawFrame(SpriteBatch Batch, int frame, Vector2 screenpos)
70 {
71
72 int FrameWidth = myTexture.Width / framecount;
73 Rectangle sourcerect = new Rectangle(FrameWidth * Frame, Row * 64,
74 FrameWidth, 64);
75 Batch.Draw(myTexture, screenpos, sourcerect, Color.White,
76 Rotation, Origin, Scale, SpriteEffects.None, Depth);
77 }
78
79 public bool IsPaused
80 {
81 get { return Paused; }
82 }
83 public void Reset()
84 {
85 Frame = 0;
86 TotalElapsed = 0f;
87 }
88 public void Stop()
89 {
90 Pause();
91 Reset();
92 }
93 public void Play()
94 {
95 Paused = false;
96 }
97 public void Pause()
98 {
99 Paused = true;
100
101 }
102
103 }
104 }
This page took 0.044504 seconds and 4 git commands to generate.