using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; namespace SemiColinGames { public class Tile { private TextureRef texture; private Rectangle textureSource; public Tile(TextureRef texture, Rectangle textureSource, Rectangle position, bool isHazard) { Position = position; this.texture = texture; this.textureSource = textureSource; IsHazard = isHazard; } public Rectangle Position { get; private set; } public bool IsHazard = false; public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw( texture.Get, Position.Location.ToVector2(), textureSource, Color.White); } } public class World { // Size of World in terms of tile grid. private int gridWidth; private int gridHeight; // TODO: remove this. public const int TileSize = 16; readonly Tile[] obstacles; readonly Tile[] decorations; // Kept around for resetting the world's entities after player death or level restart. readonly JToken entitiesLayer; NPC[] npcs; public Player Player { get; private set; } // Size of World in pixels. public int Width { get { return gridWidth * TileSize; } } public int Height { get { return gridHeight * TileSize; } } private List ParseLayer(JToken layer) { string layerName = layer.SelectToken("name").Value(); var tileList = new List(); int layerWidth = layer.SelectToken("gridCellsX").Value(); int layerHeight = layer.SelectToken("gridCellsY").Value(); gridWidth = Math.Max(gridWidth, layerWidth); gridHeight = Math.Max(gridHeight, layerHeight); int dataIndex = -1; int tileWidth = layer.SelectToken("gridCellWidth").Value(); int tileHeight = layer.SelectToken("gridCellHeight").Value(); int textureWidth = Textures.Grassland.Get.Width / tileWidth; foreach (int textureIndex in layer.SelectToken("data").Values()) { dataIndex++; if (textureIndex == -1) { continue; } int i = dataIndex % layerWidth; int j = dataIndex / layerWidth; Rectangle position = new Rectangle( i * tileWidth, j * tileHeight, tileWidth, tileHeight); int x = textureIndex % textureWidth; int y = textureIndex / textureWidth; Rectangle textureSource = new Rectangle( x * tileWidth, y * tileHeight, tileWidth, tileHeight); bool isHazard = layerName == "hazards"; tileList.Add(new Tile(Textures.Grassland, textureSource, position, isHazard)); } return tileList; } private (Player, NPC[]) ParseEntities(JToken layer) { Player player = null; List npcs = new List(); foreach (JToken entity in layer.SelectToken("entities").Children()) { string name = entity.SelectToken("name").Value(); int x = entity.SelectToken("x").Value(); int y = entity.SelectToken("y").Value(); int facing = entity.SelectToken("flippedX").Value() ? -1 : 1; if (name == "player") { player = new Player(new Point(x, y), facing); } else if (name == "executioner") { npcs.Add(new NPC(new Point(x, y), facing)); } } 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()) { string layerName = layer.SelectToken("name").Value(); if (layerName == "entities") { entitiesLayer = layer; (Player, npcs) = ParseEntities(layer); continue; } List tileList = ParseLayer(layer); 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); obstacles = obstacleTiles.ToArray(); Array.Sort(obstacles, 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); // 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[obstacles.Length + 2]; // Add a synthetic collisionTarget on the left side of the world. CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue)); // Now add all the normal collisionTargets for every obstacle. Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2); for (int i = 0; i < obstacles.Length; i++) { Vector2 center = new Vector2( obstacles[i].Position.Left + halfSize.X, obstacles[i].Position.Top + halfSize.Y); CollisionTargets[i + 1] = new AABB(center, halfSize, obstacles[i]); } // Add a final synthetic collisionTarget on the right side of the world. CollisionTargets[obstacles.Length + 1] = new AABB( new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue)); } public void Update(float modelTime, History input) { Player.Update(modelTime, this, input); foreach (NPC npc in npcs) { npc.Update(modelTime, this); } if (Player.Health <= 0) { Reset(); } } void Reset() { (Player, npcs) = ParseEntities(entitiesLayer); } // Draws everything that's behind the player, from back to front. public void DrawBackground(SpriteBatch spriteBatch) { foreach (Tile t in decorations) { t.Draw(spriteBatch); } foreach (NPC npc in npcs) { npc.Draw(spriteBatch); } } // Draws everything that's in front of the player, from back to front. public void DrawForeground(SpriteBatch spriteBatch) { foreach (Tile t in obstacles) { t.Draw(spriteBatch); } } public AABB[] CollisionTargets { get; } } }