change Sprite / Animation times to double (sec) instead of int (ms).

This commit is contained in:
Colin McMillen 2020-03-05 21:12:11 -05:00
parent 53083818c0
commit b574a5a702
3 changed files with 19 additions and 20 deletions

View File

@ -69,7 +69,7 @@ namespace SemiColinGames {
public void Draw(SpriteBatch spriteBatch) {
Rectangle textureSource = Sprites.Executioner.GetTextureSource(
fsm.StateName, (int) Clock.ModelTime.TotalMilliseconds);
fsm.StateName, Clock.ModelTime.TotalSeconds);
// TODO: move this into Sprite metadata.
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
SpriteEffects effect = Facing == 1 ?

View File

@ -232,7 +232,7 @@ namespace SemiColinGames {
}
private Rectangle GetTextureSource(Pose pose) {
int time = (int) Clock.ModelTime.TotalMilliseconds;
double time = Clock.ModelTime.TotalSeconds;
switch (pose) {
case Pose.Walking:
case Pose.Jumping:
@ -240,7 +240,7 @@ namespace SemiColinGames {
case Pose.SwordSwing:
// TODO: make a proper animation class & FSM-driven animations.
return Sprites.Ninja.GetTextureSource(
"attack_sword", (int) (1000 * (0.3 - swordSwingTime)));
"attack_sword", 0.3 - swordSwingTime);
case Pose.Crouching:
case Pose.Stretching:
case Pose.Standing:

View File

@ -24,24 +24,24 @@ namespace SemiColinGames {
struct SpriteAnimation {
public readonly int Start;
public readonly int End;
public readonly int DurationMs;
public readonly double Duration;
public readonly AnimationDirection Direction;
public SpriteAnimation(int start, int end, int durationMs, AnimationDirection direction) {
public SpriteAnimation(int start, int end, double duration, AnimationDirection direction) {
Start = start;
End = end;
DurationMs = durationMs;
Duration = duration;
Direction = direction;
}
}
struct Frame {
public readonly Rectangle Source;
public readonly int DurationMs;
public readonly double Duration;
public Frame(Rectangle source, int durationMs) {
public Frame(Rectangle source, double duration) {
Source = source;
DurationMs = durationMs;
Duration = duration;
}
}
@ -64,9 +64,8 @@ namespace SemiColinGames {
child.SelectToken("frame.y").Value<int>(),
child.SelectToken("frame.w").Value<int>(),
child.SelectToken("frame.h").Value<int>());
// TODO: convert all durations to floats.
int durationMs = child.SelectToken("duration").Value<int>();
frames.Add(new Frame(source, durationMs));
double duration = child.SelectToken("duration").Value<double>() / 1000;
frames.Add(new Frame(source, duration));
}
JToken frameTags = json.SelectToken("meta.frameTags");
@ -77,25 +76,25 @@ namespace SemiColinGames {
string directionString = child.SelectToken("direction").Value<string>();
AnimationDirection direction = directionString == "pingpong" ?
AnimationDirection.PingPong : AnimationDirection.Forward;
int durationMs = 0;
double duration = 0;
for (int i = start; i <= end; i++) {
durationMs += frames[i].DurationMs;
duration += frames[i].Duration;
}
// 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;
duration = duration * 2 - frames[start].Duration - frames[end].Duration;
}
animations[name] = new SpriteAnimation(start, end, durationMs, direction);
animations[name] = new SpriteAnimation(start, end, duration, direction);
}
}
public Rectangle GetTextureSource(string animationName, int timeMs) {
public Rectangle GetTextureSource(string animationName, double time) {
SpriteAnimation animation = animations[animationName];
int time = timeMs % animation.DurationMs;
time %= animation.Duration;
for (int i = animation.Start; i <= animation.End; i++) {
int frameTime = frames[i].DurationMs;
double frameTime = frames[i].Duration;
if (time < frameTime) {
return frames[i].Source;
}
@ -103,7 +102,7 @@ namespace SemiColinGames {
}
if (animation.Direction == AnimationDirection.PingPong) {
for (int i = animation.End - 1; i > animation.Start; i--) {
int frameTime = frames[i].DurationMs;
double frameTime = frames[i].Duration;
if (time < frameTime) {
return frames[i].Source;
}