A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.8 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace SemiColinGames {
  7. public class World {
  8. public const int TileSize = 16;
  9. // Size of World in terms of tile grid.
  10. private int gridWidth;
  11. private int gridHeight;
  12. private readonly Tile[] obstacles;
  13. private readonly Tile[] decorations;
  14. // Kept around for resetting the world's entities after player death or level restart.
  15. private readonly JToken entitiesLayer;
  16. private NPC[] npcs;
  17. public Player Player { get; private set; }
  18. public AABB[] CollisionTargets { get; }
  19. // Size of World in pixels.
  20. public int Width {
  21. get { return gridWidth * TileSize; }
  22. }
  23. public int Height {
  24. get { return gridHeight * TileSize; }
  25. }
  26. public World(string json) {
  27. JObject root = JObject.Parse(json);
  28. List<Tile> hazardTiles = new List<Tile>();
  29. List<Tile> obstacleTiles = new List<Tile>();
  30. List<Tile> decorationTiles = new List<Tile>();
  31. List<Tile> backgroundTiles = new List<Tile>();
  32. foreach (JToken layer in root.SelectToken("layers").Children()) {
  33. string layerName = layer.SelectToken("name").Value<string>();
  34. if (layerName == "entities") {
  35. entitiesLayer = layer;
  36. (Player, npcs) = ParseEntities(layer);
  37. continue;
  38. }
  39. List<Tile> tileList = ParseLayer(layer);
  40. if (layerName == "hazards") {
  41. hazardTiles = tileList;
  42. } else if (layerName == "obstacles") {
  43. obstacleTiles = tileList;
  44. } else if (layerName == "decorations") {
  45. decorationTiles = tileList;
  46. } else if (layerName == "background") {
  47. backgroundTiles = tileList;
  48. }
  49. }
  50. // Get all the obstacles into a single array, sorted by X.
  51. obstacleTiles.AddRange(hazardTiles);
  52. obstacles = obstacleTiles.ToArray();
  53. Array.Sort(obstacles, Tile.CompareByX);
  54. // The background tiles are added before the rest of the decorations, so that they're drawn
  55. // in the back.
  56. backgroundTiles.AddRange(decorationTiles);
  57. decorations = backgroundTiles.ToArray();
  58. Debug.WriteLine("world size: {0}x{1}", gridWidth, gridHeight);
  59. // The obstacles are sorted by x-position. We maintain this invariant so that it's possible
  60. // to efficiently find CollisionTargets that are nearby a given x-position.
  61. CollisionTargets = new AABB[obstacles.Length + 2];
  62. // Add a synthetic collisionTarget on the left side of the world.
  63. CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
  64. // Now add all the normal collisionTargets for every obstacle.
  65. Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
  66. for (int i = 0; i < obstacles.Length; i++) {
  67. Vector2 center = new Vector2(
  68. obstacles[i].Position.Left + halfSize.X, obstacles[i].Position.Top + halfSize.Y);
  69. CollisionTargets[i + 1] = new AABB(center, halfSize, obstacles[i]);
  70. }
  71. // Add a final synthetic collisionTarget on the right side of the world.
  72. CollisionTargets[obstacles.Length + 1] = new AABB(
  73. new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
  74. }
  75. private List<Tile> ParseLayer(JToken layer) {
  76. string layerName = layer.SelectToken("name").Value<string>();
  77. var tileList = new List<Tile>();
  78. int layerWidth = layer.SelectToken("gridCellsX").Value<int>();
  79. int layerHeight = layer.SelectToken("gridCellsY").Value<int>();
  80. gridWidth = Math.Max(gridWidth, layerWidth);
  81. gridHeight = Math.Max(gridHeight, layerHeight);
  82. int dataIndex = -1;
  83. int tileWidth = layer.SelectToken("gridCellWidth").Value<int>();
  84. int tileHeight = layer.SelectToken("gridCellHeight").Value<int>();
  85. int textureWidth = Textures.Grassland.Get.Width / tileWidth;
  86. foreach (int textureIndex in layer.SelectToken("data").Values<int>()) {
  87. dataIndex++;
  88. if (textureIndex == -1) {
  89. continue;
  90. }
  91. int i = dataIndex % layerWidth;
  92. int j = dataIndex / layerWidth;
  93. Rectangle position = new Rectangle(
  94. i * tileWidth, j * tileHeight, tileWidth, tileHeight);
  95. int x = textureIndex % textureWidth;
  96. int y = textureIndex / textureWidth;
  97. Rectangle textureSource = new Rectangle(
  98. x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  99. bool isHazard = layerName == "hazards";
  100. tileList.Add(new Tile(Textures.Grassland, textureSource, position, isHazard));
  101. }
  102. return tileList;
  103. }
  104. private (Player, NPC[]) ParseEntities(JToken layer) {
  105. Player player = null;
  106. List<NPC> npcs = new List<NPC>();
  107. foreach (JToken entity in layer.SelectToken("entities").Children()) {
  108. string name = entity.SelectToken("name").Value<string>();
  109. int x = entity.SelectToken("x").Value<int>();
  110. int y = entity.SelectToken("y").Value<int>();
  111. int facing = entity.SelectToken("flippedX").Value<bool>() ? -1 : 1;
  112. if (name == "player") {
  113. player = new Player(new Point(x, y), facing);
  114. } else if (name == "executioner") {
  115. npcs.Add(new NPC(new Point(x, y), facing));
  116. }
  117. }
  118. return (player, npcs.ToArray());
  119. }
  120. private void Reset() {
  121. (Player, npcs) = ParseEntities(entitiesLayer);
  122. }
  123. public void Update(float modelTime, History<Input> input) {
  124. Player.Update(modelTime, this, input);
  125. foreach (NPC npc in npcs) {
  126. npc.Update(modelTime, this);
  127. }
  128. if (Player.Health <= 0) {
  129. Reset();
  130. }
  131. }
  132. // Draws everything that's behind the player, from back to front.
  133. public void DrawBackground(SpriteBatch spriteBatch) {
  134. foreach (Tile t in decorations) {
  135. t.Draw(spriteBatch);
  136. }
  137. foreach (NPC npc in npcs) {
  138. npc.Draw(spriteBatch);
  139. }
  140. }
  141. // Draws everything that's in front of the player, from back to front.
  142. public void DrawForeground(SpriteBatch spriteBatch) {
  143. foreach (Tile t in obstacles) {
  144. t.Draw(spriteBatch);
  145. }
  146. }
  147. }
  148. public class Tile {
  149. public static int CompareByX(Tile t1, Tile t2) {
  150. return t1.Position.X.CompareTo(t2.Position.X);
  151. }
  152. private TextureRef texture;
  153. private Rectangle textureSource;
  154. public Tile(TextureRef texture, Rectangle textureSource, Rectangle position, bool isHazard) {
  155. Position = position;
  156. this.texture = texture;
  157. this.textureSource = textureSource;
  158. IsHazard = isHazard;
  159. }
  160. public Rectangle Position { get; private set; }
  161. public bool IsHazard = false;
  162. public void Draw(SpriteBatch spriteBatch) {
  163. spriteBatch.Draw(
  164. texture.Get, Position.Location.ToVector2(), textureSource, Color.White);
  165. }
  166. }
  167. }