handle PingPong animations
This commit is contained in:
parent
0b8eb3e3f1
commit
5b82f4d646
@ -16,15 +16,22 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum AnimationDirection {
|
||||||
|
Forward,
|
||||||
|
PingPong
|
||||||
|
}
|
||||||
|
|
||||||
struct SpriteAnimation {
|
struct SpriteAnimation {
|
||||||
public readonly int Start;
|
public readonly int Start;
|
||||||
public readonly int End;
|
public readonly int End;
|
||||||
public readonly int DurationMs;
|
public readonly int DurationMs;
|
||||||
|
public readonly AnimationDirection Direction;
|
||||||
|
|
||||||
public SpriteAnimation(int start, int end, int durationMs) {
|
public SpriteAnimation(int start, int end, int durationMs, AnimationDirection direction) {
|
||||||
Start = start;
|
Start = start;
|
||||||
End = end;
|
End = end;
|
||||||
DurationMs = durationMs;
|
DurationMs = durationMs;
|
||||||
|
Direction = direction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,17 +68,25 @@ namespace SemiColinGames {
|
|||||||
frames.Add(new Frame(source, durationMs));
|
frames.Add(new Frame(source, durationMs));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle ping-pong animations.
|
|
||||||
JToken frameTags = json.SelectToken("meta.frameTags");
|
JToken frameTags = json.SelectToken("meta.frameTags");
|
||||||
foreach (JToken child in frameTags.Children()) {
|
foreach (JToken child in frameTags.Children()) {
|
||||||
string name = child.SelectToken("name").Value<string>();
|
string name = child.SelectToken("name").Value<string>();
|
||||||
int start = child.SelectToken("from").Value<int>();
|
int start = child.SelectToken("from").Value<int>();
|
||||||
int end = child.SelectToken("to").Value<int>();
|
int end = child.SelectToken("to").Value<int>();
|
||||||
|
string directionString = child.SelectToken("direction").Value<string>();
|
||||||
|
AnimationDirection direction = directionString == "pingpong" ?
|
||||||
|
AnimationDirection.PingPong: AnimationDirection.Forward;
|
||||||
int durationMs = 0;
|
int durationMs = 0;
|
||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i <= end; i++) {
|
||||||
durationMs += frames[i].DurationMs;
|
durationMs += frames[i].DurationMs;
|
||||||
}
|
}
|
||||||
animations[name] = new SpriteAnimation(start, end, durationMs);
|
// A PingPong animation repeats every frame but the first and last.
|
||||||
|
// Therefore its duration is 2x, minus the duration of the first and last frames.
|
||||||
|
if (direction == AnimationDirection.PingPong) {
|
||||||
|
durationMs = durationMs * 2 - frames[start].DurationMs - frames[end].DurationMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
animations[name] = new SpriteAnimation(start, end, durationMs, direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,6 +100,15 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
time -= frameTime;
|
time -= frameTime;
|
||||||
}
|
}
|
||||||
|
if (animation.Direction == AnimationDirection.PingPong) {
|
||||||
|
for (int i = animation.End - 1; i > animation.Start; i--) {
|
||||||
|
int frameTime = frames[i].DurationMs;
|
||||||
|
if (time < frameTime) {
|
||||||
|
return frames[i].Source;
|
||||||
|
}
|
||||||
|
time -= frameTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
// We shouldn't get here, but if we did, the last frame is a fine thing to return.
|
// We shouldn't get here, but if we did, the last frame is a fine thing to return.
|
||||||
return frames[animation.End].Source;
|
return frames[animation.End].Source;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user