A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Content;
  3. using Newtonsoft.Json.Linq;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. struct SpriteAnimation {
  7. public readonly int Start;
  8. public readonly int End;
  9. public readonly int DurationMs;
  10. public SpriteAnimation(int start, int end, int durationMs) {
  11. Start = start;
  12. End = end;
  13. DurationMs = durationMs;
  14. }
  15. }
  16. struct Frame {
  17. public readonly Rectangle Source;
  18. public readonly int DurationMs;
  19. public Frame(Rectangle source, int durationMs) {
  20. Source = source;
  21. DurationMs = durationMs;
  22. }
  23. }
  24. class Sprite {
  25. public readonly TextureRef Texture;
  26. private readonly Dictionary<string, SpriteAnimation> animations;
  27. private readonly List<Frame> frames;
  28. public Sprite(TextureRef texture, string metadataJson) {
  29. Texture = texture;
  30. animations = new Dictionary<string, SpriteAnimation>();
  31. JObject json = JObject.Parse(metadataJson);
  32. frames = new List<Frame>();
  33. foreach (JToken child in json.SelectToken("frames").Children()) {
  34. Rectangle source = new Rectangle(
  35. child.SelectToken("frame.x").Value<int>(),
  36. child.SelectToken("frame.y").Value<int>(),
  37. child.SelectToken("frame.w").Value<int>(),
  38. child.SelectToken("frame.h").Value<int>());
  39. int durationMs = child.SelectToken("duration").Value<int>();
  40. frames.Add(new Frame(source, durationMs));
  41. }
  42. // TODO: handle ping-pong animations.
  43. JToken frameTags = json.SelectToken("meta.frameTags");
  44. foreach (JToken child in frameTags.Children()) {
  45. string name = child.SelectToken("name").Value<string>();
  46. int start = child.SelectToken("from").Value<int>();
  47. int end = child.SelectToken("to").Value<int>();
  48. int durationMs = 0;
  49. for (int i = start; i <= end; i++) {
  50. durationMs += frames[i].DurationMs;
  51. }
  52. animations[name] = new SpriteAnimation(start, end, durationMs);
  53. }
  54. }
  55. public Rectangle GetTextureSource(string animationName, int timeMs) {
  56. SpriteAnimation animation = animations[animationName];
  57. int time = timeMs % animation.DurationMs;
  58. for (int i = animation.Start; i <= animation.End; i++) {
  59. int frameTime = frames[i].DurationMs;
  60. if (time < frameTime) {
  61. return frames[i].Source;
  62. }
  63. time -= frameTime;
  64. }
  65. // We shouldn't get here, but if we did, the last frame is a fine thing to return.
  66. return frames[animation.End].Source;
  67. }
  68. }
  69. static class Sprites {
  70. public static Sprite Executioner;
  71. public static void Load(ContentManager content) {
  72. Executioner = new Sprite(
  73. Textures.Executioner, content.LoadString("sprites/ccg/executioner_female.json"));
  74. }
  75. }
  76. }