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.

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