]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/AnimatedTexture.cs
git-svn-id: https://bd85.net/svn/cs3505_group@168 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(TimeSpan timeSpan)
46 {
47 float elapsed = timeSpan.Milliseconds;
48 if (Paused)
49 return;
50 TotalElapsed += elapsed;
51 if (TotalElapsed > TimePerFrame)
52 {
53 Frame++;
54 // Keep the Frame between 0 and the total frames, minus one.
55 if (framecount != 0)
56 Frame = Frame % framecount;
57 else
58 Console.WriteLine("AHHH!");
59 TotalElapsed -= TimePerFrame;
60 }
61 //If loop is false and the current Frame is the last frame
62 //then stop playing the animation
63 if ((Frame == framecount - 1) && loop == false)
64 Pause();
65
66 }
67
68 // class AnimatedTexture
69 public void DrawFrame(SpriteBatch Batch, Vector2 screenpos)
70 {
71 DrawFrame(Batch, Frame, screenpos);
72 }
73 public void DrawFrame(SpriteBatch Batch, int frame, Vector2 screenpos)
74 {
75
76 int FrameWidth = myTexture.Width / framecount;
77 Rectangle sourcerect = new Rectangle(FrameWidth * Frame, Row * 64,
78 FrameWidth, 64);
79 Console.WriteLine(Frame);
80 Batch.Draw(myTexture, screenpos, sourcerect, Color.White,
81 Rotation, Origin, Scale, SpriteEffects.None, Depth);
82 }
83
84 public bool IsPaused
85 {
86 get { return Paused; }
87 }
88 public void Reset()
89 {
90 Frame = 0;
91 TotalElapsed = 0f;
92 }
93 public void Stop()
94 {
95 Pause();
96 Reset();
97 }
98 public void Play()
99 {
100 Paused = false;
101 }
102 public void Pause()
103 {
104 Paused = true;
105
106 }
107
108 }
109 }
This page took 0.037004 seconds and 4 git commands to generate.