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.

214 lines
7.5 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 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 JToken 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. JObject root = JObject.Parse(json);
  28. Width = root.SelectToken("width").Value<int>();
  29. Height = root.SelectToken("height").Value<int>();
  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 (JToken layer in root.SelectToken("layers").Children()) {
  36. string layerName = layer.SelectToken("name").Value<string>();
  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(JToken layer) {
  88. string layerName = layer.SelectToken("name").Value<string>();
  89. var tileList = new List<Tile>();
  90. int layerWidth = layer.SelectToken("gridCellsX").Value<int>();
  91. int layerHeight = layer.SelectToken("gridCellsY").Value<int>();
  92. gridWidth = Math.Max(gridWidth, layerWidth);
  93. gridHeight = Math.Max(gridHeight, layerHeight);
  94. int dataIndex = -1;
  95. int tileWidth = layer.SelectToken("gridCellWidth").Value<int>();
  96. int tileHeight = layer.SelectToken("gridCellHeight").Value<int>();
  97. int textureWidth = Textures.Grassland.Get.Width / tileWidth;
  98. foreach (int textureIndex in layer.SelectToken("data").Values<int>()) {
  99. dataIndex++;
  100. if (textureIndex == -1) {
  101. continue;
  102. }
  103. int i = dataIndex % layerWidth;
  104. int j = dataIndex / layerWidth;
  105. Rectangle position = new Rectangle(
  106. i * tileWidth, j * tileHeight, tileWidth, tileHeight);
  107. int x = textureIndex % textureWidth;
  108. int y = textureIndex / textureWidth;
  109. Rectangle textureSource = new Rectangle(
  110. x * tileWidth, y * tileHeight, tileWidth, tileHeight);
  111. bool isHazard = layerName == "hazards";
  112. tileList.Add(new Tile(Textures.Grassland, textureSource, position, isHazard));
  113. }
  114. return tileList;
  115. }
  116. private (Player, NPC[]) ParseEntities(JToken layer) {
  117. Player player = null;
  118. List<NPC> npcs = new List<NPC>();
  119. foreach (JToken entity in layer.SelectToken("entities").Children()) {
  120. string name = entity.SelectToken("name").Value<string>();
  121. int x = entity.SelectToken("x").Value<int>();
  122. int y = entity.SelectToken("y").Value<int>();
  123. int facing = entity.SelectToken("flippedX").Value<bool>() ? -1 : 1;
  124. if (name == "player") {
  125. player = new Player(new Vector2(x, y), facing);
  126. } else if (name == "executioner") {
  127. npcs.Add(new NPC(new Vector2(x, y), facing));
  128. }
  129. }
  130. return (player, npcs.ToArray());
  131. }
  132. private void Reset() {
  133. (Player, npcs) = ParseEntities(entitiesLayer);
  134. }
  135. public void Update(float modelTime, History<Input> input) {
  136. Player.Update(modelTime, this, input);
  137. foreach (NPC npc in npcs) {
  138. npc.Update(modelTime, this);
  139. }
  140. if (Player.Health <= 0) {
  141. Reset();
  142. }
  143. LinesOfSight.Update(npcs, CollisionTargets);
  144. Camera.Update(modelTime, Player, Width, Height);
  145. }
  146. public void ScreenShake() {
  147. Camera.Shake();
  148. }
  149. // Draws everything that's behind the player, from back to front.
  150. public void DrawBackground(SpriteBatch spriteBatch) {
  151. foreach (Tile t in decorations) {
  152. t.Draw(spriteBatch);
  153. }
  154. foreach (NPC npc in npcs) {
  155. npc.Draw(spriteBatch);
  156. }
  157. }
  158. // Draws everything that's in front of the player, from back to front.
  159. public void DrawForeground(SpriteBatch spriteBatch) {
  160. foreach (Tile t in obstacles) {
  161. t.Draw(spriteBatch);
  162. }
  163. }
  164. }
  165. public class Tile {
  166. public static int CompareByX(Tile t1, Tile t2) {
  167. return t1.Position.X.CompareTo(t2.Position.X);
  168. }
  169. private readonly TextureRef texture;
  170. private readonly Rectangle textureSource;
  171. public Tile(TextureRef texture, Rectangle textureSource, Rectangle position, bool isHazard) {
  172. Position = position;
  173. this.texture = texture;
  174. this.textureSource = textureSource;
  175. IsHazard = isHazard;
  176. }
  177. public Rectangle Position { get; private set; }
  178. public bool IsHazard = false;
  179. public void Draw(SpriteBatch spriteBatch) {
  180. spriteBatch.Draw(
  181. texture.Get, Position.Location.ToVector2(), textureSource, Color.White);
  182. }
  183. }
  184. }