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.

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