World: rearrange a bunch of code to be in a more sensible order
This commit is contained in:
parent
36e70989af
commit
bb910fbe58
138
Shared/World.cs
138
Shared/World.cs
@ -8,6 +8,10 @@ using System.Linq;
|
|||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
|
|
||||||
public class Tile {
|
public class Tile {
|
||||||
|
public static int CompareByX(Tile t1, Tile t2) {
|
||||||
|
return t1.Position.X.CompareTo(t2.Position.X);
|
||||||
|
}
|
||||||
|
|
||||||
private TextureRef texture;
|
private TextureRef texture;
|
||||||
private Rectangle textureSource;
|
private Rectangle textureSource;
|
||||||
|
|
||||||
@ -29,19 +33,19 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
public class World {
|
public class World {
|
||||||
|
|
||||||
|
public const int TileSize = 16;
|
||||||
|
|
||||||
// Size of World in terms of tile grid.
|
// Size of World in terms of tile grid.
|
||||||
private int gridWidth;
|
private int gridWidth;
|
||||||
private int gridHeight;
|
private int gridHeight;
|
||||||
|
private readonly Tile[] obstacles;
|
||||||
// TODO: remove this.
|
private readonly Tile[] decorations;
|
||||||
public const int TileSize = 16;
|
|
||||||
readonly Tile[] obstacles;
|
|
||||||
readonly Tile[] decorations;
|
|
||||||
// Kept around for resetting the world's entities after player death or level restart.
|
// Kept around for resetting the world's entities after player death or level restart.
|
||||||
readonly JToken entitiesLayer;
|
private readonly JToken entitiesLayer;
|
||||||
|
private NPC[] npcs;
|
||||||
|
|
||||||
NPC[] npcs;
|
|
||||||
public Player Player { get; private set; }
|
public Player Player { get; private set; }
|
||||||
|
public AABB[] CollisionTargets { get; }
|
||||||
|
|
||||||
// Size of World in pixels.
|
// Size of World in pixels.
|
||||||
public int Width {
|
public int Width {
|
||||||
@ -52,6 +56,61 @@ namespace SemiColinGames {
|
|||||||
get { return gridHeight * TileSize; }
|
get { return gridHeight * TileSize; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public World(string json) {
|
||||||
|
JObject root = JObject.Parse(json);
|
||||||
|
|
||||||
|
List<Tile> hazardTiles = new List<Tile>();
|
||||||
|
List<Tile> obstacleTiles = new List<Tile>();
|
||||||
|
List<Tile> decorationTiles = new List<Tile>();
|
||||||
|
List<Tile> backgroundTiles = new List<Tile>();
|
||||||
|
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);
|
||||||
|
if (layerName == "hazards") {
|
||||||
|
hazardTiles = tileList;
|
||||||
|
} else if (layerName == "obstacles") {
|
||||||
|
obstacleTiles = 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(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.
|
||||||
|
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
|
||||||
|
for (int i = 0; i < obstacles.Length; i++) {
|
||||||
|
Vector2 center = new Vector2(
|
||||||
|
obstacles[i].Position.Left + halfSize.X, obstacles[i].Position.Top + halfSize.Y);
|
||||||
|
CollisionTargets[i + 1] = new AABB(center, halfSize, obstacles[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
|
||||||
private List<Tile> ParseLayer(JToken layer) {
|
private List<Tile> ParseLayer(JToken layer) {
|
||||||
string layerName = layer.SelectToken("name").Value<string>();
|
string layerName = layer.SelectToken("name").Value<string>();
|
||||||
|
|
||||||
@ -103,63 +162,8 @@ namespace SemiColinGames {
|
|||||||
return (player, npcs.ToArray());
|
return (player, npcs.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
static int CompareByX(Tile t1, Tile t2) {
|
private void Reset() {
|
||||||
return t1.Position.X.CompareTo(t2.Position.X);
|
(Player, npcs) = ParseEntities(entitiesLayer);
|
||||||
}
|
|
||||||
|
|
||||||
public World(string json) {
|
|
||||||
JObject root = JObject.Parse(json);
|
|
||||||
|
|
||||||
List<Tile> hazardTiles = new List<Tile>();
|
|
||||||
List<Tile> obstacleTiles = new List<Tile>();
|
|
||||||
List<Tile> decorationTiles = new List<Tile>();
|
|
||||||
List<Tile> backgroundTiles = new List<Tile>();
|
|
||||||
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);
|
|
||||||
if (layerName == "hazards") {
|
|
||||||
hazardTiles = tileList;
|
|
||||||
} else if (layerName == "obstacles") {
|
|
||||||
obstacleTiles = 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(hazardTiles);
|
|
||||||
obstacles = obstacleTiles.ToArray();
|
|
||||||
Array.Sort(obstacles, 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.
|
|
||||||
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
|
|
||||||
for (int i = 0; i < obstacles.Length; i++) {
|
|
||||||
Vector2 center = new Vector2(
|
|
||||||
obstacles[i].Position.Left + halfSize.X, obstacles[i].Position.Top + halfSize.Y);
|
|
||||||
CollisionTargets[i + 1] = new AABB(center, halfSize, obstacles[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(float modelTime, History<Input> input) {
|
public void Update(float modelTime, History<Input> input) {
|
||||||
@ -172,10 +176,6 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset() {
|
|
||||||
(Player, npcs) = ParseEntities(entitiesLayer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draws everything that's behind the player, from back to front.
|
// Draws everything that's behind the player, from back to front.
|
||||||
public void DrawBackground(SpriteBatch spriteBatch) {
|
public void DrawBackground(SpriteBatch spriteBatch) {
|
||||||
foreach (Tile t in decorations) {
|
foreach (Tile t in decorations) {
|
||||||
@ -192,7 +192,5 @@ namespace SemiColinGames {
|
|||||||
t.Draw(spriteBatch);
|
t.Draw(spriteBatch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public AABB[] CollisionTargets { get; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user