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