Newtonsoft.Json -> System.Text.Json for Sprites.cs

This commit is contained in:
Colin McMillen 2021-07-13 11:00:19 -04:00
parent b561cf7243
commit b15a55afd1

View File

@ -1,7 +1,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Newtonsoft.Json.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json;
namespace SemiColinGames { namespace SemiColinGames {
public static class Sprites { public static class Sprites {
@ -61,29 +61,32 @@ namespace SemiColinGames {
Texture = texture; Texture = texture;
animations = new Dictionary<string, SpriteAnimation>(); animations = new Dictionary<string, SpriteAnimation>();
JObject json = JObject.Parse(metadataJson); JsonElement jsonRoot = JsonDocument.Parse(metadataJson).RootElement;
frames = new List<Frame>(); frames = new List<Frame>();
foreach (JToken child in json.SelectToken("frames").Children()) { foreach (JsonElement child in jsonRoot.GetProperty("frames").EnumerateArray()) {
JsonElement frame = child.GetProperty("frame");
Rectangle source = new Rectangle( Rectangle source = new Rectangle(
child.SelectToken("frame.x").Value<int>(), frame.GetProperty("x").GetInt32(),
child.SelectToken("frame.y").Value<int>(), frame.GetProperty("y").GetInt32(),
child.SelectToken("frame.w").Value<int>(), frame.GetProperty("w").GetInt32(),
child.SelectToken("frame.h").Value<int>()); frame.GetProperty("h").GetInt32());
double duration = child.SelectToken("duration").Value<double>() / 1000;
double duration = child.GetProperty("duration").GetDouble() / 1000;
frames.Add(new Frame(source, duration)); frames.Add(new Frame(source, duration));
} }
// We assume that all frames are the same size (which right now is assured by the // We assume that all frames are the same size (which right now is assured by the
// Aseprite-based spritesheet export process). // Aseprite-based spritesheet export process).
Width = frames[0].Source.Width; Width = frames[0].Source.Width;
Height = frames[0].Source.Height; Height = frames[0].Source.Height;
JToken frameTags = json.SelectToken("meta.frameTags"); JsonElement frameTags = jsonRoot.GetProperty("meta").GetProperty("frameTags");
foreach (JToken child in frameTags.Children()) { foreach (JsonElement child in frameTags.EnumerateArray()) {
string name = child.SelectToken("name").Value<string>(); string name = child.GetProperty("name").GetString();
int start = child.SelectToken("from").Value<int>(); int start = child.GetProperty("from").GetInt32();
int end = child.SelectToken("to").Value<int>(); int end = child.GetProperty("to").GetInt32();
string directionString = child.SelectToken("direction").Value<string>(); string directionString = child.GetProperty("direction").GetString();
AnimationDirection direction = directionString == "pingpong" ? AnimationDirection direction = directionString == "pingpong" ?
AnimationDirection.PingPong : AnimationDirection.Forward; AnimationDirection.PingPong : AnimationDirection.Forward;
double duration = 0; double duration = 0;