2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-03-08 21:23:51 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-02-02 14:33:33 +00:00
|
|
|
using System;
|
2020-01-18 03:41:45 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-02-20 21:36:54 +00:00
|
|
|
|
2020-03-09 16:19:19 +00:00
|
|
|
public sealed class World : IDisposable {
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-03-08 22:58:52 +00:00
|
|
|
public const int TileSize = 16;
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
// Size of World in terms of tile grid.
|
|
|
|
private int gridWidth;
|
|
|
|
private int gridHeight;
|
2020-03-08 22:58:52 +00:00
|
|
|
private readonly Tile[] obstacles;
|
|
|
|
private readonly Tile[] decorations;
|
2020-03-08 21:23:51 +00:00
|
|
|
// Kept around for resetting the world's entities after player death or level restart.
|
2020-03-08 22:58:52 +00:00
|
|
|
private readonly JToken entitiesLayer;
|
|
|
|
private NPC[] npcs;
|
2020-01-29 23:01:41 +00:00
|
|
|
|
2020-03-03 18:08:51 +00:00
|
|
|
public Player Player { get; private set; }
|
2020-03-08 22:58:52 +00:00
|
|
|
public AABB[] CollisionTargets { get; }
|
2020-03-09 16:19:19 +00:00
|
|
|
public LinesOfSight LinesOfSight { get; private set; }
|
2020-03-10 18:38:08 +00:00
|
|
|
public Camera Camera { get; }
|
2020-03-03 18:04:29 +00:00
|
|
|
|
2020-01-29 23:01:41 +00:00
|
|
|
// Size of World in pixels.
|
2020-03-10 21:32:33 +00:00
|
|
|
public readonly int Width;
|
|
|
|
public readonly int Height;
|
2020-01-29 23:27:20 +00:00
|
|
|
|
2020-03-09 16:19:19 +00:00
|
|
|
public World(GraphicsDevice graphics, string json) {
|
2020-03-10 18:38:08 +00:00
|
|
|
Camera = new Camera();
|
2020-03-09 16:19:19 +00:00
|
|
|
LinesOfSight = new LinesOfSight(graphics);
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
JObject root = JObject.Parse(json);
|
2020-03-10 21:32:33 +00:00
|
|
|
Width = root.SelectToken("width").Value<int>();
|
|
|
|
Height = root.SelectToken("height").Value<int>();
|
2020-03-08 21:23:51 +00:00
|
|
|
|
2020-03-08 22:27:20 +00:00
|
|
|
List<Tile> hazardTiles = new List<Tile>();
|
|
|
|
List<Tile> obstacleTiles = new List<Tile>();
|
2020-03-10 21:10:09 +00:00
|
|
|
List<Tile> obstacleTiles8 = new List<Tile>();
|
2020-03-08 21:39:36 +00:00
|
|
|
List<Tile> decorationTiles = new List<Tile>();
|
|
|
|
List<Tile> backgroundTiles = new List<Tile>();
|
2020-03-08 21:23:51 +00:00
|
|
|
foreach (JToken layer in root.SelectToken("layers").Children()) {
|
|
|
|
string layerName = layer.SelectToken("name").Value<string>();
|
|
|
|
if (layerName == "entities") {
|
|
|
|
entitiesLayer = layer;
|
|
|
|
(Player, npcs) = ParseEntities(layer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
List<Tile> tileList = ParseLayer(layer);
|
2020-03-08 22:27:20 +00:00
|
|
|
if (layerName == "hazards") {
|
|
|
|
hazardTiles = tileList;
|
|
|
|
} else if (layerName == "obstacles") {
|
|
|
|
obstacleTiles = tileList;
|
2020-03-10 21:10:09 +00:00
|
|
|
} else if (layerName == "obstacles-8") {
|
|
|
|
obstacleTiles8 = tileList;
|
2020-03-08 21:23:51 +00:00
|
|
|
} else if (layerName == "decorations") {
|
2020-03-08 21:39:36 +00:00
|
|
|
decorationTiles = tileList;
|
|
|
|
} else if (layerName == "background") {
|
|
|
|
backgroundTiles = tileList;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 22:27:20 +00:00
|
|
|
// Get all the obstacles into a single array, sorted by X.
|
2020-03-10 21:10:09 +00:00
|
|
|
obstacleTiles.AddRange(obstacleTiles8);
|
2020-03-08 22:27:20 +00:00
|
|
|
obstacleTiles.AddRange(hazardTiles);
|
2020-03-08 22:36:42 +00:00
|
|
|
obstacles = obstacleTiles.ToArray();
|
2020-03-08 22:58:52 +00:00
|
|
|
Array.Sort(obstacles, Tile.CompareByX);
|
2020-03-08 21:39:36 +00:00
|
|
|
// The background tiles are added before the rest of the decorations, so that they're drawn
|
|
|
|
// in the back.
|
|
|
|
backgroundTiles.AddRange(decorationTiles);
|
|
|
|
decorations = backgroundTiles.ToArray();
|
2020-03-08 21:23:51 +00:00
|
|
|
Debug.WriteLine("world size: {0}x{1}", gridWidth, gridHeight);
|
2020-01-29 21:11:28 +00:00
|
|
|
|
2020-03-08 22:27:20 +00:00
|
|
|
// The obstacles are sorted by x-position. We maintain this invariant so that it's possible
|
|
|
|
// to efficiently find CollisionTargets that are nearby a given x-position.
|
2020-03-08 22:36:42 +00:00
|
|
|
CollisionTargets = new AABB[obstacles.Length + 2];
|
2020-01-29 22:43:32 +00:00
|
|
|
|
|
|
|
// Add a synthetic collisionTarget on the left side of the world.
|
2020-01-30 21:58:42 +00:00
|
|
|
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
|
2020-01-29 22:43:32 +00:00
|
|
|
|
2020-03-08 22:36:42 +00:00
|
|
|
// Now add all the normal collisionTargets for every obstacle.
|
|
|
|
for (int i = 0; i < obstacles.Length; i++) {
|
2020-03-10 21:10:09 +00:00
|
|
|
Tile obstacle = obstacles[i];
|
|
|
|
CollisionTargets[i + 1] = new AABB(
|
|
|
|
obstacle.Position.Center.ToVector2(), obstacle.Position.HalfSize(), obstacle);
|
2020-01-28 02:58:48 +00:00
|
|
|
}
|
2020-01-29 22:43:32 +00:00
|
|
|
|
|
|
|
// Add a final synthetic collisionTarget on the right side of the world.
|
2020-03-08 22:36:42 +00:00
|
|
|
CollisionTargets[obstacles.Length + 1] = new AABB(
|
2020-01-29 23:01:41 +00:00
|
|
|
new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 16:19:19 +00:00
|
|
|
~World() {
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
LinesOfSight.Dispose();
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2020-03-08 22:58:52 +00:00
|
|
|
private List<Tile> ParseLayer(JToken layer) {
|
|
|
|
string layerName = layer.SelectToken("name").Value<string>();
|
|
|
|
|
|
|
|
var tileList = new List<Tile>();
|
|
|
|
|
|
|
|
int layerWidth = layer.SelectToken("gridCellsX").Value<int>();
|
|
|
|
int layerHeight = layer.SelectToken("gridCellsY").Value<int>();
|
|
|
|
gridWidth = Math.Max(gridWidth, layerWidth);
|
|
|
|
gridHeight = Math.Max(gridHeight, layerHeight);
|
|
|
|
|
|
|
|
int dataIndex = -1;
|
|
|
|
int tileWidth = layer.SelectToken("gridCellWidth").Value<int>();
|
|
|
|
int tileHeight = layer.SelectToken("gridCellHeight").Value<int>();
|
|
|
|
int textureWidth = Textures.Grassland.Get.Width / tileWidth;
|
|
|
|
foreach (int textureIndex in layer.SelectToken("data").Values<int>()) {
|
|
|
|
dataIndex++;
|
|
|
|
if (textureIndex == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int i = dataIndex % layerWidth;
|
|
|
|
int j = dataIndex / layerWidth;
|
|
|
|
Rectangle position = new Rectangle(
|
|
|
|
i * tileWidth, j * tileHeight, tileWidth, tileHeight);
|
|
|
|
int x = textureIndex % textureWidth;
|
|
|
|
int y = textureIndex / textureWidth;
|
|
|
|
Rectangle textureSource = new Rectangle(
|
|
|
|
x * tileWidth, y * tileHeight, tileWidth, tileHeight);
|
|
|
|
bool isHazard = layerName == "hazards";
|
|
|
|
tileList.Add(new Tile(Textures.Grassland, textureSource, position, isHazard));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tileList;
|
|
|
|
}
|
|
|
|
|
|
|
|
private (Player, NPC[]) ParseEntities(JToken layer) {
|
|
|
|
Player player = null;
|
|
|
|
List<NPC> npcs = new List<NPC>();
|
|
|
|
foreach (JToken entity in layer.SelectToken("entities").Children()) {
|
|
|
|
string name = entity.SelectToken("name").Value<string>();
|
|
|
|
int x = entity.SelectToken("x").Value<int>();
|
|
|
|
int y = entity.SelectToken("y").Value<int>();
|
|
|
|
int facing = entity.SelectToken("flippedX").Value<bool>() ? -1 : 1;
|
|
|
|
if (name == "player") {
|
|
|
|
player = new Player(new Point(x, y), facing);
|
|
|
|
} else if (name == "executioner") {
|
|
|
|
npcs.Add(new NPC(new Point(x, y), facing));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (player, npcs.ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Reset() {
|
|
|
|
(Player, npcs) = ParseEntities(entitiesLayer);
|
|
|
|
}
|
|
|
|
|
2020-03-03 18:04:29 +00:00
|
|
|
public void Update(float modelTime, History<Input> input) {
|
2020-03-06 17:28:58 +00:00
|
|
|
Player.Update(modelTime, this, input);
|
2020-02-28 22:08:34 +00:00
|
|
|
foreach (NPC npc in npcs) {
|
2020-03-06 17:28:58 +00:00
|
|
|
npc.Update(modelTime, this);
|
2020-02-28 22:08:34 +00:00
|
|
|
}
|
2020-03-03 18:08:51 +00:00
|
|
|
if (Player.Health <= 0) {
|
2020-03-03 18:04:29 +00:00
|
|
|
Reset();
|
|
|
|
}
|
2020-03-09 16:41:07 +00:00
|
|
|
LinesOfSight.Update(npcs, CollisionTargets);
|
2020-03-10 19:04:41 +00:00
|
|
|
Camera.Update(modelTime, Player.Position, Width);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ScreenShake() {
|
|
|
|
Camera.Shake();
|
2020-03-03 18:04:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 22:36:42 +00:00
|
|
|
// Draws everything that's behind the player, from back to front.
|
2020-02-20 17:25:46 +00:00
|
|
|
public void DrawBackground(SpriteBatch spriteBatch) {
|
|
|
|
foreach (Tile t in decorations) {
|
|
|
|
t.Draw(spriteBatch);
|
|
|
|
}
|
2020-02-28 22:08:34 +00:00
|
|
|
foreach (NPC npc in npcs) {
|
|
|
|
npc.Draw(spriteBatch);
|
|
|
|
}
|
2020-02-20 17:25:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 22:36:42 +00:00
|
|
|
// Draws everything that's in front of the player, from back to front.
|
2020-02-20 17:25:46 +00:00
|
|
|
public void DrawForeground(SpriteBatch spriteBatch) {
|
2020-03-08 22:36:42 +00:00
|
|
|
foreach (Tile t in obstacles) {
|
2020-02-04 22:37:42 +00:00
|
|
|
t.Draw(spriteBatch);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 15:48:22 +00:00
|
|
|
|
|
|
|
public class Tile {
|
|
|
|
public static int CompareByX(Tile t1, Tile t2) {
|
|
|
|
return t1.Position.X.CompareTo(t2.Position.X);
|
|
|
|
}
|
|
|
|
|
2020-03-09 16:19:19 +00:00
|
|
|
private readonly TextureRef texture;
|
|
|
|
private readonly Rectangle textureSource;
|
2020-03-09 15:48:22 +00:00
|
|
|
|
|
|
|
public Tile(TextureRef texture, Rectangle textureSource, Rectangle position, bool isHazard) {
|
|
|
|
Position = position;
|
|
|
|
this.texture = texture;
|
|
|
|
this.textureSource = textureSource;
|
|
|
|
IsHazard = isHazard;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rectangle Position { get; private set; }
|
|
|
|
public bool IsHazard = false;
|
|
|
|
|
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
|
|
|
spriteBatch.Draw(
|
|
|
|
texture.Get, Position.Location.ToVector2(), textureSource, Color.White);
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|