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;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-02-20 21:36:54 +00:00
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
public class Tile {
|
|
|
|
private TextureRef texture;
|
|
|
|
private Rectangle textureSource;
|
2020-02-20 17:25:46 +00:00
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
public Tile(TextureRef texture, Rectangle textureSource, Rectangle position) {
|
2020-01-25 17:03:26 +00:00
|
|
|
Position = position;
|
2020-03-08 21:23:51 +00:00
|
|
|
this.texture = texture;
|
|
|
|
this.textureSource = textureSource;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 16:15:11 +00:00
|
|
|
public Rectangle Position { get; private set; }
|
2020-03-08 21:23:51 +00:00
|
|
|
public bool IsHarmful = false;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-04 22:37:42 +00:00
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
2020-02-20 21:36:54 +00:00
|
|
|
spriteBatch.Draw(
|
2020-03-08 21:23:51 +00:00
|
|
|
texture.Get, Position.Location.ToVector2(), textureSource, Color.White);
|
2020-02-02 14:04:33 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 17:28:58 +00:00
|
|
|
public class World {
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
// Size of World in terms of tile grid.
|
|
|
|
private int gridWidth;
|
|
|
|
private int gridHeight;
|
|
|
|
|
|
|
|
// TODO: remove this.
|
2020-01-18 03:41:45 +00:00
|
|
|
public const int TileSize = 16;
|
2020-01-28 02:58:48 +00:00
|
|
|
readonly Tile[] tiles;
|
2020-02-20 17:25:46 +00:00
|
|
|
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.
|
|
|
|
readonly JToken entitiesLayer;
|
2020-01-29 23:01:41 +00:00
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
NPC[] npcs;
|
2020-03-03 18:08:51 +00:00
|
|
|
public Player Player { get; private set; }
|
2020-03-03 18:04:29 +00:00
|
|
|
|
2020-01-29 23:01:41 +00:00
|
|
|
// Size of World in pixels.
|
|
|
|
public int Width {
|
2020-03-08 21:23:51 +00:00
|
|
|
get { return gridWidth * TileSize; }
|
2020-01-29 23:01:41 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 23:27:20 +00:00
|
|
|
public int Height {
|
2020-03-08 21:23:51 +00:00
|
|
|
get { return gridHeight * TileSize; }
|
2020-01-29 23:27:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
private List<Tile> ParseLayer(JToken layer) {
|
|
|
|
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);
|
|
|
|
tileList.Add(new Tile(Textures.Grassland, textureSource, position));
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public World(string json) {
|
|
|
|
JObject root = JObject.Parse(json);
|
|
|
|
|
|
|
|
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);
|
|
|
|
// TODO: add background layer
|
|
|
|
if (layerName == "obstacles") {
|
|
|
|
tiles = tileList.ToArray();
|
|
|
|
} else if (layerName == "decorations") {
|
|
|
|
decorations = tileList.ToArray();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
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-01-29 23:04:56 +00:00
|
|
|
// Because we added tiles from left to right, the CollisionTargets are sorted by x-position.
|
|
|
|
// We maintain this invariant so that it's possible to efficiently find CollisionTargets that
|
2020-01-29 22:43:32 +00:00
|
|
|
// are nearby a given x-position.
|
2020-01-30 21:58:42 +00:00
|
|
|
CollisionTargets = new AABB[tiles.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
|
|
|
|
|
|
|
// Now add all the normal collisionTargets for every static terrain tile.
|
2020-01-29 21:11:28 +00:00
|
|
|
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
|
2020-01-28 02:58:48 +00:00
|
|
|
for (int i = 0; i < tiles.Length; i++) {
|
2020-01-29 21:11:28 +00:00
|
|
|
Vector2 center = new Vector2(
|
|
|
|
tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y);
|
2020-03-08 21:23:51 +00:00
|
|
|
CollisionTargets[i + 1] = new AABB(center, halfSize, tiles[i]);
|
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-01-30 21:58:42 +00:00
|
|
|
CollisionTargets[tiles.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-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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
2020-03-08 21:23:51 +00:00
|
|
|
(Player, npcs) = ParseEntities(entitiesLayer);
|
2020-02-28 22:08:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawForeground(SpriteBatch spriteBatch) {
|
2020-01-25 16:23:37 +00:00
|
|
|
foreach (Tile t in tiles) {
|
2020-02-04 22:37:42 +00:00
|
|
|
t.Draw(spriteBatch);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:58:42 +00:00
|
|
|
public AABB[] CollisionTargets { get; }
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|