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.

174 lines
5.7 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. foreach (JToken layer in root.SelectToken("layers").Children()) {
  88. string layerName = layer.SelectToken("name").Value<string>();
  89. if (layerName == "entities") {
  90. entitiesLayer = layer;
  91. (Player, npcs) = ParseEntities(layer);
  92. continue;
  93. }
  94. List<Tile> tileList = ParseLayer(layer);
  95. // TODO: add background layer
  96. if (layerName == "obstacles") {
  97. tiles = tileList.ToArray();
  98. } else if (layerName == "decorations") {
  99. decorations = tileList.ToArray();
  100. }
  101. }
  102. Debug.WriteLine("world size: {0}x{1}", gridWidth, gridHeight);
  103. // Because we added tiles from left to right, the CollisionTargets are sorted by x-position.
  104. // We maintain this invariant so that it's possible to efficiently find CollisionTargets that
  105. // are nearby a given x-position.
  106. CollisionTargets = new AABB[tiles.Length + 2];
  107. // Add a synthetic collisionTarget on the left side of the world.
  108. CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
  109. // Now add all the normal collisionTargets for every static terrain tile.
  110. Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
  111. for (int i = 0; i < tiles.Length; i++) {
  112. Vector2 center = new Vector2(
  113. tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y);
  114. CollisionTargets[i + 1] = new AABB(center, halfSize, tiles[i]);
  115. }
  116. // Add a final synthetic collisionTarget on the right side of the world.
  117. CollisionTargets[tiles.Length + 1] = new AABB(
  118. new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
  119. }
  120. public void Update(float modelTime, History<Input> input) {
  121. Player.Update(modelTime, this, input);
  122. foreach (NPC npc in npcs) {
  123. npc.Update(modelTime, this);
  124. }
  125. if (Player.Health <= 0) {
  126. Reset();
  127. }
  128. }
  129. void Reset() {
  130. (Player, npcs) = ParseEntities(entitiesLayer);
  131. }
  132. public void DrawBackground(SpriteBatch spriteBatch) {
  133. foreach (Tile t in decorations) {
  134. t.Draw(spriteBatch);
  135. }
  136. foreach (NPC npc in npcs) {
  137. npc.Draw(spriteBatch);
  138. }
  139. }
  140. public void DrawForeground(SpriteBatch spriteBatch) {
  141. foreach (Tile t in tiles) {
  142. t.Draw(spriteBatch);
  143. }
  144. }
  145. public AABB[] CollisionTargets { get; }
  146. }
  147. }