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.

215 lines
7.6 KiB

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