diff --git a/Shared/World.cs b/Shared/World.cs index 05fbb2b..bed9048 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -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 ParseLayer(JToken layer) { + string layerName = layer.SelectToken("name").Value(); + var tileList = new List(); int layerWidth = layer.SelectToken("gridCellsX").Value(); @@ -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 hazardTiles = new List(); + List obstacleTiles = new List(); List decorationTiles = new List(); List backgroundTiles = new List(); foreach (JToken layer in root.SelectToken("layers").Children()) { @@ -112,23 +122,28 @@ namespace SemiColinGames { continue; } List 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.