From c2090786614d9c164da20119e6984c0799d23f71 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 30 Jan 2020 01:08:10 +0100 Subject: [PATCH] added a corner-based rectangleF method to fix the pathfinding demo --- Demos.Android/Demos.Android.csproj | 4 ---- Demos.DesktopGL/Demos.DesktopGL.csproj | 1 - Demos/PathfindingDemo.cs | 13 +++++++------ MLEM/Misc/RectangleF.cs | 8 ++++++++ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Demos.Android/Demos.Android.csproj b/Demos.Android/Demos.Android.csproj index 933f1da..ccee7e6 100644 --- a/Demos.Android/Demos.Android.csproj +++ b/Demos.Android/Demos.Android.csproj @@ -51,10 +51,6 @@ True - - ..\packages\MonoGame.Extended.3.7.0\lib\netstandard2.0\MonoGame.Extended.dll - True - ..\packages\Newtonsoft.Json.11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll diff --git a/Demos.DesktopGL/Demos.DesktopGL.csproj b/Demos.DesktopGL/Demos.DesktopGL.csproj index 99b189c..05930bc 100644 --- a/Demos.DesktopGL/Demos.DesktopGL.csproj +++ b/Demos.DesktopGL/Demos.DesktopGL.csproj @@ -7,7 +7,6 @@ - diff --git a/Demos/PathfindingDemo.cs b/Demos/PathfindingDemo.cs index 2fc2245..42b0ba2 100644 --- a/Demos/PathfindingDemo.cs +++ b/Demos/PathfindingDemo.cs @@ -53,8 +53,10 @@ namespace Demos { // print out some info Console.WriteLine("Pathfinding took " + this.pathfinder.LastTriesNeeded + " tries and " + this.pathfinder.LastTimeNeeded.TotalSeconds + " seconds"); - if (this.path == null) - Console.WriteLine("Couldn't find a path, press the left mouse button to try again"); + if (this.path == null) { + Console.WriteLine("Couldn't find a path, trying again"); + this.Init(); + } } public override void LoadContent() { @@ -88,10 +90,9 @@ namespace Demos { // in a real game, you'd obviously make your characters walk along the path instead of drawing it if (this.path != null) { for (var i = 1; i < this.path.Count; i++) { - var first = this.path[i - 1]; - var second = this.path[i]; - Vector2 firstPos = new Vector2(first.X + 0.25F, first.Y + 0.25F); - this.SpriteBatch.Draw(tex, new RectangleF(firstPos, new Vector2(second.X + 0.75F, second.Y + 0.75F) - firstPos), Color.Blue); + var (firstX, firstY) = this.path[i - 1]; + var (secondX, secondY) = this.path[i]; + this.SpriteBatch.Draw(tex, RectangleF.FromCorners(new Vector2(firstX + 0.25F, firstY + 0.25F), new Vector2(secondX + 0.75F, secondY + 0.75F)), Color.Blue); } } this.SpriteBatch.End(); diff --git a/MLEM/Misc/RectangleF.cs b/MLEM/Misc/RectangleF.cs index 3229d48..e709aa0 100644 --- a/MLEM/Misc/RectangleF.cs +++ b/MLEM/Misc/RectangleF.cs @@ -54,6 +54,14 @@ namespace MLEM.Misc { this.Height = size.Y; } + public static RectangleF FromCorners(Vector2 corner1, Vector2 corner2) { + var minX = Math.Min(corner1.X, corner2.X); + var minY = Math.Min(corner1.Y, corner2.Y); + var maxX = Math.Max(corner1.X, corner2.X); + var maxY = Math.Max(corner1.Y, corner2.Y); + return new RectangleF(minX, minY, maxX - minX, maxY - minY); + } + public static explicit operator Rectangle(RectangleF rect) { return new Rectangle(rect.X.Floor(), rect.Y.Floor(), rect.Width.Floor(), rect.Height.Floor()); }