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.

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