Browse Source

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

main
Colin McMillen 3 months ago
parent
commit
3cd98d08f4
  1. 41
      Shared/SneakWorld.cs

41
Shared/SneakWorld.cs

@ -1,8 +1,8 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace SemiColinGames {
@ -14,7 +14,7 @@ namespace SemiColinGames {
private readonly Tile[] obstacles;
private readonly Tile[] decorations;
// Kept around for resetting the world's entities after player death or level restart.
private readonly JToken entitiesLayer;
private readonly JsonElement entitiesLayer;
private NPC[] npcs;
public Player Player { get; private set; }
@ -31,17 +31,17 @@ namespace SemiColinGames {
Camera = new Camera();
LinesOfSight = new LinesOfSight(graphics);
JObject root = JObject.Parse(json);
Width = root.SelectToken("width").Value<int>();
Height = root.SelectToken("height").Value<int>();
JsonElement jsonRoot = JsonDocument.Parse(json).RootElement;
Width = jsonRoot.GetProperty("width").GetInt32();
Height = jsonRoot.GetProperty("height").GetInt32();
List<Tile> hazardTiles = new List<Tile>();
List<Tile> obstacleTiles = new List<Tile>();
List<Tile> obstacleTiles8 = new List<Tile>();
List<Tile> decorationTiles = new List<Tile>();
List<Tile> backgroundTiles = new List<Tile>();
foreach (JToken layer in root.SelectToken("layers").Children()) {
string layerName = layer.SelectToken("name").Value<string>();
foreach (JsonElement layer in jsonRoot.GetProperty("layers").EnumerateArray()) {
string layerName = layer.GetProperty("name").GetString();
if (layerName == "entities") {
entitiesLayer = layer;
(Player, npcs) = ParseEntities(layer);
@ -99,21 +99,22 @@ namespace SemiColinGames {
GC.SuppressFinalize(this);
}
private List<Tile> ParseLayer(JToken layer) {
string layerName = layer.SelectToken("name").Value<string>();
private List<Tile> ParseLayer(JsonElement layer) {
string layerName = layer.GetProperty("name").GetString();
var tileList = new List<Tile>();
int layerWidth = layer.SelectToken("gridCellsX").Value<int>();
int layerHeight = layer.SelectToken("gridCellsY").Value<int>();
int layerWidth = layer.GetProperty("gridCellsX").GetInt32();
int layerHeight = layer.GetProperty("gridCellsY").GetInt32();
gridWidth = Math.Max(gridWidth, layerWidth);
gridHeight = Math.Max(gridHeight, layerHeight);
int dataIndex = -1;
int tileWidth = layer.SelectToken("gridCellWidth").Value<int>();
int tileHeight = layer.SelectToken("gridCellHeight").Value<int>();
int tileWidth = layer.GetProperty("gridCellWidth").GetInt32();
int tileHeight = layer.GetProperty("gridCellHeight").GetInt32();
int textureWidth = Textures.Grassland.Get.Width / tileWidth;
foreach (int textureIndex in layer.SelectToken("data").Values<int>()) {
foreach (JsonElement textureIndexElement in layer.GetProperty("data").EnumerateArray()) {
int textureIndex = textureIndexElement.GetInt32();
dataIndex++;
if (textureIndex == -1) {
continue;
@ -133,14 +134,14 @@ namespace SemiColinGames {
return tileList;
}
private (Player, NPC[]) ParseEntities(JToken layer) {
private (Player, NPC[]) ParseEntities(JsonElement layer) {
Player player = null;
List<NPC> npcs = new List<NPC>();
foreach (JToken entity in layer.SelectToken("entities").Children()) {
string name = entity.SelectToken("name").Value<string>();
int x = entity.SelectToken("x").Value<int>();
int y = entity.SelectToken("y").Value<int>();
int facing = entity.SelectToken("flippedX").Value<bool>() ? -1 : 1;
foreach (JsonElement entity in layer.GetProperty("entities").EnumerateArray()) {
string name = entity.GetProperty("name").GetString();
int x = entity.GetProperty("x").GetInt32();
int y = entity.GetProperty("y").GetInt32();
int facing = entity.GetProperty("flippedX").GetBoolean() ? -1 : 1;
if (name == "player") {
player = new Player(new Vector2(x, y), facing);
} else if (name == "executioner") {

Loading…
Cancel
Save