sneak/Shared/SneakWorld.cs

216 lines
7.6 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace SemiColinGames {
public sealed class SneakWorld : IWorld {
// Size of World in terms of tile grid.
private int gridWidth;
private int gridHeight;
private readonly Tile[] obstacles;
private readonly Tile[] decorations;
// Kept around for resetting the world's entities after player death or level restart.
private readonly JsonElement entitiesLayer;
private NPC[] npcs;
public Player Player { get; private set; }
public AABB[] CollisionTargets { get; }
public LinesOfSight LinesOfSight { get; private set; }
public Camera Camera { get; }
// Size of World in pixels.
public readonly int Width;
public readonly int Height;
// TODO: it seems weird that World takes in a GraphicsDevice. Remove it?
public SneakWorld(GraphicsDevice graphics, string json) {
Camera = new Camera();
LinesOfSight = new LinesOfSight(graphics);
JsonElement jsonRoot = JsonDocument.Parse(json).RootElement;
Width = jsonRoot.GetProperty("width").GetInt32();
Height = jsonRoot.GetProperty("height").GetInt32();
List<Tile> hazardTiles = new List<Tile>();
List<Tile> obstacleTiles = new List<Tile>();
List<Tile> obstacleTiles8 = new List<Tile>();
List<Tile> decorationTiles = new List<Tile>();
List<Tile> backgroundTiles = new List<Tile>();
foreach (JsonElement layer in jsonRoot.GetProperty("layers").EnumerateArray()) {
string layerName = layer.GetProperty("name").GetString();
if (layerName == "entities") {
entitiesLayer = layer;
(Player, npcs) = ParseEntities(layer);
continue;
}
List<Tile> tileList = ParseLayer(layer);
if (layerName == "hazards") {
hazardTiles = tileList;
} else if (layerName == "obstacles") {
obstacleTiles = tileList;
} else if (layerName == "obstacles-8") {
obstacleTiles8 = tileList;
} else if (layerName == "decorations") {
decorationTiles = tileList;
} else if (layerName == "background") {
backgroundTiles = tileList;
}
}
// Get all the obstacles into a single array, sorted by X.
obstacleTiles.AddRange(obstacleTiles8);
obstacleTiles.AddRange(hazardTiles);
obstacles = obstacleTiles.ToArray();
Array.Sort(obstacles, Tile.CompareByX);
// 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();
Debug.WriteLine("world size: {0}x{1}", gridWidth, gridHeight);
// 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.
CollisionTargets = new AABB[obstacles.Length + 2];
// Add a synthetic collisionTarget on the left side of the world.
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
// Now add all the normal collisionTargets for every obstacle.
for (int i = 0; i < obstacles.Length; i++) {
Tile obstacle = obstacles[i];
CollisionTargets[i + 1] = new AABB(
obstacle.Position.Center.ToVector2(), obstacle.Position.HalfSize(), obstacle);
}
// Add a final synthetic collisionTarget on the right side of the world.
CollisionTargets[obstacles.Length + 1] = new AABB(
new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
}
~SneakWorld() {
Dispose();
}
public void Dispose() {
LinesOfSight.Dispose();
GC.SuppressFinalize(this);
}
private List<Tile> ParseLayer(JsonElement layer) {
string layerName = layer.GetProperty("name").GetString();
var tileList = new List<Tile>();
int layerWidth = layer.GetProperty("gridCellsX").GetInt32();
int layerHeight = layer.GetProperty("gridCellsY").GetInt32();
gridWidth = Math.Max(gridWidth, layerWidth);
gridHeight = Math.Max(gridHeight, layerHeight);
int dataIndex = -1;
int tileWidth = layer.GetProperty("gridCellWidth").GetInt32();
int tileHeight = layer.GetProperty("gridCellHeight").GetInt32();
int textureWidth = Textures.Grassland.Get.Width / tileWidth;
foreach (JsonElement textureIndexElement in layer.GetProperty("data").EnumerateArray()) {
int textureIndex = textureIndexElement.GetInt32();
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(JsonElement layer) {
Player player = null;
List<NPC> npcs = new List<NPC>();
foreach (JsonElement entity in layer.GetProperty("entities").EnumerateArray()) {
string name = entity.GetProperty("name").GetString();
int x = entity.GetProperty("x").GetInt32();
int y = entity.GetProperty("y").GetInt32();
int facing = entity.GetProperty("flippedX").GetBoolean() ? -1 : 1;
if (name == "player") {
player = new Player(new Vector2(x, y), facing);
} else if (name == "executioner") {
npcs.Add(new NPC(new Vector2(x, y), facing));
}
}
return (player, npcs.ToArray());
}
private void Reset() {
(Player, npcs) = ParseEntities(entitiesLayer);
}
public void Update(float modelTime, History<Input> input) {
Player.Update(modelTime, this, input);
foreach (NPC npc in npcs) {
npc.Update(modelTime, this);
}
if (Player.Health <= 0) {
Reset();
}
LinesOfSight.Update(npcs, CollisionTargets);
Camera.Update(modelTime, Player, Width, Height);
}
public void ScreenShake() {
Camera.Shake();
}
// Draws everything that's behind the player, from back to front.
public void DrawBackground(SpriteBatch spriteBatch) {
foreach (Tile t in decorations) {
t.Draw(spriteBatch);
}
foreach (NPC npc in npcs) {
npc.Draw(spriteBatch);
}
}
// Draws everything that's in front of the player, from back to front.
public void DrawForeground(SpriteBatch spriteBatch) {
foreach (Tile t in obstacles) {
t.Draw(spriteBatch);
}
}
}
public class Tile {
public static int CompareByX(Tile t1, Tile t2) {
return t1.Position.X.CompareTo(t2.Position.X);
}
private readonly TextureRef texture;
private readonly Rectangle textureSource;
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);
}
}
}