using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Newtonsoft.Json.Linq; using System.Collections.Generic; namespace SemiColinGames { struct SpriteAnimation { public readonly int Start; public readonly int End; public readonly int DurationMs; public SpriteAnimation(int start, int end, int durationMs) { Start = start; End = end; DurationMs = durationMs; } } struct Frame { public readonly Rectangle Source; public readonly int DurationMs; public Frame(Rectangle source, int durationMs) { Source = source; DurationMs = durationMs; } } class Sprite { public readonly TextureRef Texture; private readonly Dictionary animations; private readonly List frames; public Sprite(TextureRef texture, string metadataJson) { Texture = texture; animations = new Dictionary(); JObject json = JObject.Parse(metadataJson); frames = new List(); foreach (JToken child in json.SelectToken("frames").Children()) { Rectangle source = new Rectangle( child.SelectToken("frame.x").Value(), child.SelectToken("frame.y").Value(), child.SelectToken("frame.w").Value(), child.SelectToken("frame.h").Value()); int durationMs = child.SelectToken("duration").Value(); frames.Add(new Frame(source, durationMs)); } // TODO: handle ping-pong animations. JToken frameTags = json.SelectToken("meta.frameTags"); foreach (JToken child in frameTags.Children()) { string name = child.SelectToken("name").Value(); int start = child.SelectToken("from").Value(); int end = child.SelectToken("to").Value(); int durationMs = 0; for (int i = start; i <= end; i++) { durationMs += frames[i].DurationMs; } animations[name] = new SpriteAnimation(start, end, durationMs); } } public Rectangle GetTextureSource(string animationName, int timeMs) { SpriteAnimation animation = animations[animationName]; int time = timeMs % animation.DurationMs; for (int i = animation.Start; i <= animation.End; 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. return frames[animation.End].Source; } } static class Sprites { public static Sprite Executioner; public static void Load(ContentManager content) { Executioner = new Sprite( Textures.Executioner, content.LoadString("sprites/ccg/executioner_female.json")); } } }