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.

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