From 20e583dfe6d3413cf674fb3f23dccef83d467e8c Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 15:36:39 +0200 Subject: [PATCH 01/25] Calculation of the shortest path --- .../PathFinding/ShortestPath.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs new file mode 100644 index 0000000..7aa8735 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding +{ + public class ShortestPath + { + public List Calculate(Planet start, Planet end) + { + var previous = new Dictionary(); + var queue = new Queue(); + + queue.Enqueue(start); + + while (queue.Count > 0) + { + var planet = queue.Dequeue(); + + foreach (var neighbour in planet.Neighbours) + { + if (previous.ContainsKey(neighbour)) continue; + + previous[neighbour] = planet; + queue.Enqueue(neighbour); + } + } + + var shortest = new List(); + var current = end; + while (!current.Equals(start)) + { + shortest.Add(current); + current = previous[current]; + } + + shortest.Add(start); + shortest.Reverse(); + + return shortest; + } + } +} \ No newline at end of file From ca720c2a0cc4e2648d8a86d84014f2cfdc61003a Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 15:37:38 +0200 Subject: [PATCH 02/25] Implementation of shortest path --- .../SimulationWindow.xaml.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index b56f370..ce9dab3 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -7,6 +8,7 @@ using Avans.FlatGalaxy.Models.CelestialBodies; using Avans.FlatGalaxy.Presentation.Extensions; using Avans.FlatGalaxy.Simulation; +using Avans.FlatGalaxy.Simulation.PathFinding; namespace Avans.FlatGalaxy.Presentation { @@ -52,6 +54,9 @@ public void Show(Galaxy galaxy) private void Draw(Galaxy galaxy) { GalaxyCanvas.Children.Clear(); + + var stepPlanets = galaxy.CelestialBodies.OfType().OrderByDescending(planet => planet.Radius).ToList(); + var steps = new ShortestPath().Calculate(stepPlanets[0], stepPlanets[1]); foreach (var celestialBody in galaxy.CelestialBodies) { @@ -59,7 +64,7 @@ private void Draw(Galaxy galaxy) { Height = celestialBody.Diameter, Width = celestialBody.Diameter, - Fill = new SolidColorBrush(celestialBody.Color.ToColor()), + Fill = new SolidColorBrush(steps.Contains(celestialBody) ? Colors.Chartreuse : celestialBody.Color.ToColor()), }; GalaxyCanvas.Children.Add(ellipse); @@ -73,7 +78,7 @@ private void Draw(Galaxy galaxy) { GalaxyCanvas.Children.Add(new Line { - Stroke = new SolidColorBrush(Colors.Blue), + Stroke = new SolidColorBrush((steps.Contains(planet) && steps.Contains(neighbour)) ? Colors.Chartreuse : Colors.Blue) , X1 = celestialBody.CenterX, Y1 = celestialBody.CenterY, X2 = neighbour.CenterX, From 3f225c5466effd921fa290c544b31e3558a2d2ff Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 15:37:45 +0200 Subject: [PATCH 03/25] Create .name --- .idea/.idea.Avans.FlatGalaxy/.idea/.name | 1 + 1 file changed, 1 insertion(+) create mode 100644 .idea/.idea.Avans.FlatGalaxy/.idea/.name diff --git a/.idea/.idea.Avans.FlatGalaxy/.idea/.name b/.idea/.idea.Avans.FlatGalaxy/.idea/.name new file mode 100644 index 0000000..e494467 --- /dev/null +++ b/.idea/.idea.Avans.FlatGalaxy/.idea/.name @@ -0,0 +1 @@ +Avans.FlatGalaxy \ No newline at end of file From 9c57054900413a4332896a51c25fa76d94837bdd Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 17:45:21 +0200 Subject: [PATCH 04/25] Renamed to finder --- .../PathFinding/CheapestPathFinder.cs | 42 +++++++++++++++++++ .../PathFinding/IPathFinder.cs | 10 +++++ ...{ShortestPath.cs => ShortestPathFinder.cs} | 2 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs rename src/Avans.FlatGalaxy.Simulation/PathFinding/{ShortestPath.cs => ShortestPathFinder.cs} (96%) diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs new file mode 100644 index 0000000..6f7607d --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding +{ + public class CheapestPathFinder : IPathFinder + { + public List Calculate(Planet start, Planet end) + { + // var previous = new Dictionary(); + // var queue = new Queue(); + // + // queue.Enqueue(start); + // + // while (queue.Count > 0) + // { + // var planet = queue.Dequeue(); + // + // foreach (var neighbour in planet.Neighbours) + // { + // if (previous.ContainsKey(neighbour)) continue; + // + // previous[neighbour] = planet; + // queue.Enqueue(neighbour); + // } + // } + // + // var shortest = new List(); + // var current = end; + // while (!current.Equals(start)) + // { + // shortest.Add(current); + // current = previous[current]; + // } + // + // shortest.Add(start); + // shortest.Reverse(); + // + // return shortest; + } + } +} \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs new file mode 100644 index 0000000..e4904fd --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding +{ + public interface IPathFinder + { + List Calculate(Planet start, Planet end); + } +} \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs similarity index 96% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs rename to src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs index 7aa8735..f47dcc6 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPath.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs @@ -8,7 +8,7 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { - public class ShortestPath + public class ShortestPathFinder : IPathFinder { public List Calculate(Planet start, Planet end) { From 6b903f7dd4d5abd7cb0575c2fb67e7353da777b5 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 17:45:33 +0200 Subject: [PATCH 05/25] Update SimulationWindow.xaml.cs --- src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index ce9dab3..d40f719 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -56,7 +56,7 @@ private void Draw(Galaxy galaxy) GalaxyCanvas.Children.Clear(); var stepPlanets = galaxy.CelestialBodies.OfType().OrderByDescending(planet => planet.Radius).ToList(); - var steps = new ShortestPath().Calculate(stepPlanets[0], stepPlanets[1]); + var steps = new ShortestPathFinder().Calculate(stepPlanets[0], stepPlanets[1]); foreach (var celestialBody in galaxy.CelestialBodies) { From 1ac76dc99b2be4acc405e6e7612064b799b68126 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 18:20:46 +0200 Subject: [PATCH 06/25] Make the draw method work using simulator --- .../SimulationWindow.xaml.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 635853d..09b6107 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -43,7 +43,7 @@ private void OnRender(object? sender, EventArgs e) { GalaxyCanvas.Children.Clear(); - if (_simulator.Galaxy != null) { Draw(_simulator.Galaxy); } + if (_simulator.Galaxy != null) { Draw(_simulator); } if (_simulator.QuadTree != null) { Draw(_simulator.QuadTree); } } @@ -55,19 +55,17 @@ public void Show(Galaxy galaxy) _simulator.Resume(); } - private void Draw(Galaxy galaxy) + private void Draw(ISimulator simulator) { GalaxyCanvas.Children.Clear(); - - var stepPlanets = galaxy.CelestialBodies.OfType().OrderByDescending(planet => planet.Radius).ToList(); - var steps = new ShortestPathFinder().Calculate(stepPlanets[0], stepPlanets[1]); - foreach (var celestialBody in galaxy.CelestialBodies) + + foreach (var celestialBody in simulator.Galaxy.CelestialBodies) { var ellipse = new Ellipse { Height = celestialBody.Diameter, Width = celestialBody.Diameter, - Fill = new SolidColorBrush(steps.Contains(celestialBody) ? Colors.Chartreuse : celestialBody.Color.ToColor()), + Fill = new SolidColorBrush(simulator.PathSteps?.Contains(celestialBody) ?? false ? Colors.Chartreuse : celestialBody.Color.ToColor()), }; GalaxyCanvas.Children.Add(ellipse); @@ -79,9 +77,15 @@ private void Draw(Galaxy galaxy) { foreach (var neighbour in planet.Neighbours) { + var isStepLine = false; + if (simulator.PathSteps != null) + { + isStepLine = simulator.PathSteps.Contains(planet) && simulator.PathSteps.Contains(neighbour); + } + GalaxyCanvas.Children.Add(new Line { - Stroke = new SolidColorBrush((steps.Contains(planet) && steps.Contains(neighbour)) ? Colors.Chartreuse : Colors.Blue) , + Stroke = new SolidColorBrush(isStepLine ? Colors.Chartreuse : Colors.Blue) , X1 = celestialBody.CenterX, Y1 = celestialBody.CenterY, X2 = neighbour.CenterX, From 9309e12371fd8a7e3bbf174aa816387af701d344 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 18:21:13 +0200 Subject: [PATCH 07/25] Update CheapestPathFinder.cs --- .../PathFinding/CheapestPathFinder.cs | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs index 6f7607d..055cf79 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs @@ -3,40 +3,11 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { - public class CheapestPathFinder : IPathFinder + public class CheapestPathFinder : PathFinder { - public List Calculate(Planet start, Planet end) + public override List Find(Planet start, Planet end) { - // var previous = new Dictionary(); - // var queue = new Queue(); - // - // queue.Enqueue(start); - // - // while (queue.Count > 0) - // { - // var planet = queue.Dequeue(); - // - // foreach (var neighbour in planet.Neighbours) - // { - // if (previous.ContainsKey(neighbour)) continue; - // - // previous[neighbour] = planet; - // queue.Enqueue(neighbour); - // } - // } - // - // var shortest = new List(); - // var current = end; - // while (!current.Equals(start)) - // { - // shortest.Add(current); - // current = previous[current]; - // } - // - // shortest.Add(start); - // shortest.Reverse(); - // - // return shortest; + return new(); } } } \ No newline at end of file From eeefa5167e1e7877b71b53d15f7e1e3250c21a07 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 18:21:28 +0200 Subject: [PATCH 08/25] Added Pathsteps to the simulator --- src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 6 +++++- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index 7702f5c..364bc7e 100644 --- a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs @@ -1,4 +1,6 @@ -using Avans.FlatGalaxy.Models; +using System.Collections.Generic; +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; using Avans.FlatGalaxy.Simulation.Data; namespace Avans.FlatGalaxy.Simulation @@ -13,6 +15,8 @@ public interface ISimulator QuadTree QuadTree { get; set; } + List PathSteps { get; set; } + void Resume(); void Pause(); diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index e9adf47..04f2636 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -1,9 +1,13 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; using Avans.FlatGalaxy.Simulation.Collision; using Avans.FlatGalaxy.Simulation.Data; +using Avans.FlatGalaxy.Simulation.PathFinding; namespace Avans.FlatGalaxy.Simulation { @@ -16,7 +20,7 @@ public class Simulator : ISimulator private Galaxy _galaxy; private DateTime _lastTick = DateTime.UtcNow; - private int _speed = 50; + private int _speed = 500; private bool _running = false; private CancellationTokenSource _source; @@ -39,6 +43,7 @@ public Galaxy Galaxy } public QuadTree QuadTree { get; set; } + public List PathSteps { get; set; } public void Resume() { @@ -69,6 +74,8 @@ public void Tick(CancellationToken token) Update(deltaTime); _collisionDetector.Detect(this); + + PathSteps = new ShortestPathFinder().Get(this); _lastTick = DateTime.UtcNow; From b2be443752d6ea730a909c250b32af6f98be8047 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 18:21:34 +0200 Subject: [PATCH 09/25] Delete IPathFinder.cs --- .../PathFinding/IPathFinder.cs | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs deleted file mode 100644 index e4904fd..0000000 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/IPathFinder.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Collections.Generic; -using Avans.FlatGalaxy.Models.CelestialBodies; - -namespace Avans.FlatGalaxy.Simulation.PathFinding -{ - public interface IPathFinder - { - List Calculate(Planet start, Planet end); - } -} \ No newline at end of file From f0199674a75ae08154a7e1e73137e26690ce382c Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 4 Oct 2021 18:21:48 +0200 Subject: [PATCH 10/25] Made more modular using abstract PathFinder --- .../PathFinding/PathFinder.cs | 17 +++++++++++++++++ .../PathFinding/ShortestPathFinder.cs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs new file mode 100644 index 0000000..605af58 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Linq; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding +{ + public abstract class PathFinder + { + public abstract List Find(Planet start, Planet end); + + public List Get(ISimulator simulator) + { + var stepPlanets = simulator.Galaxy.CelestialBodies.OfType().OrderByDescending(planet => planet.Radius).ToList(); + return stepPlanets.Count < 2 ? null : Find(stepPlanets[0], stepPlanets[1]); + } + } +} \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs index f47dcc6..3ec6fd3 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs @@ -8,9 +8,9 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { - public class ShortestPathFinder : IPathFinder + public class ShortestPathFinder : PathFinder { - public List Calculate(Planet start, Planet end) + public override List Find(Planet start, Planet end) { var previous = new Dictionary(); var queue = new Queue(); From f298039fd6429c3ed473e75b14cb1ada0e03fd57 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Mon, 11 Oct 2021 11:50:24 +0200 Subject: [PATCH 11/25] Mooi --- .../Factories/CelestialBodyFactory.cs | 5 +---- src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs | 1 - .../PathFinding/ShortestPathFinder.cs | 7 +------ src/Avans.FlatGalaxy.Simulation/Simulator.cs | 4 ++-- 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Avans.FlatGalaxy.Persistence/Factories/CelestialBodyFactory.cs b/src/Avans.FlatGalaxy.Persistence/Factories/CelestialBodyFactory.cs index 30c96b0..0fa2e19 100644 --- a/src/Avans.FlatGalaxy.Persistence/Factories/CelestialBodyFactory.cs +++ b/src/Avans.FlatGalaxy.Persistence/Factories/CelestialBodyFactory.cs @@ -23,9 +23,7 @@ public CelestialBody Create(string type, double x, double y, double vx, double v }; } - private static ICollisionState GetCollisionState(string collisionName) - { - return collisionName.ToLower() switch + private static ICollisionState GetCollisionState(string collisionName) => collisionName.ToLower() switch { "blink" => new BlinkState(), "bounce" => new BounceState(), @@ -34,6 +32,5 @@ private static ICollisionState GetCollisionState(string collisionName) "grow" => new GrowState(), _ => new NullCollisionState() }; - } } } diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 09b6107..7dbad97 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -8,7 +8,6 @@ using Avans.FlatGalaxy.Models.CelestialBodies; using Avans.FlatGalaxy.Presentation.Extensions; using Avans.FlatGalaxy.Simulation; -using Avans.FlatGalaxy.Simulation.PathFinding; using Avans.FlatGalaxy.Simulation.Data; namespace Avans.FlatGalaxy.Presentation diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs index 3ec6fd3..b2b3c55 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Avans.FlatGalaxy.Models; +using System.Collections.Generic; using Avans.FlatGalaxy.Models.CelestialBodies; namespace Avans.FlatGalaxy.Simulation.PathFinding diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 04f2636..d34a323 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; using Avans.FlatGalaxy.Models; @@ -20,7 +19,7 @@ public class Simulator : ISimulator private Galaxy _galaxy; private DateTime _lastTick = DateTime.UtcNow; - private int _speed = 500; + private int _speed = 50; private bool _running = false; private CancellationTokenSource _source; @@ -76,6 +75,7 @@ public void Tick(CancellationToken token) _collisionDetector.Detect(this); PathSteps = new ShortestPathFinder().Get(this); + // PathSteps = new CheapestPathFinder().Get(this); _lastTick = DateTime.UtcNow; From ef3459711653c99f0c44c0817fd648eb401ec0c3 Mon Sep 17 00:00:00 2001 From: Tommy Hosewol Date: Tue, 26 Oct 2021 15:08:40 +0200 Subject: [PATCH 12/25] Worked on dijkstra --- ...athFinder.cs => BreadthFirstPathFinder.cs} | 2 +- .../PathFinding/CheapestPathFinder.cs | 13 ---- .../PathFinding/DijkstraPathFinder.cs | 60 +++++++++++++++++++ src/Avans.FlatGalaxy.Simulation/Simulator.cs | 2 +- 4 files changed, 62 insertions(+), 15 deletions(-) rename src/Avans.FlatGalaxy.Simulation/PathFinding/{ShortestPathFinder.cs => BreadthFirstPathFinder.cs} (95%) delete mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs similarity index 95% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs rename to src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs index b2b3c55..710469f 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/ShortestPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs @@ -3,7 +3,7 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { - public class ShortestPathFinder : PathFinder + public class BreadthFirstPathFinder : PathFinder { public override List Find(Planet start, Planet end) { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs deleted file mode 100644 index 055cf79..0000000 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/CheapestPathFinder.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; -using Avans.FlatGalaxy.Models.CelestialBodies; - -namespace Avans.FlatGalaxy.Simulation.PathFinding -{ - public class CheapestPathFinder : PathFinder - { - public override List Find(Planet start, Planet end) - { - return new(); - } - } -} \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs new file mode 100644 index 0000000..fd26121 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding +{ + public class DijkstraPathFinder : PathFinder + { + public override List Find(Planet start, Planet end) + { + var steps = new Dictionary(); + var queue = new Queue(); + Planet previous = null; + var previousDistance = 0.0; + + // start with the first planet + queue.Enqueue(start); + + // check distance to all neighbours + while (queue.Count > 0) + { + var planet = queue.Dequeue(); + + foreach (var neighbour in planet.Neighbours) + { + if (previous != neighbour) + { + var distance = GetDistance(planet, neighbour); + steps.Add(neighbour, new Conn(distance, planet)); + + queue.Enqueue(neighbour); + } + } + + previous = planet; + } + + // add those neighbours to queue + + // + + return null; + } + + private static double GetDistance(Planet origin, Planet target) => Math.Pow(origin.CenterX - target.CenterX, 2) + Math.Pow(origin.CenterY - target.CenterY, 2); + + class Conn + { + public double distance; + public Planet previous; + + public Conn(double distance, Planet previous) + { + this.distance = distance; + this.previous = previous; + } + } + } +} \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index d34a323..a23231b 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -74,7 +74,7 @@ public void Tick(CancellationToken token) _collisionDetector.Detect(this); - PathSteps = new ShortestPathFinder().Get(this); + PathSteps = new BreadthFirstPathFinder().Get(this); // PathSteps = new CheapestPathFinder().Get(this); _lastTick = DateTime.UtcNow; From 93d7759c0b02b1883cc35f21f43b812cf2e3898c Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:24:02 +0200 Subject: [PATCH 13/25] Update SimulationWindow.xaml.cs --- .../SimulationWindow.xaml.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 35ae88b..c73e12f 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; @@ -9,6 +10,7 @@ using Avans.FlatGalaxy.Presentation.Extensions; using Avans.FlatGalaxy.Simulation; using Avans.FlatGalaxy.Simulation.Data; +using Color=System.Drawing.Color; namespace Avans.FlatGalaxy.Presentation { @@ -41,8 +43,8 @@ private void OnRender(object? sender, EventArgs e) { GalaxyCanvas.Children.Clear(); - if (_simulator.Galaxy != null) { Draw(_simulator); } - if (_simulator.QuadTree != null) { Draw(_simulator.QuadTree); } + if (_simulator?.Galaxy != null) { Draw(_simulator.Galaxy, _simulator.PathSteps); } + if (_simulator?.QuadTree != null) { Draw(_simulator.QuadTree); } } public void Show(Galaxy galaxy) @@ -53,17 +55,19 @@ public void Show(Galaxy galaxy) _simulator.Resume(); } - private void Draw(ISimulator simulator) + private void Draw(Galaxy galaxy, ICollection pathSteps) { + var pathColor = Colors.Chartreuse; + GalaxyCanvas.Children.Clear(); - foreach (var celestialBody in simulator.Galaxy.CelestialBodies) + foreach (var celestialBody in galaxy.CelestialBodies) { var ellipse = new Ellipse { Height = celestialBody.Diameter, Width = celestialBody.Diameter, - Fill = new SolidColorBrush(simulator.PathSteps?.Contains(celestialBody) ?? false ? Colors.Chartreuse : celestialBody.Color.ToColor()), + Fill = new SolidColorBrush(pathSteps?.Contains(celestialBody) ?? false ? pathColor : celestialBody.Color.ToColor()), }; GalaxyCanvas.Children.Add(ellipse); @@ -75,15 +79,11 @@ private void Draw(ISimulator simulator) { foreach (var neighbour in planet.Neighbours) { - var isStepLine = false; - if (simulator.PathSteps != null) - { - isStepLine = simulator.PathSteps.Contains(planet) && simulator.PathSteps.Contains(neighbour); - } - + var isStepLine = pathSteps != null && pathSteps.Contains(planet) && pathSteps.Contains(neighbour); + GalaxyCanvas.Children.Add(new Line { - Stroke = new SolidColorBrush(isStepLine ? Colors.Chartreuse : Colors.Blue) , + Stroke = new SolidColorBrush(isStepLine ? pathColor : Colors.Blue), X1 = celestialBody.CenterX, Y1 = celestialBody.CenterY, X2 = neighbour.CenterX, From bf971b1ff3ba32180a0ef1e86bb7e6b54b7a0021 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:24:13 +0200 Subject: [PATCH 14/25] Update PathFinder --- .../PathFinding/BreadthFirstPathFinder.cs | 2 +- src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs index 710469f..e5cd2ca 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs @@ -5,7 +5,7 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { public class BreadthFirstPathFinder : PathFinder { - public override List Find(Planet start, Planet end) + protected override List Find(Planet start, Planet end) { var previous = new Dictionary(); var queue = new Queue(); diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs index 605af58..d764a5a 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs @@ -6,7 +6,7 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { public abstract class PathFinder { - public abstract List Find(Planet start, Planet end); + protected abstract List Find(Planet start, Planet end); public List Get(ISimulator simulator) { From 79b4b9011de84b683bbed5391c6e22ec2709707b Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:26:37 +0200 Subject: [PATCH 15/25] Update DijkstraPathFinder.cs --- .../PathFinding/DijkstraPathFinder.cs | 48 +------------------ 1 file changed, 2 insertions(+), 46 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs index fd26121..41a7c28 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs @@ -1,60 +1,16 @@ using System; using System.Collections.Generic; -using System.Linq; using Avans.FlatGalaxy.Models.CelestialBodies; namespace Avans.FlatGalaxy.Simulation.PathFinding { public class DijkstraPathFinder : PathFinder { - public override List Find(Planet start, Planet end) + protected override List Find(Planet start, Planet end) { - var steps = new Dictionary(); - var queue = new Queue(); - Planet previous = null; - var previousDistance = 0.0; - - // start with the first planet - queue.Enqueue(start); - - // check distance to all neighbours - while (queue.Count > 0) - { - var planet = queue.Dequeue(); - - foreach (var neighbour in planet.Neighbours) - { - if (previous != neighbour) - { - var distance = GetDistance(planet, neighbour); - steps.Add(neighbour, new Conn(distance, planet)); - - queue.Enqueue(neighbour); - } - } - - previous = planet; - } - - // add those neighbours to queue - - // - return null; } private static double GetDistance(Planet origin, Planet target) => Math.Pow(origin.CenterX - target.CenterX, 2) + Math.Pow(origin.CenterY - target.CenterY, 2); - - class Conn - { - public double distance; - public Planet previous; - - public Conn(double distance, Planet previous) - { - this.distance = distance; - this.previous = previous; - } - } } -} \ No newline at end of file +} From 9cdd7f674b6a330439110933074683e073770d95 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 14:42:18 +0200 Subject: [PATCH 16/25] Fix Dijkstra --- .../CelestialBodies/CelestialBody.cs | 2 + .../SimulationWindow.xaml.cs | 1 - .../PathFinding/BreadthFirstPathFinder.cs | 2 +- .../PathFinding/Data/DijkstraEdge.cs | 15 +++++++ .../PathFinding/Data/DijkstraGraph.cs | 26 +++++++++++ .../PathFinding/Data/DijkstraNode.cs | 24 ++++++++++ .../PathFinding/DijkstraPathFinder.cs | 44 ++++++++++++++++--- .../PathFinding/PathFinder.cs | 7 +-- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 4 +- 9 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs index a0750bd..779575d 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs @@ -56,6 +56,8 @@ public void Collide(CelestialBody other) CollisionState.Collide(this, other); } + public double DistanceTo(CelestialBody target) => Math.Pow(CenterX - target.CenterX, 2) + Math.Pow(CenterY - target.CenterY, 2); + public IDisposable Subscribe(IObserver observer) { _observers.Add(observer); diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index c73e12f..f0d4cc2 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -10,7 +10,6 @@ using Avans.FlatGalaxy.Presentation.Extensions; using Avans.FlatGalaxy.Simulation; using Avans.FlatGalaxy.Simulation.Data; -using Color=System.Drawing.Color; namespace Avans.FlatGalaxy.Presentation { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs index e5cd2ca..684b4e6 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs @@ -5,7 +5,7 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { public class BreadthFirstPathFinder : PathFinder { - protected override List Find(Planet start, Planet end) + protected override List Find(Planet start, Planet end, List planets) { var previous = new Dictionary(); var queue = new Queue(); diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs new file mode 100644 index 0000000..b151e77 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs @@ -0,0 +1,15 @@ +namespace Avans.FlatGalaxy.Simulation.PathFinding.Data +{ + public class DijkstraEdge + { + public DijkstraNode Node { get; set; } + + public double Weight { get; set; } + + public DijkstraEdge(DijkstraNode node, double weight) + { + Node = node; + Weight = weight; + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs new file mode 100644 index 0000000..45788d2 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.Linq; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding.Data +{ + public class DijkstraGraph + { + public List Nodes { get; } + + public DijkstraGraph(List planets) + { + Nodes = planets.Select(planet => new DijkstraNode(planet)).ToList(); + + foreach (var node in Nodes) + { + foreach (var neighbour in node.Planet.Neighbours) + { + var neighbourNode = Nodes.First(node1 => node1.Planet == neighbour); + + node.Neighbours.Add(new(neighbourNode, node.Planet.DistanceTo(neighbour))); + } + } + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs new file mode 100644 index 0000000..7fa13a1 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.PathFinding.Data +{ + public class DijkstraNode + { + public Planet Planet { get; } + + public KeyValuePair Weight { get; set; } + + public List Neighbours { get; } + + public bool Visited { get; set; } + + public DijkstraNode(Planet planet) + { + Neighbours = new(); + Weight = new(null, double.PositiveInfinity); + Planet = planet; + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs index 41a7c28..7fc635c 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs @@ -1,16 +1,48 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Linq; using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Simulation.PathFinding.Data; namespace Avans.FlatGalaxy.Simulation.PathFinding { public class DijkstraPathFinder : PathFinder { - protected override List Find(Planet start, Planet end) + protected override List Find(Planet start, Planet end, List planets) { - return null; - } + var graph = new DijkstraGraph(planets); + var startNode = graph.Nodes.First(node => node.Planet == start); + startNode.Weight = new(null, 0); + var unvisited = new List { startNode }; + + while (unvisited.Any()) + { + var node = unvisited.OrderBy(node1 => node1.Weight.Value).First(); + + foreach (var neighbour in node.Neighbours) + { + if (neighbour.Weight + node.Weight.Value < neighbour.Node.Weight.Value) + { + neighbour.Node.Weight = new(node.Planet, neighbour.Weight + node.Weight.Value); - private static double GetDistance(Planet origin, Planet target) => Math.Pow(origin.CenterX - target.CenterX, 2) + Math.Pow(origin.CenterY - target.CenterY, 2); + if (!unvisited.Contains(neighbour.Node) && !neighbour.Node.Visited) + { + unvisited.Add(neighbour.Node); + } + } + } + node.Visited = true; + unvisited.Remove(node); + } + + var endNode = graph.Nodes.First(node => node.Planet == end); + var path = new List { endNode }; + while (endNode.Weight.Key != null) + { + endNode = endNode.Neighbours.OrderBy(edge => edge.Node.Weight.Value).First().Node; + path.Add(endNode); + } + + return path.Select(node => node.Planet).ToList(); + } } } diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs b/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs index d764a5a..142563d 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs @@ -6,12 +6,13 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding { public abstract class PathFinder { - protected abstract List Find(Planet start, Planet end); + protected abstract List Find(Planet start, Planet end, List planets); public List Get(ISimulator simulator) { - var stepPlanets = simulator.Galaxy.CelestialBodies.OfType().OrderByDescending(planet => planet.Radius).ToList(); - return stepPlanets.Count < 2 ? null : Find(stepPlanets[0], stepPlanets[1]); + var planets = simulator.Galaxy.CelestialBodies.OfType().ToList(); + var planets2 = planets.OrderByDescending(planet => planet.Radius).ToList(); + return planets2.Count < 2 ? null : Find(planets2[0], planets2[1], planets); } } } \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index aaf6f8d..73daa99 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -122,8 +122,8 @@ private void Tick(CancellationToken token) _collisionHandler.Detect(this); - PathSteps = new BreadthFirstPathFinder().Get(this); - // PathSteps = new CheapestPathFinder().Get(this); + // PathSteps = new BreadthFirstPathFinder().Get(this); + PathSteps = new DijkstraPathFinder().Get(this); _lastTick = DateTime.UtcNow; if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= ISimulator.BookmarkTime) From 67c2afb02e324e5594244c59952e54e5604f18e2 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 14:45:28 +0200 Subject: [PATCH 17/25] Relocate Files --- .../{PathFinding => Path}/BreadthFirstPathFinder.cs | 2 +- .../{PathFinding => Path}/Data/DijkstraEdge.cs | 2 +- .../{PathFinding => Path}/Data/DijkstraGraph.cs | 2 +- .../{PathFinding => Path}/Data/DijkstraNode.cs | 5 ++--- .../{PathFinding => Path}/DijkstraPathFinder.cs | 4 ++-- .../{PathFinding => Path}/PathFinder.cs | 2 +- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 2 +- 7 files changed, 9 insertions(+), 10 deletions(-) rename src/Avans.FlatGalaxy.Simulation/{PathFinding => Path}/BreadthFirstPathFinder.cs (95%) rename src/Avans.FlatGalaxy.Simulation/{PathFinding => Path}/Data/DijkstraEdge.cs (82%) rename src/Avans.FlatGalaxy.Simulation/{PathFinding => Path}/Data/DijkstraGraph.cs (92%) rename src/Avans.FlatGalaxy.Simulation/{PathFinding => Path}/Data/DijkstraNode.cs (81%) rename src/Avans.FlatGalaxy.Simulation/{PathFinding => Path}/DijkstraPathFinder.cs (94%) rename src/Avans.FlatGalaxy.Simulation/{PathFinding => Path}/PathFinder.cs (92%) diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathFinder.cs similarity index 95% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs rename to src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathFinder.cs index 684b4e6..48e2e4a 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/BreadthFirstPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathFinder.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using Avans.FlatGalaxy.Models.CelestialBodies; -namespace Avans.FlatGalaxy.Simulation.PathFinding +namespace Avans.FlatGalaxy.Simulation.Path { public class BreadthFirstPathFinder : PathFinder { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs b/src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraEdge.cs similarity index 82% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs rename to src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraEdge.cs index b151e77..6be95cc 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraEdge.cs @@ -1,4 +1,4 @@ -namespace Avans.FlatGalaxy.Simulation.PathFinding.Data +namespace Avans.FlatGalaxy.Simulation.Path.Data { public class DijkstraEdge { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs b/src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraGraph.cs similarity index 92% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs rename to src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraGraph.cs index 45788d2..03eb464 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraGraph.cs @@ -2,7 +2,7 @@ using System.Linq; using Avans.FlatGalaxy.Models.CelestialBodies; -namespace Avans.FlatGalaxy.Simulation.PathFinding.Data +namespace Avans.FlatGalaxy.Simulation.Path.Data { public class DijkstraGraph { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs b/src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraNode.cs similarity index 81% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs rename to src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraNode.cs index 7fa13a1..8abf33c 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/Data/DijkstraNode.cs @@ -1,8 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Avans.FlatGalaxy.Models.CelestialBodies; -namespace Avans.FlatGalaxy.Simulation.PathFinding.Data +namespace Avans.FlatGalaxy.Simulation.Path.Data { public class DijkstraNode { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathFinder.cs similarity index 94% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs rename to src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathFinder.cs index 7fc635c..cd7fea5 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathFinder.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; using System.Linq; using Avans.FlatGalaxy.Models.CelestialBodies; -using Avans.FlatGalaxy.Simulation.PathFinding.Data; +using Avans.FlatGalaxy.Simulation.Path.Data; -namespace Avans.FlatGalaxy.Simulation.PathFinding +namespace Avans.FlatGalaxy.Simulation.Path { public class DijkstraPathFinder : PathFinder { diff --git a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs b/src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs similarity index 92% rename from src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs rename to src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs index 142563d..08d8ca4 100644 --- a/src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs @@ -2,7 +2,7 @@ using System.Linq; using Avans.FlatGalaxy.Models.CelestialBodies; -namespace Avans.FlatGalaxy.Simulation.PathFinding +namespace Avans.FlatGalaxy.Simulation.Path { public abstract class PathFinder { diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 73daa99..6b9381b 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -8,11 +8,11 @@ using Avans.FlatGalaxy.Models.CelestialBodies; using Avans.FlatGalaxy.Simulation.Collision; using Avans.FlatGalaxy.Simulation.Data; -using Avans.FlatGalaxy.Simulation.PathFinding; using Avans.FlatGalaxy.Models.CelestialBodies.States; using Avans.FlatGalaxy.Simulation.Bookmark; using Avans.FlatGalaxy.Simulation.Bookmark.Common; using Avans.FlatGalaxy.Simulation.Extensions; +using Avans.FlatGalaxy.Simulation.Path; namespace Avans.FlatGalaxy.Simulation { From 8bca3d54d96b47f42b24a632c49ea6184c21bc67 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 15:17:56 +0200 Subject: [PATCH 18/25] Update --- .../SimulationWindow.xaml.cs | 6 ++-- .../Collision/CollisionHandler.cs | 27 ++++----------- .../Commands/PathSwitchCommand.cs | 12 +++++++ .../Common/ImplementationSwapper.cs | 31 +++++++++++++++++ src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 2 ++ ...Finder.cs => BreadthFirstPathAlgorithm.cs} | 14 ++++---- ...PathFinder.cs => DijkstraPathAlgorithm.cs} | 7 ++-- .../Path/IPathAlgorithm.cs | 10 ++++++ .../Path/PathFinder.cs | 18 ---------- .../Path/PathHandler.cs | 33 +++++++++++++++++++ src/Avans.FlatGalaxy.Simulation/Simulator.cs | 19 +++++++---- .../CollisionTests.cs | 4 +-- 12 files changed, 124 insertions(+), 59 deletions(-) create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/PathSwitchCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Common/ImplementationSwapper.cs rename src/Avans.FlatGalaxy.Simulation/Path/{BreadthFirstPathFinder.cs => BreadthFirstPathAlgorithm.cs} (82%) rename src/Avans.FlatGalaxy.Simulation/Path/{DijkstraPathFinder.cs => DijkstraPathAlgorithm.cs} (87%) create mode 100644 src/Avans.FlatGalaxy.Simulation/Path/IPathAlgorithm.cs delete mode 100644 src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Path/PathHandler.cs diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index f0d4cc2..89b58da 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -42,8 +42,10 @@ private void OnRender(object? sender, EventArgs e) { GalaxyCanvas.Children.Clear(); - if (_simulator?.Galaxy != null) { Draw(_simulator.Galaxy, _simulator.PathSteps); } - if (_simulator?.QuadTree != null) { Draw(_simulator.QuadTree); } + if (_simulator == null) return; + + if (_simulator.Galaxy != null) { Draw(_simulator.Galaxy, _simulator.PathSteps); } + if (_simulator.CollisionVisible && _simulator.QuadTree != null) { Draw(_simulator.QuadTree); } } public void Show(Galaxy galaxy) diff --git a/src/Avans.FlatGalaxy.Simulation/Collision/CollisionHandler.cs b/src/Avans.FlatGalaxy.Simulation/Collision/CollisionHandler.cs index 2e7dff5..571621d 100644 --- a/src/Avans.FlatGalaxy.Simulation/Collision/CollisionHandler.cs +++ b/src/Avans.FlatGalaxy.Simulation/Collision/CollisionHandler.cs @@ -1,36 +1,23 @@ using System.Collections.Generic; using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Simulation.Common; namespace Avans.FlatGalaxy.Simulation.Collision { - public class CollisionHandler + public class CollisionHandler : ImplementationSwapper { - private readonly List _detectors; private readonly List> _collisions; - private int _currentDetector; - public CollisionHandler() { - _detectors = new List - { - new QuadTreeCollisionDetector(), - new NaiveCollisionDetector() - }; - _collisions = new List>(); - } - - public void Toggle() - { - if (++_currentDetector >= _detectors.Count) - { - _currentDetector = 0; - } + Add(new QuadTreeCollisionDetector()); + Add(new NaiveCollisionDetector()); + _collisions = new(); } public void Detect(ISimulator simulator) { - _detectors[_currentDetector].Detect(simulator, this); + Current.Detect(simulator, this); TriggerEnd(); } @@ -39,7 +26,7 @@ public void AddCollision(CelestialBody body1, CelestialBody body2) { if (body1 == body2) return; - var pair = body1.GetHashCode() < body2.GetHashCode() ? new KeyValuePair(body1, body2) : new KeyValuePair(body2, body1); + KeyValuePair pair = body1.GetHashCode() < body2.GetHashCode() ? new(body1, body2) : new(body2, body1); if (!_collisions.Contains(pair)) { diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/PathSwitchCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/PathSwitchCommand.cs new file mode 100644 index 0000000..6dae47d --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/PathSwitchCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class PathSwitchCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.SwitchPathAlgo(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Common/ImplementationSwapper.cs b/src/Avans.FlatGalaxy.Simulation/Common/ImplementationSwapper.cs new file mode 100644 index 0000000..f279a7d --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Common/ImplementationSwapper.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; + +namespace Avans.FlatGalaxy.Simulation.Common +{ + public class ImplementationSwapper + { + private readonly List _items; + + private int _cIndex; + + public ImplementationSwapper() + { + _items = new(); + } + + protected void Add(T item) + { + _items.Add(item); + } + + public void Next() + { + if (++_cIndex >= _items.Count) + { + _cIndex = 0; + } + } + + public T Current => _items[_cIndex]; + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index edc8a52..b496fd7 100644 --- a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs @@ -35,6 +35,8 @@ public interface ISimulator void SwitchCollisionAlgo(); + void SwitchPathAlgo(); + void AddAsteroid(); void RemoveAsteroid(); diff --git a/src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathAlgorithm.cs similarity index 82% rename from src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathFinder.cs rename to src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathAlgorithm.cs index 48e2e4a..895236f 100644 --- a/src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/BreadthFirstPathAlgorithm.cs @@ -3,23 +3,23 @@ namespace Avans.FlatGalaxy.Simulation.Path { - public class BreadthFirstPathFinder : PathFinder + public class BreadthFirstPathAlgorithm : IPathAlgorithm { - protected override List Find(Planet start, Planet end, List planets) + public List Find(Planet start, Planet end, List planets) { var previous = new Dictionary(); var queue = new Queue(); - + queue.Enqueue(start); while (queue.Count > 0) { var planet = queue.Dequeue(); - + foreach (var neighbour in planet.Neighbours) { if (previous.ContainsKey(neighbour)) continue; - + previous[neighbour] = planet; queue.Enqueue(neighbour); } @@ -32,11 +32,11 @@ protected override List Find(Planet start, Planet end, List plan shortest.Add(current); current = previous[current]; } - + shortest.Add(start); shortest.Reverse(); return shortest; } } -} \ No newline at end of file +} diff --git a/src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathFinder.cs b/src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathAlgorithm.cs similarity index 87% rename from src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathFinder.cs rename to src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathAlgorithm.cs index cd7fea5..5493174 100644 --- a/src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathFinder.cs +++ b/src/Avans.FlatGalaxy.Simulation/Path/DijkstraPathAlgorithm.cs @@ -5,9 +5,9 @@ namespace Avans.FlatGalaxy.Simulation.Path { - public class DijkstraPathFinder : PathFinder + public class DijkstraPathAlgorithm : IPathAlgorithm { - protected override List Find(Planet start, Planet end, List planets) + public List Find(Planet start, Planet end, List planets) { var graph = new DijkstraGraph(planets); var startNode = graph.Nodes.First(node => node.Planet == start); @@ -38,7 +38,8 @@ protected override List Find(Planet start, Planet end, List plan var path = new List { endNode }; while (endNode.Weight.Key != null) { - endNode = endNode.Neighbours.OrderBy(edge => edge.Node.Weight.Value).First().Node; + endNode = endNode.Neighbours.OrderBy(edge => edge.Node.Weight.Value).FirstOrDefault()?.Node; + if (endNode == null) return null; path.Add(endNode); } diff --git a/src/Avans.FlatGalaxy.Simulation/Path/IPathAlgorithm.cs b/src/Avans.FlatGalaxy.Simulation/Path/IPathAlgorithm.cs new file mode 100644 index 0000000..bcaab1f --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Path/IPathAlgorithm.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.Path +{ + public interface IPathAlgorithm + { + public abstract List Find(Planet start, Planet end, List planets); + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs b/src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs deleted file mode 100644 index 08d8ca4..0000000 --- a/src/Avans.FlatGalaxy.Simulation/Path/PathFinder.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Avans.FlatGalaxy.Models.CelestialBodies; - -namespace Avans.FlatGalaxy.Simulation.Path -{ - public abstract class PathFinder - { - protected abstract List Find(Planet start, Planet end, List planets); - - public List Get(ISimulator simulator) - { - var planets = simulator.Galaxy.CelestialBodies.OfType().ToList(); - var planets2 = planets.OrderByDescending(planet => planet.Radius).ToList(); - return planets2.Count < 2 ? null : Find(planets2[0], planets2[1], planets); - } - } -} \ No newline at end of file diff --git a/src/Avans.FlatGalaxy.Simulation/Path/PathHandler.cs b/src/Avans.FlatGalaxy.Simulation/Path/PathHandler.cs new file mode 100644 index 0000000..4782738 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Path/PathHandler.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Linq; +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Simulation.Common; + +namespace Avans.FlatGalaxy.Simulation.Path +{ + public class PathHandler : ImplementationSwapper + { + public PathHandler() + { + Add(new DijkstraPathAlgorithm()); + Add(new BreadthFirstPathAlgorithm()); + } + + public List Find(Galaxy galaxy) + { + var planets = galaxy.CelestialBodies.OfType().OrderByDescending(planet => planet.Radius).ToList(); + return planets.Count < 2 ? null : Find(planets[0], planets[1], galaxy); + } + + public List Find(Planet start, Planet end, Galaxy galaxy) + { + return Find(start, end, galaxy.CelestialBodies.OfType().ToList()); + } + + public List Find(Planet start, Planet end, List planets) + { + return planets.Contains(start) && planets.Contains(end) ? Current.Find(start, end, planets) : null; + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 6b9381b..a33740a 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -31,12 +31,14 @@ public class Simulator : ISimulator private CancellationTokenSource _source; private CancellationToken _token; private readonly CollisionHandler _collisionHandler; + private readonly PathHandler _pathHandler; private readonly ICaretaker _caretaker; public Simulator(Galaxy galaxy) { Galaxy = galaxy; - _collisionHandler = new CollisionHandler(); + _collisionHandler = new(); + _pathHandler = new(); _caretaker = new SimulatorCaretaker(this); } @@ -53,7 +55,7 @@ public void Resume() if (_running) return; _running = true; - _source = new CancellationTokenSource(); + _source = new(); _token = _source.Token; _lastTick = DateTime.UtcNow; _lastBookmark = DateTime.UtcNow; @@ -83,7 +85,12 @@ public void SpeedDown(double speed) public void SwitchCollisionAlgo() { - _collisionHandler.Toggle(); + _collisionHandler.Next(); + } + + public void SwitchPathAlgo() + { + _pathHandler.Next(); } public void ToggleCollisionVisibility() @@ -121,16 +128,14 @@ private void Tick(CancellationToken token) Update(deltaTime); _collisionHandler.Detect(this); - - // PathSteps = new BreadthFirstPathFinder().Get(this); - PathSteps = new DijkstraPathFinder().Get(this); + PathSteps = _pathHandler.Find(Galaxy); - _lastTick = DateTime.UtcNow; if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= ISimulator.BookmarkTime) { _caretaker.Save(); _lastBookmark = DateTime.UtcNow; } + _lastTick = DateTime.UtcNow; var nextTick = (int)(TpsTime - tickTime); await Task.Delay(nextTick >= 0 ? nextTick : 0, token); diff --git a/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs b/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs index 35e7b57..522fb03 100644 --- a/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs +++ b/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs @@ -21,9 +21,9 @@ public void Test_Collision() var handler = new CollisionHandler(); handler.Detect(sim); - handler.Toggle(); + handler.Next(); handler.Detect(sim); - handler.Toggle(); + handler.Next(); handler.Detect(sim); mockCollisionState.Verify(state => state.Collide(body1, body2), Times.Once); From 139cf1e0a6cc7e042f45321601235c69938d37a1 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 15:29:06 +0200 Subject: [PATCH 19/25] Move MapNeighbours to Galaxy --- src/Avans.FlatGalaxy.Models/Galaxy.cs | 12 ++++++++++++ .../Parsers/ConfigurationParserBase.cs | 11 ----------- .../Parsers/CsvConfigurationParser.cs | 2 +- .../Parsers/XmlConfigurationParser.cs | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Avans.FlatGalaxy.Models/Galaxy.cs b/src/Avans.FlatGalaxy.Models/Galaxy.cs index 39d514b..3258a42 100644 --- a/src/Avans.FlatGalaxy.Models/Galaxy.cs +++ b/src/Avans.FlatGalaxy.Models/Galaxy.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; using Avans.FlatGalaxy.Models.CelestialBodies; using Avans.FlatGalaxy.Models.CelestialBodies.States; @@ -42,6 +43,17 @@ public void Remove(CelestialBody celestialBody) _celestialBodies.Remove(celestialBody); } + public void MapNeighbours(IDictionary planetNeighbours) + { + foreach (var (planet, neighbours) in planetNeighbours) + { + foreach (var neighbour in neighbours) + { + planet.Neighbours.Add(CelestialBodies.OfType().First(b => b.Name == neighbour)); + } + } + } + public void OnCompleted() { throw new NotImplementedException(); diff --git a/src/Avans.FlatGalaxy.Persistence/Parsers/ConfigurationParserBase.cs b/src/Avans.FlatGalaxy.Persistence/Parsers/ConfigurationParserBase.cs index 36453e4..59c05fd 100644 --- a/src/Avans.FlatGalaxy.Persistence/Parsers/ConfigurationParserBase.cs +++ b/src/Avans.FlatGalaxy.Persistence/Parsers/ConfigurationParserBase.cs @@ -18,16 +18,5 @@ protected ConfigurationParserBase(ICelestialBodyFactory celestialBodyFactory) public abstract Galaxy Parse(string content); public abstract bool CanParse(string content); - - protected static void MapNeighbours(Galaxy galaxy, Dictionary planetNeighbours) - { - foreach (var (planet, neighbours) in planetNeighbours) - { - foreach (var neighbour in neighbours) - { - planet.Neighbours.Add(galaxy.CelestialBodies.OfType().First(b => b.Name == neighbour)); - } - } - } } } diff --git a/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs b/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs index 19129bc..14f38d4 100644 --- a/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs +++ b/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs @@ -56,7 +56,7 @@ public override Galaxy Parse(string content) } } - MapNeighbours(galaxy, planetNeighbours); + galaxy.MapNeighbours(planetNeighbours); return galaxy; } } diff --git a/src/Avans.FlatGalaxy.Persistence/Parsers/XmlConfigurationParser.cs b/src/Avans.FlatGalaxy.Persistence/Parsers/XmlConfigurationParser.cs index eabb8f4..c94f410 100644 --- a/src/Avans.FlatGalaxy.Persistence/Parsers/XmlConfigurationParser.cs +++ b/src/Avans.FlatGalaxy.Persistence/Parsers/XmlConfigurationParser.cs @@ -86,7 +86,7 @@ public override Galaxy Parse(string content) if (body is Planet planet) planetNeighbours.Add(planet, neighbours.ToArray()); } - MapNeighbours(galaxy, planetNeighbours); + galaxy.MapNeighbours(planetNeighbours); return galaxy; } } From 40b4fe0e7c89a1ade8b3658d310fe53e1cebdb26 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 15:29:12 +0200 Subject: [PATCH 20/25] Update GalaxyMemento.cs --- .../Bookmark/GalaxyMemento.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs index f360044..b94e3c6 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using Avans.FlatGalaxy.Models; using Avans.FlatGalaxy.Models.CelestialBodies; @@ -8,16 +9,19 @@ namespace Avans.FlatGalaxy.Simulation.Bookmark { public class GalaxyMemento : IMemento { - private readonly IList _celestialBodies; + private readonly IDictionary _celestialBodies; public GalaxyMemento(Galaxy galaxy) { - _celestialBodies = galaxy.CelestialBodies.Select(body => body.Clone()).ToList(); + _celestialBodies = galaxy.CelestialBodies.ToDictionary(body => body.Clone(), body => body is Planet planet ? planet.Neighbours.Select(planet1 => planet1.Name).ToArray() : Array.Empty()); } public Galaxy GetState() { - return new Galaxy(_celestialBodies); + var galaxy = new Galaxy(_celestialBodies.Keys); + galaxy.MapNeighbours(_celestialBodies.Where(pair => pair.Key is Planet).ToDictionary(pair => pair.Key as Planet, pair => pair.Value)); + + return galaxy; } } } From 58fadbd86465c7b14cc6e041b61be271d3c5bdb7 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 15:31:54 +0200 Subject: [PATCH 21/25] Remove exceptions --- src/Avans.FlatGalaxy.Models/Galaxy.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Models/Galaxy.cs b/src/Avans.FlatGalaxy.Models/Galaxy.cs index 39d514b..1eaf4e6 100644 --- a/src/Avans.FlatGalaxy.Models/Galaxy.cs +++ b/src/Avans.FlatGalaxy.Models/Galaxy.cs @@ -44,12 +44,10 @@ public void Remove(CelestialBody celestialBody) public void OnCompleted() { - throw new NotImplementedException(); } public void OnError(Exception error) { - throw new NotImplementedException(); } public void OnNext(CelestialBody celestialBody) From 708b438e9e5b01324ead71a6bdc326fd127a111f Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 17:25:56 +0200 Subject: [PATCH 22/25] Update --- .../Commands/ShortcutList.cs | 34 +++++++++----- .../SimulationWindow.xaml.cs | 12 ++++- .../Bookmark/Common/Caretaker.cs | 2 +- src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 2 +- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 46 +++++++++++-------- 5 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs b/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs index d9a408a..e11e8bf 100644 --- a/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs +++ b/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Linq; using System.Windows.Input; +using Avans.FlatGalaxy.Simulation; using Avans.FlatGalaxy.Simulation.Commands; namespace Avans.FlatGalaxy.Presentation.Commands @@ -8,68 +10,76 @@ public class ShortcutList : List { public ShortcutList() { - Add(new Shortcut + Add(new() { Command = new PauseCommand(), Description = "Pause simulation", Key = Key.P }); - - Add(new Shortcut + + Add(new() { Command = new ResumeCommand(), Description = "Resume simulation", Key = Key.R }); - - Add(new Shortcut + + Add(new() { Command = new SpeedUpCommand(), Description = "Speed up simulation", Key = Key.OemPlus }); - Add(new Shortcut + Add(new() { Command = new SpeedUpCommand(), Description = "Speed down simulation", Key = Key.OemMinus, }); - Add(new Shortcut + Add(new() { Command = new RestoreCommand(), Description = "Go back 5 seconds in simulation", Key = Key.Back, }); - Add(new Shortcut + Add(new() { Command = new CollisionSwitchCommand(), Description = "Switch collision (Quad tree / Naive)", Key = Key.K, }); - Add(new Shortcut + Add(new() { Command = new CollisionVisibilityCommand(), Description = "Switch collision visibility", Key = Key.L, }); - Add(new Shortcut + Add(new() { Command = new AddAsteroidCommand(), Description = "Add asteroid to simulation", Key = Key.U, }); - Add(new Shortcut + Add(new() { Command = new RemoveAsteroidCommand(), Description = "Remove asteroid from simulation", Key = Key.I, }); } + + public void HandleKey(Key key, ISimulator simulator) + { + foreach (var shortcut in this.Where(shortcut => shortcut.Key == key)) + { + shortcut.Command.Execute(simulator); + } + } } -} \ No newline at end of file +} diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index c4b5ddd..f667938 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -1,10 +1,12 @@ using System; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using Avans.FlatGalaxy.Models; using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Presentation.Commands; using Avans.FlatGalaxy.Presentation.Extensions; using Avans.FlatGalaxy.Simulation; using Avans.FlatGalaxy.Simulation.Data; @@ -14,9 +16,11 @@ namespace Avans.FlatGalaxy.Presentation public partial class SimulationWindow : Window { private ISimulator? _simulator; + private readonly ShortcutList _shortcutList; - public SimulationWindow() + public SimulationWindow(ShortcutList shortcutList) { + _shortcutList = shortcutList; InitializeComponent(); GalaxyCanvas.Width = ISimulator.Width; @@ -24,6 +28,12 @@ public SimulationWindow() Loaded += OnLoaded; Unloaded += OnUnloaded; + KeyUp += OnKeyUp; + } + + private void OnKeyUp(object sender, KeyEventArgs e) + { + if (_simulator != null) _shortcutList.HandleKey(e.Key, _simulator); } private void OnLoaded(object sender, RoutedEventArgs e) diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs index 773e67f..95b9d42 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs @@ -9,7 +9,7 @@ public abstract class Caretaker : ICaretaker protected Caretaker() { - _mementos = new Stack>(); + _mementos = new(); } protected abstract IMemento Create(); diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index c2dc7b5..152cc97 100644 --- a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs @@ -9,7 +9,7 @@ public interface ISimulator public const int Height = 600; - public const double SpeedDiff = 0.1; + public const double SpeedDiff = 5; public const double BookmarkTime = 5000; diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index b9ca0a4..951c5eb 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -114,26 +114,32 @@ private void Tick(CancellationToken token) { if (_running) { - Task.Run(async () => { - var currentTime = DateTime.UtcNow; - var tickTime = (currentTime - _lastTick).TotalMilliseconds; - var deltaTime = tickTime * _speed / 1000; - - Update(deltaTime); - - _collisionHandler.Detect(this); - - _lastTick = DateTime.UtcNow; - if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= ISimulator.BookmarkTime) - { - _caretaker.Save(); - _lastBookmark = DateTime.UtcNow; - } - - var nextTick = (int)(TpsTime - tickTime); - await Task.Delay(nextTick >= 0 ? nextTick : 0, token); - Tick(token); - }, token); + try + { + Task.Run(async () => { + var currentTime = DateTime.UtcNow; + var tickTime = (currentTime - _lastTick).TotalMilliseconds; + var deltaTime = tickTime * _speed / 1000; + + Update(deltaTime); + + _collisionHandler.Detect(this); + + _lastTick = DateTime.UtcNow; + if ((DateTime.UtcNow - _lastBookmark).TotalMilliseconds >= ISimulator.BookmarkTime) + { + _caretaker.Save(); + _lastBookmark = DateTime.UtcNow; + } + + var nextTick = (int)(TpsTime - tickTime); + await Task.Delay(nextTick >= 0 ? nextTick : 0, token); + Tick(token); + }, token); + } + catch (TaskCanceledException) + { + } } } From b7b9ae283f1f4c8b61a74382c7ff06a47f6b2248 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 17:28:16 +0200 Subject: [PATCH 23/25] Update Simulator.cs --- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 5650e26..0f3bc2e 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -53,12 +53,16 @@ public Simulator(Galaxy galaxy) public void Resume() { if (_running) return; - _running = true; + _source = new(); _token = _source.Token; + _lastTick = DateTime.UtcNow; + + _caretaker.Save(); _lastBookmark = DateTime.UtcNow; + Tick(_token); } From 5784613a7e4e495240630cb87c9f57c07d30e7da Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 17:31:03 +0200 Subject: [PATCH 24/25] Update Simulator.cs --- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 0f3bc2e..f1117b7 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -114,9 +114,8 @@ public void AddAsteroid() var y = rnd.NextDouble() * ISimulator.Height; var vx = rnd.NextDouble() * 10 - 5; var vy = rnd.NextDouble() * 10 - 5; - var radius = rnd.Next(2, 6); - Galaxy.Add(new Asteroid(x, y, vx, vy, radius, Color.Black, new NullCollisionState())); + Galaxy.Add(new Asteroid(x, y, vx, vy, 5, Color.Black, new NullCollisionState())); } public void RemoveAsteroid() From 7bdaa34ae5c021bd70af35fcb4bc60a557b38b4d Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 17:38:54 +0200 Subject: [PATCH 25/25] Fix --- src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs | 7 +++++++ src/Avans.FlatGalaxy.Simulation/Simulator.cs | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs b/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs index e11e8bf..4f18901 100644 --- a/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs +++ b/src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs @@ -59,6 +59,13 @@ public ShortcutList() Key = Key.L, }); + Add(new() + { + Command = new PathSwitchCommand(), + Description = "Switch path finding (Breadth first / Dijkstra)", + Key = Key.J, + }); + Add(new() { Command = new AddAsteroidCommand(), diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index f1117b7..041da6a 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -120,8 +120,8 @@ public void AddAsteroid() public void RemoveAsteroid() { - var asteroid = Galaxy.CelestialBodies.OfType().Random(); - Galaxy.Remove(asteroid); + var asteroids = Galaxy.CelestialBodies.OfType().ToList(); + if (asteroids.Any()) Galaxy.Remove(asteroids.Random()); } private void Tick(CancellationToken token) @@ -138,6 +138,7 @@ private void Tick(CancellationToken token) Update(deltaTime); _collisionHandler.Detect(this); + PathSteps = _pathHandler.Find(Galaxy); _lastTick = DateTime.UtcNow; if ((DateTime.UtcNow - _lastBookmark).TotalMilliseconds >= ISimulator.BookmarkTime)