2020-03-03 22:14:05 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
|
|
|
|
using System.Collections.Generic;
|
2021-07-13 15:00:19 +00:00
|
|
|
|
using System.Text.Json;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-03-09 16:22:33 +00:00
|
|
|
|
public static class Sprites {
|
2020-03-05 20:28:34 +00:00
|
|
|
|
public static Sprite Executioner;
|
|
|
|
|
public static Sprite Ninja;
|
|
|
|
|
|
|
|
|
|
public static void Load(ContentManager content) {
|
|
|
|
|
Executioner = new Sprite(
|
|
|
|
|
Textures.Executioner, content.LoadString("sprites/ccg/executioner_female.json"));
|
|
|
|
|
Ninja = new Sprite(
|
|
|
|
|
Textures.Ninja, content.LoadString("sprites/ccg/ninja_female.json"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 20:55:40 +00:00
|
|
|
|
enum AnimationDirection {
|
|
|
|
|
Forward,
|
|
|
|
|
PingPong
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 22:14:05 +00:00
|
|
|
|
struct SpriteAnimation {
|
|
|
|
|
public readonly int Start;
|
|
|
|
|
public readonly int End;
|
2020-03-06 02:12:11 +00:00
|
|
|
|
public readonly double Duration;
|
2020-03-05 20:55:40 +00:00
|
|
|
|
public readonly AnimationDirection Direction;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
|
2020-03-06 02:12:11 +00:00
|
|
|
|
public SpriteAnimation(int start, int end, double duration, AnimationDirection direction) {
|
2020-03-03 22:14:05 +00:00
|
|
|
|
Start = start;
|
|
|
|
|
End = end;
|
2020-03-06 02:12:11 +00:00
|
|
|
|
Duration = duration;
|
2020-03-05 20:55:40 +00:00
|
|
|
|
Direction = direction;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Frame {
|
|
|
|
|
public readonly Rectangle Source;
|
2020-03-06 02:12:11 +00:00
|
|
|
|
public readonly double Duration;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
|
2020-03-06 02:12:11 +00:00
|
|
|
|
public Frame(Rectangle source, double duration) {
|
2020-03-03 22:14:05 +00:00
|
|
|
|
Source = source;
|
2020-03-06 02:12:11 +00:00
|
|
|
|
Duration = duration;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 16:22:33 +00:00
|
|
|
|
public class Sprite {
|
2020-03-11 18:42:10 +00:00
|
|
|
|
public readonly int Width;
|
|
|
|
|
public readonly int Height;
|
|
|
|
|
// Measures the empty pixels between the ground (where the sprite's feet rest in idle pose)
|
|
|
|
|
// and the bottom of the sprite's image.
|
|
|
|
|
public readonly int GroundPadding = 7;
|
|
|
|
|
|
2020-03-03 22:14:05 +00:00
|
|
|
|
public readonly TextureRef Texture;
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<string, SpriteAnimation> animations;
|
|
|
|
|
private readonly List<Frame> frames;
|
|
|
|
|
|
|
|
|
|
public Sprite(TextureRef texture, string metadataJson) {
|
|
|
|
|
Texture = texture;
|
|
|
|
|
animations = new Dictionary<string, SpriteAnimation>();
|
|
|
|
|
|
2021-07-13 15:00:19 +00:00
|
|
|
|
JsonElement jsonRoot = JsonDocument.Parse(metadataJson).RootElement;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
|
|
|
|
|
frames = new List<Frame>();
|
2021-07-13 15:00:19 +00:00
|
|
|
|
foreach (JsonElement child in jsonRoot.GetProperty("frames").EnumerateArray()) {
|
|
|
|
|
JsonElement frame = child.GetProperty("frame");
|
2020-03-03 22:14:05 +00:00
|
|
|
|
Rectangle source = new Rectangle(
|
2021-07-13 15:00:19 +00:00
|
|
|
|
frame.GetProperty("x").GetInt32(),
|
|
|
|
|
frame.GetProperty("y").GetInt32(),
|
|
|
|
|
frame.GetProperty("w").GetInt32(),
|
|
|
|
|
frame.GetProperty("h").GetInt32());
|
|
|
|
|
|
|
|
|
|
double duration = child.GetProperty("duration").GetDouble() / 1000;
|
2020-03-06 02:12:11 +00:00
|
|
|
|
frames.Add(new Frame(source, duration));
|
2020-03-03 22:14:05 +00:00
|
|
|
|
}
|
2021-07-13 15:00:19 +00:00
|
|
|
|
|
2020-03-11 18:42:10 +00:00
|
|
|
|
// We assume that all frames are the same size (which right now is assured by the
|
|
|
|
|
// Aseprite-based spritesheet export process).
|
|
|
|
|
Width = frames[0].Source.Width;
|
|
|
|
|
Height = frames[0].Source.Height;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
|
2021-07-13 15:00:19 +00:00
|
|
|
|
JsonElement frameTags = jsonRoot.GetProperty("meta").GetProperty("frameTags");
|
|
|
|
|
foreach (JsonElement child in frameTags.EnumerateArray()) {
|
|
|
|
|
string name = child.GetProperty("name").GetString();
|
|
|
|
|
int start = child.GetProperty("from").GetInt32();
|
|
|
|
|
int end = child.GetProperty("to").GetInt32();
|
|
|
|
|
string directionString = child.GetProperty("direction").GetString();
|
2020-03-05 20:55:40 +00:00
|
|
|
|
AnimationDirection direction = directionString == "pingpong" ?
|
2020-03-05 22:39:30 +00:00
|
|
|
|
AnimationDirection.PingPong : AnimationDirection.Forward;
|
2020-03-06 02:12:11 +00:00
|
|
|
|
double duration = 0;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
for (int i = start; i <= end; i++) {
|
2020-03-06 02:12:11 +00:00
|
|
|
|
duration += frames[i].Duration;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
}
|
2020-03-05 20:55:40 +00:00
|
|
|
|
// 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) {
|
2020-03-06 02:12:11 +00:00
|
|
|
|
duration = duration * 2 - frames[start].Duration - frames[end].Duration;
|
2020-03-05 20:55:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 02:12:11 +00:00
|
|
|
|
animations[name] = new SpriteAnimation(start, end, duration, direction);
|
2020-03-03 22:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 02:12:11 +00:00
|
|
|
|
public Rectangle GetTextureSource(string animationName, double time) {
|
2020-03-03 22:14:05 +00:00
|
|
|
|
SpriteAnimation animation = animations[animationName];
|
2020-03-06 02:12:11 +00:00
|
|
|
|
time %= animation.Duration;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
for (int i = animation.Start; i <= animation.End; i++) {
|
2020-03-06 02:12:11 +00:00
|
|
|
|
double frameTime = frames[i].Duration;
|
2020-03-03 22:14:05 +00:00
|
|
|
|
if (time < frameTime) {
|
|
|
|
|
return frames[i].Source;
|
|
|
|
|
}
|
|
|
|
|
time -= frameTime;
|
|
|
|
|
}
|
2020-03-05 20:55:40 +00:00
|
|
|
|
if (animation.Direction == AnimationDirection.PingPong) {
|
|
|
|
|
for (int i = animation.End - 1; i > animation.Start; i--) {
|
2020-03-06 02:12:11 +00:00
|
|
|
|
double frameTime = frames[i].Duration;
|
2020-03-05 20:55:40 +00:00
|
|
|
|
if (time < frameTime) {
|
|
|
|
|
return frames[i].Source;
|
|
|
|
|
}
|
|
|
|
|
time -= frameTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-03 22:14:05 +00:00
|
|
|
|
// We shouldn't get here, but if we did, the last frame is a fine thing to return.
|
|
|
|
|
return frames[animation.End].Source;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|