Compare commits

..

2 Commits

Author SHA1 Message Date
a4802031d5 World: load size directly from JSON. 2020-03-10 17:32:33 -04:00
b85661e2c4 add support for 8x8 obstacles 2020-03-10 17:10:09 -04:00
2 changed files with 16 additions and 11 deletions

View File

@ -18,6 +18,11 @@ namespace SemiColinGames {
public static void Deconstruct(this Point point, out int x, out int y) => public static void Deconstruct(this Point point, out int x, out int y) =>
(x, y) = (point.X, point.Y); (x, y) = (point.X, point.Y);
// Rectangle
public static Vector2 HalfSize(this Rectangle rect) {
return new Vector2(rect.Width / 2, rect.Height / 2);
}
// SpriteFont // SpriteFont
public static Vector2 CenteredOn(this SpriteFont font, string text, Point position) { public static Vector2 CenteredOn(this SpriteFont font, string text, Point position) {
Vector2 size = font.MeasureString(text); Vector2 size = font.MeasureString(text);

View File

@ -25,22 +25,20 @@ namespace SemiColinGames {
public Camera Camera { get; } public Camera Camera { get; }
// Size of World in pixels. // Size of World in pixels.
public int Width { public readonly int Width;
get { return gridWidth * TileSize; } public readonly int Height;
}
public int Height {
get { return gridHeight * TileSize; }
}
public World(GraphicsDevice graphics, string json) { public World(GraphicsDevice graphics, string json) {
Camera = new Camera(); Camera = new Camera();
LinesOfSight = new LinesOfSight(graphics); LinesOfSight = new LinesOfSight(graphics);
JObject root = JObject.Parse(json); JObject root = JObject.Parse(json);
Width = root.SelectToken("width").Value<int>();
Height = root.SelectToken("height").Value<int>();
List<Tile> hazardTiles = new List<Tile>(); List<Tile> hazardTiles = new List<Tile>();
List<Tile> obstacleTiles = new List<Tile>(); List<Tile> obstacleTiles = new List<Tile>();
List<Tile> obstacleTiles8 = new List<Tile>();
List<Tile> decorationTiles = new List<Tile>(); List<Tile> decorationTiles = new List<Tile>();
List<Tile> backgroundTiles = new List<Tile>(); List<Tile> backgroundTiles = new List<Tile>();
foreach (JToken layer in root.SelectToken("layers").Children()) { foreach (JToken layer in root.SelectToken("layers").Children()) {
@ -55,6 +53,8 @@ namespace SemiColinGames {
hazardTiles = tileList; hazardTiles = tileList;
} else if (layerName == "obstacles") { } else if (layerName == "obstacles") {
obstacleTiles = tileList; obstacleTiles = tileList;
} else if (layerName == "obstacles-8") {
obstacleTiles8 = tileList;
} else if (layerName == "decorations") { } else if (layerName == "decorations") {
decorationTiles = tileList; decorationTiles = tileList;
} else if (layerName == "background") { } else if (layerName == "background") {
@ -62,6 +62,7 @@ namespace SemiColinGames {
} }
} }
// Get all the obstacles into a single array, sorted by X. // Get all the obstacles into a single array, sorted by X.
obstacleTiles.AddRange(obstacleTiles8);
obstacleTiles.AddRange(hazardTiles); obstacleTiles.AddRange(hazardTiles);
obstacles = obstacleTiles.ToArray(); obstacles = obstacleTiles.ToArray();
Array.Sort(obstacles, Tile.CompareByX); Array.Sort(obstacles, Tile.CompareByX);
@ -79,11 +80,10 @@ namespace SemiColinGames {
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue)); CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
// Now add all the normal collisionTargets for every obstacle. // 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++) { for (int i = 0; i < obstacles.Length; i++) {
Vector2 center = new Vector2( Tile obstacle = obstacles[i];
obstacles[i].Position.Left + halfSize.X, obstacles[i].Position.Top + halfSize.Y); CollisionTargets[i + 1] = new AABB(
CollisionTargets[i + 1] = new AABB(center, halfSize, obstacles[i]); obstacle.Position.Center.ToVector2(), obstacle.Position.HalfSize(), obstacle);
} }
// Add a final synthetic collisionTarget on the right side of the world. // Add a final synthetic collisionTarget on the right side of the world.