-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedTexture.cs
220 lines (206 loc) · 6.89 KB
/
AnimatedTexture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#region File Description
//-----------------------------------------------------------------------------
// AnimatedTexture.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace SAIYASASTRA
{
public class AnimatedTexture
{
private int framecount;
private Texture2D myTexture;
private float TimePerFrame;
public int Frame;
private int framerow = 1; // frame row
private int frame_r; // count frame row
private int endframe;
private float TotalElapsed;
private bool Paused;
private bool Ended;
private int Overload;
private int startrow;
private bool flip = false;
private int pauseFrame = -1;
private int pauseRow = -1;
public float Rotation, Scale, Depth;
public Vector2 Origin;
public AnimatedTexture(Vector2 origin, float rotation, float scale, float depth)
{
this.Origin = origin;
this.Rotation = rotation;
this.Scale = scale;
this.Depth = depth;
}
public void Load(ContentManager content, string asset, int frameCount,int frameRow, int framesPerSec)
{
framecount = frameCount;
framerow = frameRow;
endframe = (frameCount * framerow)-1;
myTexture = content.Load<Texture2D>(asset);
TimePerFrame = (float)1 / framesPerSec;
Frame = 0;
frame_r = 0;
TotalElapsed = 0;
Paused = false;
Ended = false;
Overload = 1;
}
public void Load(ContentManager content, string asset, int frameCount, int frameRow, int framesPerSec,int startRow)
{
framecount = frameCount;
framerow = frameRow;
endframe = (frameCount * framerow) - 1;
myTexture = content.Load<Texture2D>(asset);
TimePerFrame = (float)1 / framesPerSec;
Frame = 0;
frame_r = 0;
TotalElapsed = 0;
Paused = false;
Ended = false;
Overload = 2;
startrow = startRow;
}
// class AnimatedTexture
public void UpdateFrame(float elapsed)
{
if (pauseFrame > -1 && pauseRow > -1)
{
frame_r = pauseRow;
Frame = pauseFrame;
Paused = true;
pauseFrame = -1;
pauseRow = -1;
}
if (Paused)
return;
TotalElapsed += elapsed;
if (TotalElapsed > TimePerFrame)
{
Frame++;
if (Frame == framecount)
{
frame_r++;
if (Overload == 2)
{
Ended = true;
}
}
if (frame_r == framerow)
{
frame_r = 0;
if (Overload == 1)
{
Ended = true;
}
}
// Keep the Frame between 0 and the total frames, minus one.
Frame = Frame % framecount;
// check start check end
TotalElapsed -= TimePerFrame;
}
}
internal void Load(ContentManager content, string v, object frame, object framesRow, object framesPerSec)
{
throw new NotImplementedException();
}
// class AnimatedTexture
public void DrawFrame(SpriteBatch batch, Vector2 screenPos, bool flip)
{
this.flip = flip;
DrawFrame(batch, Frame, screenPos);
}
public void DrawFrame(SpriteBatch batch, Vector2 screenPos)
{
DrawFrame(batch, Frame, screenPos);
}
public void DrawFrame(SpriteBatch batch, Vector2 screenPos,int row)
{
DrawFrame(batch, Frame, screenPos,row);
}
public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos)
{
int FrameWidth = myTexture.Width / framecount;
int FrameHeight = 200 / framerow;
Rectangle sourcerect = new Rectangle();
if (Overload == 1)
{
sourcerect = new Rectangle(FrameWidth * frame, FrameHeight * frame_r,
FrameWidth, FrameHeight);
}
if (Overload == 2)
{
sourcerect = new Rectangle(FrameWidth * frame, FrameHeight * (startrow-1),
FrameWidth, FrameHeight);
}
if (flip == false)
{
batch.Draw(myTexture, screenPos, sourcerect, Color.White,
Rotation, Origin, Scale, SpriteEffects.None, Depth);
}
else
{
batch.Draw(myTexture, screenPos, sourcerect, Color.White,
Rotation, Origin, Scale, SpriteEffects.FlipHorizontally, Depth);
}
}
public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos,int row)
{
int FrameWidth = myTexture.Width / framecount;
int FrameHeight = myTexture.Height / framerow;
startrow = row;
Rectangle sourcerect = new Rectangle();
sourcerect = new Rectangle(FrameWidth * frame, FrameHeight * (startrow - 1),
FrameWidth, FrameHeight);
if (flip == false)
{
batch.Draw(myTexture, screenPos, sourcerect, Color.White,
Rotation, Origin, Scale, SpriteEffects.None, Depth);
}
else
{
batch.Draw(myTexture, screenPos, sourcerect, Color.White,
Rotation, Origin, Scale, SpriteEffects.FlipHorizontally, Depth);
}
}
public bool IsPaused
{
get { return Paused; }
}
public bool IsEnd
{
get { return Ended; }
}
public void Reset()
{
Frame = 0;
TotalElapsed = 0f;
}
public void Stop()
{
Pause();
Reset();
}
public void Play()
{
Paused = false;
}
public void Pause()
{
Paused = true;
}
public void Pause(int frame,int row)
{
this.pauseFrame = frame;
this.pauseRow = row;
}
}
}