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