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