add hazards layer

This commit is contained in:
Colin McMillen 2020-03-08 18:27:20 -04:00
parent 9aa0d05eb2
commit 299ab41ec2

View File

@ -11,10 +11,11 @@ namespace SemiColinGames {
private TextureRef texture; private TextureRef texture;
private Rectangle textureSource; private Rectangle textureSource;
public Tile(TextureRef texture, Rectangle textureSource, Rectangle position) { public Tile(TextureRef texture, Rectangle textureSource, Rectangle position, bool isHazard) {
Position = position; Position = position;
this.texture = texture; this.texture = texture;
this.textureSource = textureSource; this.textureSource = textureSource;
IsHarmful = isHazard;
} }
public Rectangle Position { get; private set; } public Rectangle Position { get; private set; }
@ -52,6 +53,8 @@ namespace SemiColinGames {
} }
private List<Tile> ParseLayer(JToken layer) { private List<Tile> ParseLayer(JToken layer) {
string layerName = layer.SelectToken("name").Value<string>();
var tileList = new List<Tile>(); var tileList = new List<Tile>();
int layerWidth = layer.SelectToken("gridCellsX").Value<int>(); int layerWidth = layer.SelectToken("gridCellsX").Value<int>();
@ -76,7 +79,8 @@ namespace SemiColinGames {
int y = textureIndex / textureWidth; int y = textureIndex / textureWidth;
Rectangle textureSource = new Rectangle( Rectangle textureSource = new Rectangle(
x * tileWidth, y * tileHeight, tileWidth, tileHeight); x * tileWidth, y * tileHeight, tileWidth, tileHeight);
tileList.Add(new Tile(Textures.Grassland, textureSource, position)); bool isHazard = layerName == "hazards";
tileList.Add(new Tile(Textures.Grassland, textureSource, position, isHazard));
} }
return tileList; return tileList;
@ -99,9 +103,15 @@ namespace SemiColinGames {
return (player, npcs.ToArray()); return (player, npcs.ToArray());
} }
static int CompareByX(Tile t1, Tile t2) {
return t1.Position.X.CompareTo(t2.Position.X);
}
public World(string json) { public World(string json) {
JObject root = JObject.Parse(json); JObject root = JObject.Parse(json);
List<Tile> hazardTiles = new List<Tile>();
List<Tile> obstacleTiles = 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 (JToken layer in root.SelectToken("layers").Children()) {
@ -112,23 +122,28 @@ namespace SemiColinGames {
continue; continue;
} }
List<Tile> tileList = ParseLayer(layer); List<Tile> tileList = ParseLayer(layer);
if (layerName == "obstacles") { if (layerName == "hazards") {
tiles = tileList.ToArray(); hazardTiles = tileList;
} else if (layerName == "obstacles") {
obstacleTiles = tileList;
} else if (layerName == "decorations") { } else if (layerName == "decorations") {
decorationTiles = tileList; decorationTiles = tileList;
} else if (layerName == "background") { } else if (layerName == "background") {
backgroundTiles = tileList; backgroundTiles = tileList;
} }
} }
// Get all the obstacles into a single array, sorted by X.
obstacleTiles.AddRange(hazardTiles);
tiles = obstacleTiles.ToArray();
Array.Sort(tiles, CompareByX);
// The background tiles are added before the rest of the decorations, so that they're drawn // The background tiles are added before the rest of the decorations, so that they're drawn
// in the back. // in the back.
backgroundTiles.AddRange(decorationTiles); backgroundTiles.AddRange(decorationTiles);
decorations = backgroundTiles.ToArray(); decorations = backgroundTiles.ToArray();
Debug.WriteLine("world size: {0}x{1}", gridWidth, gridHeight); Debug.WriteLine("world size: {0}x{1}", gridWidth, gridHeight);
// Because we added tiles from left to right, the CollisionTargets are sorted by x-position. // The obstacles are sorted by x-position. We maintain this invariant so that it's possible
// We maintain this invariant so that it's possible to efficiently find CollisionTargets that // to efficiently find CollisionTargets that are nearby a given x-position.
// are nearby a given x-position.
CollisionTargets = new AABB[tiles.Length + 2]; CollisionTargets = new AABB[tiles.Length + 2];
// Add a synthetic collisionTarget on the left side of the world. // Add a synthetic collisionTarget on the left side of the world.