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