From b85661e2c4a32bc94757c2d0197836bf696a307d Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 10 Mar 2020 17:10:09 -0400 Subject: [PATCH] add support for 8x8 obstacles --- Shared/ExtensionMethods.cs | 5 +++++ Shared/World.cs | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Shared/ExtensionMethods.cs b/Shared/ExtensionMethods.cs index c7b1a51..2adcdb0 100644 --- a/Shared/ExtensionMethods.cs +++ b/Shared/ExtensionMethods.cs @@ -18,6 +18,11 @@ namespace SemiColinGames { public static void Deconstruct(this Point point, out int x, out int 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 public static Vector2 CenteredOn(this SpriteFont font, string text, Point position) { Vector2 size = font.MeasureString(text); diff --git a/Shared/World.cs b/Shared/World.cs index ce31d7b..f3a2cbe 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -41,6 +41,7 @@ namespace SemiColinGames { List hazardTiles = new List(); List obstacleTiles = new List(); + List obstacleTiles8 = new List(); List decorationTiles = new List(); List backgroundTiles = new List(); foreach (JToken layer in root.SelectToken("layers").Children()) { @@ -55,6 +56,8 @@ namespace SemiColinGames { 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") { @@ -62,6 +65,7 @@ namespace SemiColinGames { } } // 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); @@ -79,11 +83,10 @@ namespace SemiColinGames { 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]); + 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.