From 071205ad95418a80cdb86a559d1e71eb1eedcbad Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 20:57:44 +0200 Subject: [PATCH 01/35] Create ICloneable.cs --- src/Avans.FlatGalaxy.Models/ICloneable.cs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Models/ICloneable.cs diff --git a/src/Avans.FlatGalaxy.Models/ICloneable.cs b/src/Avans.FlatGalaxy.Models/ICloneable.cs new file mode 100644 index 0000000..d2214e9 --- /dev/null +++ b/src/Avans.FlatGalaxy.Models/ICloneable.cs @@ -0,0 +1,7 @@ +namespace Avans.FlatGalaxy.Models +{ + public interface ICloneable + { + T Clone(); + } +} From 8b5311848c03379f7d6a062461a352ca8af4421d Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 20:58:23 +0200 Subject: [PATCH 02/35] Make States Cloneable --- .../CelestialBodies/States/BlinkState.cs | 5 +++++ .../CelestialBodies/States/BounceState.cs | 14 +++++++++++++- .../CelestialBodies/States/DisappearState.cs | 5 +++++ .../CelestialBodies/States/ExplodeState.cs | 5 +++++ .../CelestialBodies/States/GrowState.cs | 5 +++++ .../CelestialBodies/States/ICollisionState.cs | 2 +- .../CelestialBodies/States/NullCollisionState.cs | 5 +++++ 7 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BlinkState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BlinkState.cs index 5192836..df7a534 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BlinkState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BlinkState.cs @@ -21,5 +21,10 @@ public void Collide(CelestialBody self, CelestialBody other) }); } } + + public ICollisionState Clone() + { + return new BlinkState(); + } } } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BounceState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BounceState.cs index 482a7bd..ad2476b 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BounceState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/BounceState.cs @@ -3,7 +3,14 @@ public class BounceState : ICollisionState { private int _collisions; - + + public BounceState() {} + + private BounceState(int collisions) + { + _collisions = collisions; + } + public void Collide(CelestialBody self, CelestialBody other) { self.VX = -self.VX; @@ -14,5 +21,10 @@ public void Collide(CelestialBody self, CelestialBody other) self.TriggerStateEvent(); } } + + public ICollisionState Clone() + { + return new BounceState(_collisions); + } } } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/DisappearState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/DisappearState.cs index fdf9336..473d6de 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/DisappearState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/DisappearState.cs @@ -6,5 +6,10 @@ public void Collide(CelestialBody self, CelestialBody other) { self.TriggerStateEvent(); } + + public ICollisionState Clone() + { + return new DisappearState(); + } } } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ExplodeState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ExplodeState.cs index de61f59..580d5e3 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ExplodeState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ExplodeState.cs @@ -6,5 +6,10 @@ public void Collide(CelestialBody self, CelestialBody other) { self.TriggerStateEvent(); } + + public ICollisionState Clone() + { + return new ExplodeState(); + } } } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/GrowState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/GrowState.cs index daba5b2..0fa138d 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/GrowState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/GrowState.cs @@ -9,5 +9,10 @@ public void Collide(CelestialBody self, CelestialBody other) self.TriggerStateEvent(); } } + + public ICollisionState Clone() + { + return new GrowState(); + } } } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ICollisionState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ICollisionState.cs index be597c6..fa246fd 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ICollisionState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/ICollisionState.cs @@ -1,6 +1,6 @@ namespace Avans.FlatGalaxy.Models.CelestialBodies.States { - public interface ICollisionState + public interface ICollisionState : ICloneable { public void Collide(CelestialBody self, CelestialBody other); } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/NullCollisionState.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/NullCollisionState.cs index e398830..4faed7c 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/States/NullCollisionState.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/States/NullCollisionState.cs @@ -6,5 +6,10 @@ public void Collide(CelestialBody self, CelestialBody other) { // do nothing } + + public ICollisionState Clone() + { + return new NullCollisionState(); + } } } From b47d3eb1899ff8d8c355cc2093cdbeed794fb698 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 20:58:41 +0200 Subject: [PATCH 03/35] Make CelestialBodies Cloneable --- src/Avans.FlatGalaxy.Models/CelestialBodies/Asteroid.cs | 7 ++++++- .../CelestialBodies/CelestialBody.cs | 4 +++- src/Avans.FlatGalaxy.Models/CelestialBodies/Planet.cs | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/Asteroid.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/Asteroid.cs index f200501..a2520cb 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/Asteroid.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/Asteroid.cs @@ -8,5 +8,10 @@ public class Asteroid : CelestialBody public Asteroid(double x, double y, double vx, double vy, int radius, Color color, ICollisionState collisionState) : base(x, y, vx, vy, radius, color, collisionState) { } + + public override CelestialBody Clone() + { + return new Asteroid(X, Y, VX, VY, Radius, Color, CollisionState.Clone()); + } } -} \ No newline at end of file +} diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs index 944a574..a0750bd 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs @@ -7,7 +7,7 @@ namespace Avans.FlatGalaxy.Models.CelestialBodies { - public abstract class CelestialBody : IObservable + public abstract class CelestialBody : IObservable, ICloneable { private readonly IList> _observers; @@ -70,5 +70,7 @@ public void TriggerStateEvent() observer.OnNext(this); } } + + public abstract CelestialBody Clone(); } } diff --git a/src/Avans.FlatGalaxy.Models/CelestialBodies/Planet.cs b/src/Avans.FlatGalaxy.Models/CelestialBodies/Planet.cs index 259c09e..a04721d 100644 --- a/src/Avans.FlatGalaxy.Models/CelestialBodies/Planet.cs +++ b/src/Avans.FlatGalaxy.Models/CelestialBodies/Planet.cs @@ -14,5 +14,10 @@ public Planet(string name, double x, double y, double vx, double vy, int radius, { Name = name; } + + public override CelestialBody Clone() + { + return new Planet(Name, X, Y, VX, VY, Radius, Color, CollisionState.Clone()); + } } } From ff14e3b1b66a1534062de6bcce03ee9b3b6f2815 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 21:00:42 +0200 Subject: [PATCH 04/35] Update ICloneable.cs --- src/Avans.FlatGalaxy.Models/ICloneable.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Avans.FlatGalaxy.Models/ICloneable.cs b/src/Avans.FlatGalaxy.Models/ICloneable.cs index d2214e9..54ae365 100644 --- a/src/Avans.FlatGalaxy.Models/ICloneable.cs +++ b/src/Avans.FlatGalaxy.Models/ICloneable.cs @@ -1,7 +1,14 @@ -namespace Avans.FlatGalaxy.Models +using System; + +namespace Avans.FlatGalaxy.Models { - public interface ICloneable + public interface ICloneable : ICloneable { - T Clone(); + new T Clone(); + + object ICloneable.Clone() + { + return Clone(); + } } } From caebcc6d4f1a9fa66909dc2a0b29be85e8b544c3 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 21:47:53 +0200 Subject: [PATCH 05/35] Create extra constructor for Galaxy --- src/Avans.FlatGalaxy.Models/Galaxy.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Avans.FlatGalaxy.Models/Galaxy.cs b/src/Avans.FlatGalaxy.Models/Galaxy.cs index eebc778..0d92a9f 100644 --- a/src/Avans.FlatGalaxy.Models/Galaxy.cs +++ b/src/Avans.FlatGalaxy.Models/Galaxy.cs @@ -17,6 +17,14 @@ public Galaxy() _celestialBodies = new Dictionary(); } + public Galaxy(IEnumerable celestialBodies) : this() + { + foreach (var body in celestialBodies) + { + Add(body); + } + } + public void Add(CelestialBody celestialBody) { _celestialBodies.Add(celestialBody, celestialBody.Subscribe(this)); From 5ab0742f5f591db3772a4a43c45c57887bb1f2d2 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 21:48:02 +0200 Subject: [PATCH 06/35] Update Galaxy@Add --- src/Avans.FlatGalaxy.Models/Galaxy.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Avans.FlatGalaxy.Models/Galaxy.cs b/src/Avans.FlatGalaxy.Models/Galaxy.cs index 0d92a9f..39d514b 100644 --- a/src/Avans.FlatGalaxy.Models/Galaxy.cs +++ b/src/Avans.FlatGalaxy.Models/Galaxy.cs @@ -27,7 +27,11 @@ public Galaxy(IEnumerable celestialBodies) : this() public void Add(CelestialBody celestialBody) { - _celestialBodies.Add(celestialBody, celestialBody.Subscribe(this)); + if (_celestialBodies.ContainsKey(celestialBody)) + { + _celestialBodies[celestialBody]?.Dispose(); + } + _celestialBodies[celestialBody] = celestialBody.Subscribe(this); } public void Remove(CelestialBody celestialBody) From 91c5bf6aa1dd73763d7e96232371e628860496f2 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 21:49:36 +0200 Subject: [PATCH 07/35] Update Simulator --- .../SimulationWindow.xaml.cs | 11 +++++------ src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 2 +- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 13 +++---------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 3ff750e..f49f3d4 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -13,11 +13,10 @@ namespace Avans.FlatGalaxy.Presentation { public partial class SimulationWindow : Window { - private readonly ISimulator _simulator; + private ISimulator? _simulator; - public SimulationWindow(ISimulator simulator) + public SimulationWindow() { - _simulator = simulator; InitializeComponent(); GalaxyCanvas.Width = ISimulator.Width; @@ -41,15 +40,15 @@ private void OnRender(object? sender, EventArgs e) { GalaxyCanvas.Children.Clear(); - if (_simulator.Galaxy != null) { Draw(_simulator.Galaxy); } - if (_simulator.QuadTree != null) { Draw(_simulator.QuadTree); } + if (_simulator?.Galaxy != null) { Draw(_simulator.Galaxy); } + if (_simulator?.QuadTree != null) { Draw(_simulator.QuadTree); } } public void Show(Galaxy galaxy) { Show(); - _simulator.Galaxy = galaxy; + _simulator = new Simulator(galaxy); _simulator.Resume(); } diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index 7702f5c..689fea1 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; - Galaxy Galaxy { get; set; } + Galaxy Galaxy { get; } QuadTree QuadTree { get; set; } diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 1450c28..764166e 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -23,20 +23,13 @@ public class Simulator : ISimulator private CancellationToken _token; private readonly CollisionHandler _collisionHandler; - public Simulator() + public Simulator(Galaxy galaxy) { + _galaxy = galaxy; _collisionHandler = new CollisionHandler(); } - public Galaxy Galaxy - { - get => _galaxy; - set - { - Pause(); - _galaxy = value; - } - } + public Galaxy Galaxy => _galaxy; public QuadTree QuadTree { get; set; } From 1e94b5c15ebb0f6cde7b187b70d735421307a5b1 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 21:50:20 +0200 Subject: [PATCH 08/35] Implement Memento pattern --- .../Bookmark/Caretaker.cs | 31 +++++++++++++++++++ .../Bookmark/GalaxyMemento.cs | 22 +++++++++++++ .../Bookmark/Originator.cs | 26 ++++++++++++++++ src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 2 ++ src/Avans.FlatGalaxy.Simulation/Simulator.cs | 18 +++++++++++ 5 files changed, 99 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs new file mode 100644 index 0000000..e1a8907 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Avans.FlatGalaxy.Simulation.Bookmark +{ + public class Caretaker + { + private readonly Stack _mementos; + private readonly Originator _originator; + + public Caretaker(Originator originator) + { + _originator = originator; + _mementos = new Stack(); + } + + public void Backup() + { + _mementos.Push(_originator.Save()); + } + + public void Undo() + { + if (!_mementos.Any()) return; + + var memento = _mementos.Pop(); + + _originator.Restore(memento); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs new file mode 100644 index 0000000..85da7cb --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using System.Linq; +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; + +namespace Avans.FlatGalaxy.Simulation.Bookmark +{ + public class GalaxyMemento + { + private readonly IList _celestialBodies; + + public GalaxyMemento(Galaxy galaxy) + { + _celestialBodies = galaxy.CelestialBodies.Select(body => body.Clone()).ToList(); + } + + public Galaxy GetState() + { + return new Galaxy(_celestialBodies); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs new file mode 100644 index 0000000..082bde3 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs @@ -0,0 +1,26 @@ +using Avans.FlatGalaxy.Models; + +namespace Avans.FlatGalaxy.Simulation.Bookmark +{ + public class Originator + { + private Galaxy _state; + + public Originator(Galaxy state) + { + _state = state; + } + + public Galaxy State => _state; + + public GalaxyMemento Save() + { + return new GalaxyMemento(_state); + } + + public void Restore(GalaxyMemento memento) + { + _state = memento.GetState(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index 689fea1..c23bb9f 100644 --- a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs @@ -16,5 +16,7 @@ public interface ISimulator void Resume(); void Pause(); + + void Restore(); } } diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 764166e..8c9fe5f 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Simulation.Bookmark; using Avans.FlatGalaxy.Simulation.Collision; using Avans.FlatGalaxy.Simulation.Data; @@ -18,15 +19,20 @@ public class Simulator : ISimulator private int _speed = 50; private bool _running = false; + private DateTime _lastBookmark; private CancellationTokenSource _source; private CancellationToken _token; private readonly CollisionHandler _collisionHandler; + private readonly Originator _originator; + private readonly Caretaker _caretaker; public Simulator(Galaxy galaxy) { _galaxy = galaxy; _collisionHandler = new CollisionHandler(); + _originator = new Originator(_galaxy); + _caretaker = new Caretaker(_originator); } public Galaxy Galaxy => _galaxy; @@ -41,6 +47,7 @@ public void Resume() _source = new CancellationTokenSource(); _token = _source.Token; _lastTick = DateTime.UtcNow; + _lastBookmark = DateTime.UtcNow; Tick(_token); } @@ -50,6 +57,12 @@ public void Pause() _running = false; } + public void Restore() + { + _caretaker.Undo(); + _galaxy = _originator.State; + } + public void Tick(CancellationToken token) { if (_running) @@ -64,6 +77,11 @@ public void Tick(CancellationToken token) _collisionHandler.Detect(this); _lastTick = DateTime.UtcNow; + if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= 1) + { + _caretaker.Backup(); + _lastBookmark = DateTime.UtcNow; + } var nextTick = (int)(TpsTime - tickTime); await Task.Delay(nextTick >= 0 ? nextTick : 0, token); From 1793417da0dda7e991ed6bbd58aa5143c3b76566 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 22:01:15 +0200 Subject: [PATCH 09/35] Make memento generic --- .../Bookmark/{ => Common}/Caretaker.cs | 12 ++++----- .../Bookmark/Common/IMemento.cs | 7 +++++ .../Bookmark/Common/Originator.cs | 21 +++++++++++++++ .../Bookmark/GalaxyMemento.cs | 3 ++- .../Bookmark/GalaxyOriginator.cs | 17 ++++++++++++ .../Bookmark/Originator.cs | 26 ------------------- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 9 ++++--- 7 files changed, 58 insertions(+), 37 deletions(-) rename src/Avans.FlatGalaxy.Simulation/Bookmark/{ => Common}/Caretaker.cs (58%) create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Common/IMemento.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs delete mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs similarity index 58% rename from src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs rename to src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs index e1a8907..97ce1aa 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Caretaker.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs @@ -1,17 +1,17 @@ using System.Collections.Generic; using System.Linq; -namespace Avans.FlatGalaxy.Simulation.Bookmark +namespace Avans.FlatGalaxy.Simulation.Bookmark.Common { - public class Caretaker + public class Caretaker { - private readonly Stack _mementos; - private readonly Originator _originator; + private readonly Stack> _mementos; + private readonly Originator _originator; - public Caretaker(Originator originator) + public Caretaker(Originator originator) { _originator = originator; - _mementos = new Stack(); + _mementos = new Stack>(); } public void Backup() diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/IMemento.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/IMemento.cs new file mode 100644 index 0000000..18f827f --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/IMemento.cs @@ -0,0 +1,7 @@ +namespace Avans.FlatGalaxy.Simulation.Bookmark.Common +{ + public interface IMemento + { + T GetState(); + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs new file mode 100644 index 0000000..b4ae94e --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs @@ -0,0 +1,21 @@ +namespace Avans.FlatGalaxy.Simulation.Bookmark.Common +{ + public abstract class Originator + { + private T _state; + + public Originator(T state) + { + _state = state; + } + + public T State => _state; + + public abstract IMemento Save(); + + public void Restore(IMemento memento) + { + _state = memento.GetState(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs index 85da7cb..f360044 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs @@ -2,10 +2,11 @@ using System.Linq; using Avans.FlatGalaxy.Models; using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Simulation.Bookmark.Common; namespace Avans.FlatGalaxy.Simulation.Bookmark { - public class GalaxyMemento + public class GalaxyMemento : IMemento { private readonly IList _celestialBodies; diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs new file mode 100644 index 0000000..c2920ec --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs @@ -0,0 +1,17 @@ +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Simulation.Bookmark.Common; + +namespace Avans.FlatGalaxy.Simulation.Bookmark +{ + public class GalaxyOriginator : Originator + { + public GalaxyOriginator(Galaxy state) : base(state) + { + } + + public override IMemento Save() + { + throw new System.NotImplementedException(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs deleted file mode 100644 index 082bde3..0000000 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Originator.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Avans.FlatGalaxy.Models; - -namespace Avans.FlatGalaxy.Simulation.Bookmark -{ - public class Originator - { - private Galaxy _state; - - public Originator(Galaxy state) - { - _state = state; - } - - public Galaxy State => _state; - - public GalaxyMemento Save() - { - return new GalaxyMemento(_state); - } - - public void Restore(GalaxyMemento memento) - { - _state = memento.GetState(); - } - } -} diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 8c9fe5f..964ec2f 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Avans.FlatGalaxy.Models; using Avans.FlatGalaxy.Simulation.Bookmark; +using Avans.FlatGalaxy.Simulation.Bookmark.Common; using Avans.FlatGalaxy.Simulation.Collision; using Avans.FlatGalaxy.Simulation.Data; @@ -24,15 +25,15 @@ public class Simulator : ISimulator private CancellationTokenSource _source; private CancellationToken _token; private readonly CollisionHandler _collisionHandler; - private readonly Originator _originator; - private readonly Caretaker _caretaker; + private readonly GalaxyOriginator _originator; + private readonly Caretaker _caretaker; public Simulator(Galaxy galaxy) { _galaxy = galaxy; _collisionHandler = new CollisionHandler(); - _originator = new Originator(_galaxy); - _caretaker = new Caretaker(_originator); + _originator = new GalaxyOriginator(_galaxy); + _caretaker = new Caretaker(_originator); } public Galaxy Galaxy => _galaxy; From 3a9f91c1607f59558bc94bf60a752e48b924fc4d Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 22:01:29 +0200 Subject: [PATCH 10/35] Remove Simulator from Services --- src/Avans.FlatGalaxy.Presentation/App.xaml.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/App.xaml.cs b/src/Avans.FlatGalaxy.Presentation/App.xaml.cs index c833d09..25c8541 100644 --- a/src/Avans.FlatGalaxy.Presentation/App.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/App.xaml.cs @@ -32,8 +32,6 @@ private void ConfigureServices(IConfiguration configuration, IServiceCollection services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); From f416cd537a1e9640155805920ec206da9b8b5c52 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Fri, 22 Oct 2021 22:13:50 +0200 Subject: [PATCH 11/35] Fix GalaxyOriginator --- .../Bookmark/Common/Originator.cs | 8 +++----- .../Bookmark/GalaxyOriginator.cs | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs index b4ae94e..876de5a 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs @@ -2,20 +2,18 @@ { public abstract class Originator { - private T _state; - public Originator(T state) { - _state = state; + State = state; } - public T State => _state; + public T State { get; private set; } public abstract IMemento Save(); public void Restore(IMemento memento) { - _state = memento.GetState(); + State = memento.GetState(); } } } diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs index c2920ec..77b6c0d 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs @@ -11,7 +11,7 @@ public GalaxyOriginator(Galaxy state) : base(state) public override IMemento Save() { - throw new System.NotImplementedException(); + return new GalaxyMemento(State); } } } From 676396b6a7a10ce4a79dd5377973f6ac001219ab Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 12:32:48 +0200 Subject: [PATCH 12/35] Remove Originator --- .../Bookmark/Common/Caretaker.cs | 22 +++++++++------- .../Bookmark/Common/ICaretaker.cs | 9 +++++++ .../Bookmark/Common/Originator.cs | 19 -------------- .../Bookmark/GalaxyOriginator.cs | 17 ------------- .../Bookmark/SimulatorCaretaker.cs | 25 +++++++++++++++++++ src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 2 +- src/Avans.FlatGalaxy.Simulation/Simulator.cs | 14 ++++------- 7 files changed, 53 insertions(+), 55 deletions(-) create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Common/ICaretaker.cs delete mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs delete mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Bookmark/SimulatorCaretaker.cs diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs index 97ce1aa..6cd1694 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs @@ -1,22 +1,26 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; +using Avans.FlatGalaxy.Models; namespace Avans.FlatGalaxy.Simulation.Bookmark.Common { - public class Caretaker + public abstract class Caretaker : ICaretaker { private readonly Stack> _mementos; - private readonly Originator _originator; - public Caretaker(Originator originator) + protected Caretaker() { - _originator = originator; _mementos = new Stack>(); } - public void Backup() + protected abstract IMemento Create(); + + protected abstract void Restore(T model); + + public void Save() { - _mementos.Push(_originator.Save()); + _mementos.Push(Create()); } public void Undo() @@ -24,8 +28,8 @@ public void Undo() if (!_mementos.Any()) return; var memento = _mementos.Pop(); - - _originator.Restore(memento); + + Restore(memento.GetState()); } } } diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/ICaretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/ICaretaker.cs new file mode 100644 index 0000000..9d1a13e --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/ICaretaker.cs @@ -0,0 +1,9 @@ +namespace Avans.FlatGalaxy.Simulation.Bookmark.Common +{ + public interface ICaretaker + { + void Save(); + + void Undo(); + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs deleted file mode 100644 index 876de5a..0000000 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Originator.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Avans.FlatGalaxy.Simulation.Bookmark.Common -{ - public abstract class Originator - { - public Originator(T state) - { - State = state; - } - - public T State { get; private set; } - - public abstract IMemento Save(); - - public void Restore(IMemento memento) - { - State = memento.GetState(); - } - } -} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs deleted file mode 100644 index 77b6c0d..0000000 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyOriginator.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Avans.FlatGalaxy.Models; -using Avans.FlatGalaxy.Simulation.Bookmark.Common; - -namespace Avans.FlatGalaxy.Simulation.Bookmark -{ - public class GalaxyOriginator : Originator - { - public GalaxyOriginator(Galaxy state) : base(state) - { - } - - public override IMemento Save() - { - return new GalaxyMemento(State); - } - } -} diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/SimulatorCaretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/SimulatorCaretaker.cs new file mode 100644 index 0000000..d01e40a --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/SimulatorCaretaker.cs @@ -0,0 +1,25 @@ +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Simulation.Bookmark.Common; + +namespace Avans.FlatGalaxy.Simulation.Bookmark +{ + public class SimulatorCaretaker : Caretaker + { + private readonly ISimulator _simulator; + + public SimulatorCaretaker(ISimulator simulator) + { + _simulator = simulator; + } + + protected override IMemento Create() + { + return new GalaxyMemento(_simulator.Galaxy); + } + + protected override void Restore(Galaxy galaxy) + { + _simulator.Galaxy = galaxy; + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index c23bb9f..42fc251 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; - Galaxy Galaxy { get; } + Galaxy Galaxy { get; set; } QuadTree QuadTree { get; set; } diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 964ec2f..2d19973 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -15,7 +15,6 @@ public class Simulator : ISimulator private const float TpsTarget = 20; private const float TpsTime = Second / TpsTarget; - private Galaxy _galaxy; private DateTime _lastTick = DateTime.UtcNow; private int _speed = 50; @@ -25,18 +24,16 @@ public class Simulator : ISimulator private CancellationTokenSource _source; private CancellationToken _token; private readonly CollisionHandler _collisionHandler; - private readonly GalaxyOriginator _originator; - private readonly Caretaker _caretaker; + private readonly ICaretaker _caretaker; public Simulator(Galaxy galaxy) { - _galaxy = galaxy; + Galaxy = galaxy; _collisionHandler = new CollisionHandler(); - _originator = new GalaxyOriginator(_galaxy); - _caretaker = new Caretaker(_originator); + _caretaker = new SimulatorCaretaker(this); } - public Galaxy Galaxy => _galaxy; + public Galaxy Galaxy { get; set; } public QuadTree QuadTree { get; set; } @@ -61,7 +58,6 @@ public void Pause() public void Restore() { _caretaker.Undo(); - _galaxy = _originator.State; } public void Tick(CancellationToken token) @@ -80,7 +76,7 @@ public void Tick(CancellationToken token) _lastTick = DateTime.UtcNow; if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= 1) { - _caretaker.Backup(); + _caretaker.Save(); _lastBookmark = DateTime.UtcNow; } From 2d9ef8d7d1d980a84ac43b8af2fa9dedca262d6d Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 12:37:34 +0200 Subject: [PATCH 13/35] Remove unused usings --- src/Avans.FlatGalaxy.Presentation/App.xaml.cs | 1 - src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/App.xaml.cs b/src/Avans.FlatGalaxy.Presentation/App.xaml.cs index 25c8541..d689f74 100644 --- a/src/Avans.FlatGalaxy.Presentation/App.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/App.xaml.cs @@ -4,7 +4,6 @@ using Avans.FlatGalaxy.Persistence.Factories.Common; using Avans.FlatGalaxy.Persistence.Loaders; using Avans.FlatGalaxy.Persistence.Parsers; -using Avans.FlatGalaxy.Simulation; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs index 6cd1694..773e67f 100644 --- a/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs +++ b/src/Avans.FlatGalaxy.Simulation/Bookmark/Common/Caretaker.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using Avans.FlatGalaxy.Models; namespace Avans.FlatGalaxy.Simulation.Bookmark.Common { From 79d211f588cd61f2451065c3dfd8a737a799fa7f Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 12:41:01 +0200 Subject: [PATCH 14/35] Creates tests solution folder --- Avans.FlatGalaxy.sln | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Avans.FlatGalaxy.sln b/Avans.FlatGalaxy.sln index d98bba5..3788871 100644 --- a/Avans.FlatGalaxy.sln +++ b/Avans.FlatGalaxy.sln @@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avans.FlatGalaxy.Simulation EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avans.FlatGalaxy.Models", "src\Avans.FlatGalaxy.Models\Avans.FlatGalaxy.Models.csproj", "{CD0E92F8-3A94-42C9-A24A-0593C1D4230F}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AF26DE88-D3ED-40FC-BFA5-3F1B6785D335}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,10 +30,10 @@ Global {F4EE573C-B975-42A0-888A-456221CEE227}.Debug|Any CPU.Build.0 = Debug|Any CPU {F4EE573C-B975-42A0-888A-456221CEE227}.Release|Any CPU.ActiveCfg = Release|Any CPU {F4EE573C-B975-42A0-888A-456221CEE227}.Release|Any CPU.Build.0 = Release|Any CPU - {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.Build.0 = Release|Any CPU + {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {F47E1A6A-18B6-49FE-BC7A-B3583408E659} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} From dc5e4887821c56ef6052f59f048c77a0c7a142a5 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 12:48:53 +0200 Subject: [PATCH 15/35] Create Avans.FlatGalaxy.Persistence.Tests project --- Avans.FlatGalaxy.sln | 7 ++++++ .../Avans.FlatGalaxy.Persistence.Tests.csproj | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj diff --git a/Avans.FlatGalaxy.sln b/Avans.FlatGalaxy.sln index 3788871..1cb7882 100644 --- a/Avans.FlatGalaxy.sln +++ b/Avans.FlatGalaxy.sln @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avans.FlatGalaxy.Models", " EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AF26DE88-D3ED-40FC-BFA5-3F1B6785D335}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avans.FlatGalaxy.Persistence.Tests", "tests\Avans.FlatGalaxy.Persistence.Tests\Avans.FlatGalaxy.Persistence.Tests.csproj", "{9F26A102-93BD-4F60-9162-90EFA05F452E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -34,11 +36,16 @@ Global {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Debug|Any CPU.Build.0 = Debug|Any CPU {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.ActiveCfg = Release|Any CPU {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.Build.0 = Release|Any CPU + {9F26A102-93BD-4F60-9162-90EFA05F452E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F26A102-93BD-4F60-9162-90EFA05F452E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F26A102-93BD-4F60-9162-90EFA05F452E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F26A102-93BD-4F60-9162-90EFA05F452E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {F47E1A6A-18B6-49FE-BC7A-B3583408E659} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} {89B5ABAF-73F2-4682-AB42-048DCA3C4BB7} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} {F4EE573C-B975-42A0-888A-456221CEE227} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} {CD0E92F8-3A94-42C9-A24A-0593C1D4230F} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} + {9F26A102-93BD-4F60-9162-90EFA05F452E} = {AF26DE88-D3ED-40FC-BFA5-3F1B6785D335} EndGlobalSection EndGlobal diff --git a/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj b/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj new file mode 100644 index 0000000..b3c5ea9 --- /dev/null +++ b/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj @@ -0,0 +1,22 @@ + + + + net5.0 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + From 460b513abadf7a83643b8ec02d2e05435640d9f0 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:48:44 +0200 Subject: [PATCH 16/35] Add input files local --- .../Avans.FlatGalaxy.Persistence.csproj | 11 + .../planetsExtended.csv | 24 ++ .../planetsExtended.xml | 385 ++++++++++++++++++ 3 files changed, 420 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Persistence/planetsExtended.csv create mode 100644 src/Avans.FlatGalaxy.Persistence/planetsExtended.xml diff --git a/src/Avans.FlatGalaxy.Persistence/Avans.FlatGalaxy.Persistence.csproj b/src/Avans.FlatGalaxy.Persistence/Avans.FlatGalaxy.Persistence.csproj index 8e38dcc..b4fe124 100644 --- a/src/Avans.FlatGalaxy.Persistence/Avans.FlatGalaxy.Persistence.csproj +++ b/src/Avans.FlatGalaxy.Persistence/Avans.FlatGalaxy.Persistence.csproj @@ -8,4 +8,15 @@ + + + + Always + + + + Always + + + diff --git a/src/Avans.FlatGalaxy.Persistence/planetsExtended.csv b/src/Avans.FlatGalaxy.Persistence/planetsExtended.csv new file mode 100644 index 0000000..e10dfac --- /dev/null +++ b/src/Avans.FlatGalaxy.Persistence/planetsExtended.csv @@ -0,0 +1,24 @@ +name;type;x;y;vx;vy;neighbours;radius;color;oncollision +Kobol;Planet;45;310;0.3;-0.6;Unicron,Koarth;7;blue;blink +Namek;Planet;60;102;0.1;0.2;Helicon,Synnax,Xenex,Alderaan;15;orange;blink +Monea;Planet;105;290;-0.5;-0.1;Koarth,Solaria;7;grey;blink +Alderaan;Planet;110;400;-1;0.2;Unicron,Namek;7;brown;blink +Unicron;Planet;145;379;0.01;0.5;Kobol,Alderaan;7;blue;blink +Twinsum;Planet;195;480;0.2;0.1;Solaria,Trantor;7;purple;blink +Koarth;Planet;210;190;0.1;0.99;Kobol,Monea,Synnax;7;blue;blink +Helicon;Planet;219;95;0.01;0.2;Namek,Synnax;7;brown;blink +Solaria;Planet;240;322;0.3;-0.5;Monea,Twinsum,Xenex;15;orange;blink +Synnax;Planet;405;202;0.9;0.2;Namek,Koarth,Helicon,Trantor;7;pink;blink +Trantor;Planet;501;130;-0.5;-0.1;Twinsum,Synnax;7;purple;blink +Xenex;Planet;711;580;0.3;0.2;Solaria,Namek;7;blue;blink +;Asteroid;85;56;0;-1.5;;5;black;blink +;Asteroid;705;112;2;1;;5;black;bounce +;Asteroid;28;507;1;3;;5;black;disappear +;Asteroid;478;380;0;1;;5;black;bounce +;Asteroid;636;392;5;-1;;5;black;explode +;Asteroid;47;255;-2;-3;;5;black;explode +;Asteroid;320;250;-1;3;;5;black;bounce +;Asteroid;408;432;2;1;;5;black;bounce +;Asteroid;422;557;1;-1;;5;black;blink +;Asteroid;601;276;2;-2;;5;black;grow +;Asteroid;626;464;3;1;;5;black;disappear diff --git a/src/Avans.FlatGalaxy.Persistence/planetsExtended.xml b/src/Avans.FlatGalaxy.Persistence/planetsExtended.xml new file mode 100644 index 0000000..6f86815 --- /dev/null +++ b/src/Avans.FlatGalaxy.Persistence/planetsExtended.xml @@ -0,0 +1,385 @@ + + + + Kobol + + 45 + 310 + 7 + + + 0.3 + -0.6 + + + Unicron + Koarth + + blue + blink + + + Namek + + 60 + 102 + 15 + + + 0.1 + 0.2 + + + Helicon + Synnax + Xenex + Alderaan + + orange + blink + + + Monea + + 105 + 290 + 7 + + + -0.5 + -0.1 + + + Koarth + Solaria + + grey + blink + + + Alderaan + + 110 + 400 + 7 + + + -1 + 0.2 + + + Unicron + Namek + + brown + blink + + + Unicron + + 145 + 379 + 7 + + + 0.01 + 0.5 + + + Kobol + Alderaan + + blue + blink + + + Twinsum + + 195 + 480 + 7 + + + 0.2 + 0.1 + + + Solaria + Trantor + + purple + blink + + + Koarth + + 210 + 190 + 7 + + + 0.1 + 0.99 + + + Kobol + Monea + Synnax + + blue + blink + + + Helicon + + 219 + 95 + 7 + + + 0.01 + 0.2 + + + Namek + Synnax + + brown + blink + + + Solaria + + 240 + 322 + 15 + + + 0.3 + -0.5 + + + Monea + Twinsum + Xenex + + orange + blink + + + Synnax + + 405 + 202 + 7 + + + 0.9 + 0.2 + + + Namek + Koarth + Helicon + Trantor + + pink + blink + + + Trantor + + 501 + 130 + 7 + + + -0.5 + -0.1 + + + Twinsum + Synnax + + purple + blink + + + Xenex + + 711 + 580 + 7 + + + 0.3 + 0.2 + + + Solaria + Namek + + blue + blink + + + + + 85 + 56 + 5 + + + 0 + -1.5 + + black + blink + + + + + 705 + 112 + 5 + + + 2 + 1 + + black + bounce + + + + + 28 + 507 + 5 + + + 1 + 3 + + black + disappear + + + + + 478 + 380 + 5 + + + 0 + 1 + + black + bounce + + + + + 636 + 392 + 5 + + + 5 + -1 + + black + explode + + + + + 47 + 255 + 5 + + + -2 + -3 + + + black + explode + + + + + 320 + 250 + 5 + + + -1 + 3 + + + black + bounce + + + + + 408 + 432 + 5 + + + 2 + 1 + + + black + bounce + + + + + 422 + 557 + 5 + + + 1 + -1 + + + black + blink + + + + + 601 + 276 + 5 + + + 2 + -2 + + + black + grow + + + + + 626 + 464 + 5 + + + 3 + 1 + + + black + disappear + + \ No newline at end of file From 9e4c3480654c14f63441edc607f58d906dfb5dbf Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:49:00 +0200 Subject: [PATCH 17/35] Add Reference to Avans.FlatGalaxy.Persistence --- .../Avans.FlatGalaxy.Persistence.Tests.csproj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj b/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj index b3c5ea9..fe5168c 100644 --- a/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj +++ b/tests/Avans.FlatGalaxy.Persistence.Tests/Avans.FlatGalaxy.Persistence.Tests.csproj @@ -7,8 +7,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -19,4 +19,8 @@ + + + + From adb76fdd5953c571d0a06729195c05aefb34cfc0 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:49:20 +0200 Subject: [PATCH 18/35] Update FileLoaders --- .../Loaders/FilesystemFileLoader.cs | 9 +++++++-- .../Loaders/HttpFileLoader.cs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs b/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs index 34b0bf2..8120a77 100644 --- a/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs +++ b/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs @@ -3,7 +3,7 @@ namespace Avans.FlatGalaxy.Persistence.Loaders { - class FileSystemFileLoader : IFileLoader + public class FileSystemFileLoader : IFileLoader { public string[] SupportedSchemas => new[] { @@ -16,7 +16,12 @@ public string GetContent(Uri source) if (!File.Exists(path)) { - throw new FileNotFoundException("The file does not exist.", path); + var localPath = Path.Combine(Environment.CurrentDirectory, source.Host); + if (File.Exists(localPath)) + { + path = localPath; + } + else throw new FileNotFoundException("The file does not exist.", path); } try diff --git a/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs b/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs index 785f933..7188ced 100644 --- a/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs +++ b/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs @@ -3,7 +3,7 @@ namespace Avans.FlatGalaxy.Persistence.Loaders { - class HttpFileLoader : IFileLoader + public class HttpFileLoader : IFileLoader { public string[] SupportedSchemas => new[] { From d3c23cd03126d07e7eb09f97cd89913791ce75a6 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:49:35 +0200 Subject: [PATCH 19/35] Update Configuration Parser --- .../Parsers/CsvConfigurationParser.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs b/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs index 903f1f7..19129bc 100644 --- a/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs +++ b/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs @@ -10,6 +10,8 @@ namespace Avans.FlatGalaxy.Persistence.Parsers { public class CsvConfigurationParser : ConfigurationParserBase { + private const char Separator = ';'; + public CsvConfigurationParser(ICelestialBodyFactory celestialBodyFactory) : base(celestialBodyFactory) { } @@ -17,9 +19,9 @@ public CsvConfigurationParser(ICelestialBodyFactory celestialBodyFactory) : base public override bool CanParse(string content) { var lines = content.Split(Environment.NewLine); - var columns = lines[0].Split(',').Length; + var columns = lines[0].Split(Separator).Length; - return lines.All(line => line.Split(',').Length == columns); + return lines.All(line => string.IsNullOrWhiteSpace(line) || line.Split(Separator).Length == columns); } public override Galaxy Parse(string content) @@ -33,7 +35,7 @@ public override Galaxy Parse(string content) { if (line != "") { - var attributes = line.Split(';'); + var attributes = line.Split(Separator); var name = attributes[0]; var type = attributes[1]; From 7446d686587704cbd2e63b2223014d6f8e763025 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:49:39 +0200 Subject: [PATCH 20/35] Create Tests --- .../ConfigurationParserTests.cs | 70 ++++++++++++++++ .../FileLoaderTests.cs | 83 +++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 tests/Avans.FlatGalaxy.Persistence.Tests/ConfigurationParserTests.cs create mode 100644 tests/Avans.FlatGalaxy.Persistence.Tests/FileLoaderTests.cs diff --git a/tests/Avans.FlatGalaxy.Persistence.Tests/ConfigurationParserTests.cs b/tests/Avans.FlatGalaxy.Persistence.Tests/ConfigurationParserTests.cs new file mode 100644 index 0000000..de3abdd --- /dev/null +++ b/tests/Avans.FlatGalaxy.Persistence.Tests/ConfigurationParserTests.cs @@ -0,0 +1,70 @@ +using System; +using Avans.FlatGalaxy.Persistence.Factories; +using Avans.FlatGalaxy.Persistence.Loaders; +using Avans.FlatGalaxy.Persistence.Parsers; +using Xunit; + +namespace Avans.FlatGalaxy.Persistence.Tests +{ + public class ConfigurationParserTests + { + private readonly CelestialBodyFactory _bodyFactory; + private readonly IFileLoader _fileLoader; + + public ConfigurationParserTests() + { + _bodyFactory = new CelestialBodyFactory(); + _fileLoader = new FileLoader(); + } + + [Theory] + [InlineData("https://firebasestorage.googleapis.com/v0/b/dpa-files.appspot.com/o/planetsExtended.xml?alt=media")] + [InlineData("file://planetsExtended.xml")] + public void Test_ConfigurationParser_Xml(string uri) + { + var content = _fileLoader.GetContent(new Uri(uri)); + + var parser = new XmlConfigurationParser(_bodyFactory); + + Assert.True(parser.CanParse(content)); + + var galaxy = parser.Parse(content); + + Assert.NotEmpty(galaxy.CelestialBodies); + } + + [Theory] + [InlineData("https://firebasestorage.googleapis.com/v0/b/dpa-files.appspot.com/o/planetsExtended.csv?alt=media")] + [InlineData("file://planetsExtended.csv")] + public void Test_ConfigurationParser_Csv(string uri) + { + var content = _fileLoader.GetContent(new Uri(uri)); + + var parser = new CsvConfigurationParser(_bodyFactory); + + Assert.True(parser.CanParse(content)); + + var galaxy = parser.Parse(content); + + Assert.NotEmpty(galaxy.CelestialBodies); + } + + [Theory] + [InlineData("https://firebasestorage.googleapis.com/v0/b/dpa-files.appspot.com/o/planetsExtended.xml?alt=media")] + [InlineData("file://planetsExtended.xml")] + [InlineData("https://firebasestorage.googleapis.com/v0/b/dpa-files.appspot.com/o/planetsExtended.csv?alt=media")] + [InlineData("file://planetsExtended.csv")] + public void Test_ConfigurationParser(string uri) + { + var content = _fileLoader.GetContent(new Uri(uri)); + + var parser = new ConfigurationParser(_bodyFactory); + + Assert.True(parser.CanParse(content)); + + var galaxy = parser.Parse(content); + + Assert.NotEmpty(galaxy.CelestialBodies); + } + } +} diff --git a/tests/Avans.FlatGalaxy.Persistence.Tests/FileLoaderTests.cs b/tests/Avans.FlatGalaxy.Persistence.Tests/FileLoaderTests.cs new file mode 100644 index 0000000..ff36ab4 --- /dev/null +++ b/tests/Avans.FlatGalaxy.Persistence.Tests/FileLoaderTests.cs @@ -0,0 +1,83 @@ +using System; +using System.IO; +using System.Net.Http; +using Avans.FlatGalaxy.Persistence.Loaders; +using Xunit; + +namespace Avans.FlatGalaxy.Persistence.Tests +{ + public class FileLoaderTests + { + [Theory] + [InlineData("http")] + [InlineData("https")] + public void Test_FileLoader_Http_Supported(string schema) + { + var loader = new HttpFileLoader(); + + Assert.Contains(schema, loader.SupportedSchemas); + } + + [Theory] + [InlineData("file")] + public void Test_FileLoader_FileSystem_Supported(string schema) + { + var loader = new FileSystemFileLoader(); + + Assert.Contains(schema, loader.SupportedSchemas); + } + + [Theory] + [InlineData("file")] + [InlineData("http")] + [InlineData("https")] + public void Test_FileLoader_Supported(string schema) + { + var loader = new FileLoader(); + + Assert.Contains(schema, loader.SupportedSchemas); + } + + [Theory] + [InlineData("https://google.com")] + [InlineData("https://www.avans.nl")] + public void Test_FileLoader_Http_GetContent_Success(string uri) + { + var loader = new HttpFileLoader(); + var content = loader.GetContent(new Uri(uri)); + + Assert.False(string.IsNullOrWhiteSpace(content)); + } + + [Theory] + [InlineData("https://httpstat.us/404")] + [InlineData("https://httpstat.us/500")] + public void Test_FileLoader_Http_GetContent_Exception(string uri) + { + var loader = new HttpFileLoader(); + + Assert.Throws(() => loader.GetContent(new Uri(uri))); + } + + [Theory] + [InlineData("file://planetsExtended.xml")] + [InlineData("file://planetsExtended.csv")] + public void Test_FileLoader_FileSystem_GetContent_Success(string uri) + { + var loader = new FileSystemFileLoader(); + var content = loader.GetContent(new Uri(uri)); + + Assert.False(string.IsNullOrWhiteSpace(content)); + } + + [Theory] + [InlineData("file://planetsExtended.xml2")] + [InlineData("file://planetsExtended.csv2")] + public void Test_FileLoader_FileSystem_GetContent_Exception(string uri) + { + var loader = new FileSystemFileLoader(); + + Assert.Throws(() => loader.GetContent(new Uri(uri))); + } + } +} From babef1d1c6d04e594fa1fbab604f1b06bc215da9 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:49:20 +0200 Subject: [PATCH 21/35] Update FileLoaders --- .../Loaders/FilesystemFileLoader.cs | 9 +++++++-- .../Loaders/HttpFileLoader.cs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs b/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs index 34b0bf2..8120a77 100644 --- a/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs +++ b/src/Avans.FlatGalaxy.Persistence/Loaders/FilesystemFileLoader.cs @@ -3,7 +3,7 @@ namespace Avans.FlatGalaxy.Persistence.Loaders { - class FileSystemFileLoader : IFileLoader + public class FileSystemFileLoader : IFileLoader { public string[] SupportedSchemas => new[] { @@ -16,7 +16,12 @@ public string GetContent(Uri source) if (!File.Exists(path)) { - throw new FileNotFoundException("The file does not exist.", path); + var localPath = Path.Combine(Environment.CurrentDirectory, source.Host); + if (File.Exists(localPath)) + { + path = localPath; + } + else throw new FileNotFoundException("The file does not exist.", path); } try diff --git a/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs b/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs index 785f933..7188ced 100644 --- a/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs +++ b/src/Avans.FlatGalaxy.Persistence/Loaders/HttpFileLoader.cs @@ -3,7 +3,7 @@ namespace Avans.FlatGalaxy.Persistence.Loaders { - class HttpFileLoader : IFileLoader + public class HttpFileLoader : IFileLoader { public string[] SupportedSchemas => new[] { From c0eb2bb65fb4f585f0d9f531d4223ef6c9c613d1 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:49:35 +0200 Subject: [PATCH 22/35] Update Configuration Parser --- .../Parsers/CsvConfigurationParser.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs b/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs index 903f1f7..19129bc 100644 --- a/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs +++ b/src/Avans.FlatGalaxy.Persistence/Parsers/CsvConfigurationParser.cs @@ -10,6 +10,8 @@ namespace Avans.FlatGalaxy.Persistence.Parsers { public class CsvConfigurationParser : ConfigurationParserBase { + private const char Separator = ';'; + public CsvConfigurationParser(ICelestialBodyFactory celestialBodyFactory) : base(celestialBodyFactory) { } @@ -17,9 +19,9 @@ public CsvConfigurationParser(ICelestialBodyFactory celestialBodyFactory) : base public override bool CanParse(string content) { var lines = content.Split(Environment.NewLine); - var columns = lines[0].Split(',').Length; + var columns = lines[0].Split(Separator).Length; - return lines.All(line => line.Split(',').Length == columns); + return lines.All(line => string.IsNullOrWhiteSpace(line) || line.Split(Separator).Length == columns); } public override Galaxy Parse(string content) @@ -33,7 +35,7 @@ public override Galaxy Parse(string content) { if (line != "") { - var attributes = line.Split(';'); + var attributes = line.Split(Separator); var name = attributes[0]; var type = attributes[1]; From 3a517bfc9f85e207ef2918398aff2c54704b3e74 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 13:51:33 +0200 Subject: [PATCH 23/35] Create Avans.FlatGalaxy.Simulation.Tests project --- Avans.FlatGalaxy.sln | 7 +++++ .../Avans.FlatGalaxy.Simulation.Tests.csproj | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj diff --git a/Avans.FlatGalaxy.sln b/Avans.FlatGalaxy.sln index 3788871..563bb8c 100644 --- a/Avans.FlatGalaxy.sln +++ b/Avans.FlatGalaxy.sln @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avans.FlatGalaxy.Models", " EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AF26DE88-D3ED-40FC-BFA5-3F1B6785D335}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avans.FlatGalaxy.Simulation.Tests", "tests\Avans.FlatGalaxy.Simulation.Tests\Avans.FlatGalaxy.Simulation.Tests.csproj", "{4F05D115-2360-4A21-A718-BFB014C61289}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -34,11 +36,16 @@ Global {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Debug|Any CPU.Build.0 = Debug|Any CPU {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.ActiveCfg = Release|Any CPU {CD0E92F8-3A94-42C9-A24A-0593C1D4230F}.Release|Any CPU.Build.0 = Release|Any CPU + {4F05D115-2360-4A21-A718-BFB014C61289}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F05D115-2360-4A21-A718-BFB014C61289}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F05D115-2360-4A21-A718-BFB014C61289}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F05D115-2360-4A21-A718-BFB014C61289}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {F47E1A6A-18B6-49FE-BC7A-B3583408E659} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} {89B5ABAF-73F2-4682-AB42-048DCA3C4BB7} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} {F4EE573C-B975-42A0-888A-456221CEE227} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} {CD0E92F8-3A94-42C9-A24A-0593C1D4230F} = {7996173C-8F81-4B28-A46A-0529C89FF0C8} + {4F05D115-2360-4A21-A718-BFB014C61289} = {AF26DE88-D3ED-40FC-BFA5-3F1B6785D335} EndGlobalSection EndGlobal diff --git a/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj b/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj new file mode 100644 index 0000000..6857e13 --- /dev/null +++ b/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj @@ -0,0 +1,26 @@ + + + + net5.0 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + From 5f71e9f8999087546ea1ca0e2839c4b7844b15a0 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 14:48:01 +0200 Subject: [PATCH 24/35] Fix QuadTree --- .../SimulationWindow.xaml.cs | 12 +++---- .../Data/Bounds.cs | 32 +++++++++---------- .../Data/QuadTree.cs | 25 ++++++++------- .../Extensions/QuadTreeExtensions.cs | 8 ++--- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 3ff750e..e4bfa6f 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -91,10 +91,10 @@ private void Draw(QuadTree tree) { Draw(tree.Bounds); - if (tree.NorthEast != null) Draw(tree.NorthEast); - if (tree.NorthWest != null) Draw(tree.NorthWest); - if (tree.SouthEast != null) Draw(tree.SouthEast); - if (tree.SouthWest != null) Draw(tree.SouthWest); + if (tree.TopRight != null) Draw(tree.TopRight); + if (tree.TopLeft != null) Draw(tree.TopLeft); + if (tree.BottomRight != null) Draw(tree.BottomRight); + if (tree.BottomLeft != null) Draw(tree.BottomLeft); } private void Draw(Bounds bounds) @@ -106,8 +106,8 @@ private void Draw(Bounds bounds) Width = bounds.Width, Height = bounds.Height, }; - Canvas.SetTop(rect, bounds.North); - Canvas.SetLeft(rect, bounds.East); + Canvas.SetTop(rect, bounds.Top); + Canvas.SetLeft(rect, bounds.Left); GalaxyCanvas.Children.Add(rect); } } diff --git a/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs b/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs index 14b7a5d..c28aa8b 100644 --- a/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs +++ b/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs @@ -2,37 +2,37 @@ { public class Bounds { - public Bounds(double north, double east, double south, double west) + public Bounds(double top, double right, double bottom, double left) { - North = north; - East = east; - South = south; - West = west; + Top = top; + Right = right; + Bottom = bottom; + Left = left; } - public double North { get; } + public double Top { get; } - public double East { get; } + public double Right { get; } - public double South { get; } + public double Bottom { get; } - public double West { get; } + public double Left { get; } - public double Height => South - North; + public double Height => Bottom - Top; - public double Width => West - East; + public double Width => Right - Left; public bool Inside(double x1, double y1, double x2, double y2) { - return x2 >= East && x1 <= West && y2 >= North && y1 <= South; + return x1 <= Right && x2 >= Left && y1 <= Bottom && y2 >= Top; } - public Bounds NorthEast => new(North, East, North + Height / 2, East + Width / 2); + public Bounds TopRight => new(Top, Right, Top + Height / 2, Right - Width / 2); - public Bounds NorthWest => new(North, East + Width / 2, North + Height / 2, West); + public Bounds TopLeft => new(Top, Left + Width / 2, Top + Height / 2, Left); - public Bounds SouthEast => new(North + Height / 2, East, South, East + Width / 2); + public Bounds BottomRight => new(Bottom - Height / 2, Right, Bottom, Right - Width / 2); - public Bounds SouthWest => new(North + Height / 2, East + Width / 2, South, West); + public Bounds BottomLeft => new(Bottom - Height / 2, Left + Width / 2, Bottom, Left); } } diff --git a/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs b/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs index 6e61d38..bcc55e8 100644 --- a/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs +++ b/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs @@ -15,13 +15,13 @@ public class QuadTree public IList Elements { get; private set; } - public QuadTree NorthEast { get; private set; } + public QuadTree TopRight { get; private set; } - public QuadTree NorthWest { get; private set; } + public QuadTree TopLeft { get; private set; } - public QuadTree SouthEast { get; private set; } + public QuadTree BottomRight { get; private set; } - public QuadTree SouthWest { get; private set; } + public QuadTree BottomLeft { get; private set; } public QuadTree(Bounds bounds, int depth = 1) { @@ -41,6 +41,7 @@ public void Insert(CelestialBody element) else { Subdivide(); + InsertSub(element); } } else @@ -51,10 +52,10 @@ public void Insert(CelestialBody element) public void Subdivide() { - NorthEast = new QuadTree(Bounds.NorthEast, _depth + 1); - NorthWest = new QuadTree(Bounds.NorthWest, _depth + 1); - SouthEast = new QuadTree(Bounds.SouthEast, _depth + 1); - SouthWest = new QuadTree(Bounds.SouthWest, _depth + 1); + TopRight = new QuadTree(Bounds.TopRight, _depth + 1); + TopLeft = new QuadTree(Bounds.TopLeft, _depth + 1); + BottomRight = new QuadTree(Bounds.BottomRight, _depth + 1); + BottomLeft = new QuadTree(Bounds.BottomLeft, _depth + 1); foreach (var element in Elements) { @@ -66,10 +67,10 @@ public void Subdivide() private void InsertSub(CelestialBody element) { - if (NorthEast.Bounds.Inside(element)) NorthEast.Insert(element); - if (NorthWest.Bounds.Inside(element)) NorthWest.Insert(element); - if (SouthEast.Bounds.Inside(element)) SouthEast.Insert(element); - if (SouthWest.Bounds.Inside(element)) SouthWest.Insert(element); + if (TopRight.Bounds.Inside(element)) TopRight.Insert(element); + if (TopLeft.Bounds.Inside(element)) TopLeft.Insert(element); + if (BottomRight.Bounds.Inside(element)) BottomRight.Insert(element); + if (BottomLeft.Bounds.Inside(element)) BottomLeft.Insert(element); } } } diff --git a/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs b/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs index f078d67..14d7a83 100644 --- a/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs +++ b/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs @@ -20,10 +20,10 @@ public static void Collisions(this QuadTree quadTree, CollisionHandler collision } else { - quadTree.NorthEast.Collisions(collisionHandler); - quadTree.NorthWest.Collisions(collisionHandler); - quadTree.SouthEast.Collisions(collisionHandler); - quadTree.SouthWest.Collisions(collisionHandler); + quadTree.TopRight.Collisions(collisionHandler); + quadTree.TopLeft.Collisions(collisionHandler); + quadTree.BottomRight.Collisions(collisionHandler); + quadTree.BottomLeft.Collisions(collisionHandler); } } } From 9b09a98038238a87a8ceba04af6708bff2ac37cc Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 14:48:01 +0200 Subject: [PATCH 25/35] Fix QuadTree --- .../SimulationWindow.xaml.cs | 12 +++---- .../Data/Bounds.cs | 32 +++++++++---------- .../Data/QuadTree.cs | 25 ++++++++------- .../Extensions/QuadTreeExtensions.cs | 8 ++--- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 3ff750e..e4bfa6f 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -91,10 +91,10 @@ private void Draw(QuadTree tree) { Draw(tree.Bounds); - if (tree.NorthEast != null) Draw(tree.NorthEast); - if (tree.NorthWest != null) Draw(tree.NorthWest); - if (tree.SouthEast != null) Draw(tree.SouthEast); - if (tree.SouthWest != null) Draw(tree.SouthWest); + if (tree.TopRight != null) Draw(tree.TopRight); + if (tree.TopLeft != null) Draw(tree.TopLeft); + if (tree.BottomRight != null) Draw(tree.BottomRight); + if (tree.BottomLeft != null) Draw(tree.BottomLeft); } private void Draw(Bounds bounds) @@ -106,8 +106,8 @@ private void Draw(Bounds bounds) Width = bounds.Width, Height = bounds.Height, }; - Canvas.SetTop(rect, bounds.North); - Canvas.SetLeft(rect, bounds.East); + Canvas.SetTop(rect, bounds.Top); + Canvas.SetLeft(rect, bounds.Left); GalaxyCanvas.Children.Add(rect); } } diff --git a/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs b/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs index 14b7a5d..c28aa8b 100644 --- a/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs +++ b/src/Avans.FlatGalaxy.Simulation/Data/Bounds.cs @@ -2,37 +2,37 @@ { public class Bounds { - public Bounds(double north, double east, double south, double west) + public Bounds(double top, double right, double bottom, double left) { - North = north; - East = east; - South = south; - West = west; + Top = top; + Right = right; + Bottom = bottom; + Left = left; } - public double North { get; } + public double Top { get; } - public double East { get; } + public double Right { get; } - public double South { get; } + public double Bottom { get; } - public double West { get; } + public double Left { get; } - public double Height => South - North; + public double Height => Bottom - Top; - public double Width => West - East; + public double Width => Right - Left; public bool Inside(double x1, double y1, double x2, double y2) { - return x2 >= East && x1 <= West && y2 >= North && y1 <= South; + return x1 <= Right && x2 >= Left && y1 <= Bottom && y2 >= Top; } - public Bounds NorthEast => new(North, East, North + Height / 2, East + Width / 2); + public Bounds TopRight => new(Top, Right, Top + Height / 2, Right - Width / 2); - public Bounds NorthWest => new(North, East + Width / 2, North + Height / 2, West); + public Bounds TopLeft => new(Top, Left + Width / 2, Top + Height / 2, Left); - public Bounds SouthEast => new(North + Height / 2, East, South, East + Width / 2); + public Bounds BottomRight => new(Bottom - Height / 2, Right, Bottom, Right - Width / 2); - public Bounds SouthWest => new(North + Height / 2, East + Width / 2, South, West); + public Bounds BottomLeft => new(Bottom - Height / 2, Left + Width / 2, Bottom, Left); } } diff --git a/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs b/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs index 6e61d38..bcc55e8 100644 --- a/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs +++ b/src/Avans.FlatGalaxy.Simulation/Data/QuadTree.cs @@ -15,13 +15,13 @@ public class QuadTree public IList Elements { get; private set; } - public QuadTree NorthEast { get; private set; } + public QuadTree TopRight { get; private set; } - public QuadTree NorthWest { get; private set; } + public QuadTree TopLeft { get; private set; } - public QuadTree SouthEast { get; private set; } + public QuadTree BottomRight { get; private set; } - public QuadTree SouthWest { get; private set; } + public QuadTree BottomLeft { get; private set; } public QuadTree(Bounds bounds, int depth = 1) { @@ -41,6 +41,7 @@ public void Insert(CelestialBody element) else { Subdivide(); + InsertSub(element); } } else @@ -51,10 +52,10 @@ public void Insert(CelestialBody element) public void Subdivide() { - NorthEast = new QuadTree(Bounds.NorthEast, _depth + 1); - NorthWest = new QuadTree(Bounds.NorthWest, _depth + 1); - SouthEast = new QuadTree(Bounds.SouthEast, _depth + 1); - SouthWest = new QuadTree(Bounds.SouthWest, _depth + 1); + TopRight = new QuadTree(Bounds.TopRight, _depth + 1); + TopLeft = new QuadTree(Bounds.TopLeft, _depth + 1); + BottomRight = new QuadTree(Bounds.BottomRight, _depth + 1); + BottomLeft = new QuadTree(Bounds.BottomLeft, _depth + 1); foreach (var element in Elements) { @@ -66,10 +67,10 @@ public void Subdivide() private void InsertSub(CelestialBody element) { - if (NorthEast.Bounds.Inside(element)) NorthEast.Insert(element); - if (NorthWest.Bounds.Inside(element)) NorthWest.Insert(element); - if (SouthEast.Bounds.Inside(element)) SouthEast.Insert(element); - if (SouthWest.Bounds.Inside(element)) SouthWest.Insert(element); + if (TopRight.Bounds.Inside(element)) TopRight.Insert(element); + if (TopLeft.Bounds.Inside(element)) TopLeft.Insert(element); + if (BottomRight.Bounds.Inside(element)) BottomRight.Insert(element); + if (BottomLeft.Bounds.Inside(element)) BottomLeft.Insert(element); } } } diff --git a/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs b/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs index f078d67..14d7a83 100644 --- a/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs +++ b/src/Avans.FlatGalaxy.Simulation/Extensions/QuadTreeExtensions.cs @@ -20,10 +20,10 @@ public static void Collisions(this QuadTree quadTree, CollisionHandler collision } else { - quadTree.NorthEast.Collisions(collisionHandler); - quadTree.NorthWest.Collisions(collisionHandler); - quadTree.SouthEast.Collisions(collisionHandler); - quadTree.SouthWest.Collisions(collisionHandler); + quadTree.TopRight.Collisions(collisionHandler); + quadTree.TopLeft.Collisions(collisionHandler); + quadTree.BottomRight.Collisions(collisionHandler); + quadTree.BottomLeft.Collisions(collisionHandler); } } } From b6f11385c8ae3958d6cd2811c79bde26e92dcc16 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 15:41:01 +0200 Subject: [PATCH 26/35] Update QuadTreeCollisionDetector.cs --- .../Collision/QuadTreeCollisionDetector.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs b/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs index 83943b4..f767fe7 100644 --- a/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs +++ b/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs @@ -7,7 +7,7 @@ public class QuadTreeCollisionDetector : ICollisionDetector { public void Detect(ISimulator simulator, CollisionHandler handler) { - var quadTree = new QuadTree(new Bounds(0, 0, ISimulator.Height, ISimulator.Width)); + var quadTree = new QuadTree(new Bounds(0, ISimulator.Width, ISimulator.Height, 0)); foreach (var celestialBody in simulator.Galaxy.CelestialBodies) { From 047c0eaf574d4e8acdc3811225ca01d40571fbe5 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 15:41:01 +0200 Subject: [PATCH 27/35] Update QuadTreeCollisionDetector.cs --- .../Collision/QuadTreeCollisionDetector.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs b/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs index 83943b4..f767fe7 100644 --- a/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs +++ b/src/Avans.FlatGalaxy.Simulation/Collision/QuadTreeCollisionDetector.cs @@ -7,7 +7,7 @@ public class QuadTreeCollisionDetector : ICollisionDetector { public void Detect(ISimulator simulator, CollisionHandler handler) { - var quadTree = new QuadTree(new Bounds(0, 0, ISimulator.Height, ISimulator.Width)); + var quadTree = new QuadTree(new Bounds(0, ISimulator.Width, ISimulator.Height, 0)); foreach (var celestialBody in simulator.Galaxy.CelestialBodies) { From e0fc9fce554e83585fd2eb256de3afd07ad4f436 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 16:00:26 +0200 Subject: [PATCH 28/35] Add Moq to Avans.FlatGalaxy.Simulation.Tests --- .../Avans.FlatGalaxy.Simulation.Tests.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj b/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj index 6857e13..00e82ee 100644 --- a/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj +++ b/tests/Avans.FlatGalaxy.Simulation.Tests/Avans.FlatGalaxy.Simulation.Tests.csproj @@ -8,6 +8,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive From 24f50217230f1ecda7d5920b7d65636abd4a00ae Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Wed, 27 Oct 2021 16:00:31 +0200 Subject: [PATCH 29/35] Create Tests --- .../CollisionTests.cs | 89 ++++++++++++++++++ .../MementoTests.cs | 47 ++++++++++ .../QuadTreeTests.cs | 94 +++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs create mode 100644 tests/Avans.FlatGalaxy.Simulation.Tests/MementoTests.cs create mode 100644 tests/Avans.FlatGalaxy.Simulation.Tests/QuadTreeTests.cs diff --git a/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs b/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs new file mode 100644 index 0000000..35e7b57 --- /dev/null +++ b/tests/Avans.FlatGalaxy.Simulation.Tests/CollisionTests.cs @@ -0,0 +1,89 @@ +using System.Drawing; +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Models.CelestialBodies.States; +using Avans.FlatGalaxy.Simulation.Collision; +using Moq; +using Xunit; + +namespace Avans.FlatGalaxy.Simulation.Tests +{ + public class CollisionTests + { + [Fact] + public void Test_Collision() + { + var mockCollisionState = CreateTestState(); + var body1 = new Asteroid(5, 5, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body2 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + + var sim = new Simulator(new Galaxy(new[] { body1, body2 })); + + var handler = new CollisionHandler(); + handler.Detect(sim); + handler.Toggle(); + handler.Detect(sim); + handler.Toggle(); + handler.Detect(sim); + + mockCollisionState.Verify(state => state.Collide(body1, body2), Times.Once); + mockCollisionState.Verify(state => state.Collide(body2, body1), Times.Once); + } + + [Fact] + public void Test_Collision_Naive() + { + var mockCollisionState = CreateTestState(); + var body1 = new Asteroid(5, 5, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body2 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + + var sim = new Simulator(new Galaxy(new[] { body1, body2 })); + + var detector = new NaiveCollisionDetector(); + detector.Detect(sim, new CollisionHandler()); + + mockCollisionState.Verify(state => state.Collide(body1, body2), Times.Once); + mockCollisionState.Verify(state => state.Collide(body2, body1), Times.Once); + } + + [Fact] + public void Test_Collision_QuadTree() + { + var mockCollisionState = CreateTestState(); + var body1 = new Asteroid(5, 5, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body2 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + + var sim = new Simulator(new Galaxy(new[] { body1, body2 })); + + var detector = new QuadTreeCollisionDetector(); + detector.Detect(sim, new CollisionHandler()); + + mockCollisionState.Verify(state => state.Collide(body1, body2), Times.Once); + mockCollisionState.Verify(state => state.Collide(body2, body1), Times.Once); + } + + [Fact] + public void Test_Collision_QuadTree_AboveSize() + { + var mockCollisionState = CreateTestState(); + var body1 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body2 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body3 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body4 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + var body5 = new Asteroid(7, 7, 0, 0, 3, Color.Green, mockCollisionState.Object); + + var sim = new Simulator(new Galaxy(new[] { body1, body2, body3, body4, body5 })); + + var detector = new QuadTreeCollisionDetector(); + detector.Detect(sim, new CollisionHandler()); + + mockCollisionState.Verify(state => state.Collide(It.IsAny(), It.IsAny()), Times.Exactly(20)); + } + + private Mock CreateTestState() + { + var mock = new Mock(); + return mock; + } + } +} diff --git a/tests/Avans.FlatGalaxy.Simulation.Tests/MementoTests.cs b/tests/Avans.FlatGalaxy.Simulation.Tests/MementoTests.cs new file mode 100644 index 0000000..ed1d22c --- /dev/null +++ b/tests/Avans.FlatGalaxy.Simulation.Tests/MementoTests.cs @@ -0,0 +1,47 @@ +using System.Drawing; +using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Models.CelestialBodies.States; +using Avans.FlatGalaxy.Simulation.Bookmark; +using Xunit; + +namespace Avans.FlatGalaxy.Simulation.Tests +{ + public class MementoTests + { + [Fact] + public void Test_Memento_CreateRestore() + { + var body1 = new Asteroid(7, 7, 0, 0, 3, Color.Green, new NullCollisionState()); + var body2 = new Asteroid(7, 7, 0, 0, 3, Color.Green, new NullCollisionState()); + var galaxy = new Galaxy(new[] { body1, body2 }); + + var memento = new GalaxyMemento(galaxy); + var galaxy2 = memento.GetState(); + + Assert.NotSame(galaxy, galaxy2); + } + + [Fact] + public void Test_Caretaker_CreateRestore() + { + var body1 = new Asteroid(7, 7, 1, 1, 3, Color.Green, new NullCollisionState()); + var body2 = new Asteroid(7, 7, 1, 1, 3, Color.Green, new NullCollisionState()); + var galaxy = new Galaxy(new[] { body1, body2 }); + var simulator = new Simulator(galaxy); + + var caretaker = new SimulatorCaretaker(simulator); + caretaker.Undo(); + + Assert.Same(galaxy, simulator.Galaxy); + + caretaker.Save(); + + Assert.Same(galaxy, simulator.Galaxy); + + caretaker.Undo(); + + Assert.NotSame(galaxy, simulator.Galaxy); + } + } +} diff --git a/tests/Avans.FlatGalaxy.Simulation.Tests/QuadTreeTests.cs b/tests/Avans.FlatGalaxy.Simulation.Tests/QuadTreeTests.cs new file mode 100644 index 0000000..2da23b0 --- /dev/null +++ b/tests/Avans.FlatGalaxy.Simulation.Tests/QuadTreeTests.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Models.CelestialBodies.States; +using Avans.FlatGalaxy.Simulation.Data; +using Xunit; + +namespace Avans.FlatGalaxy.Simulation.Tests +{ + public class QuadTreeTests + { + private const int Size = 500; + private const int Speed = 4; + + [Theory] + [InlineData(1)] + [InlineData(15)] + [InlineData(28)] + [InlineData(56)] + [InlineData(74)] + public void Test_QuadTree_Insert(int count) + { + var quadTree = new QuadTree(new Bounds(0, Size, Size, 0)); + + foreach (var body in CreateCelestialBodies(count)) + { + quadTree.Insert(body); + } + } + + [Fact] + public void Test_QuadTree_Insert_SameLocation_BelowSize() + { + var quadTree = new QuadTree(new Bounds(0, Size, Size, 0)); + + foreach (var body in CreateCelestialBodies(QuadTree.Size - 1)) + { + body.X = body.Y = 1; + quadTree.Insert(body); + } + + Assert.Null(quadTree.TopRight); + Assert.NotNull(quadTree.Elements); + Assert.Equal(3, quadTree.Elements.Count); + } + + [Fact] + public void Test_QuadTree_Insert_SameLocation_AboveSize() + { + var quadTree = new QuadTree(new Bounds(0, Size, Size, 0)); + + foreach (var body in CreateCelestialBodies(QuadTree.Size + 1)) + { + body.X = body.Y = body.Radius = 1; + quadTree.Insert(body); + } + + Assert.Null(quadTree.Elements); + + var depth = 1; + while (quadTree.Elements == null) + { + depth++; + quadTree = quadTree.TopLeft; + } + + Assert.Equal(QuadTree.MaxDepth, depth); + } + + private CelestialBody CreateCelestialBody() + { + var rnd = new Random(); + + return new Planet( + $"Test {rnd.Next(100, 1000)}", + rnd.NextDouble() * Size, + rnd.NextDouble() * Size, + rnd.NextDouble() * Speed + 1, + rnd.NextDouble() * Speed + 1, + rnd.Next(2, 8), Color.Green, + new NullCollisionState() + ); + } + + private IEnumerable CreateCelestialBodies(int count) + { + for (var i = 0; i < count; i++) + { + yield return CreateCelestialBody(); + } + } + } +} From 69d6c7b5a7762f35edee9f0d32d926d7b9e015bc Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 11:18:28 +0200 Subject: [PATCH 30/35] Update SimulationWindow.xaml.cs --- src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs index 34ddf99..c4b5ddd 100644 --- a/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs +++ b/src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs @@ -100,8 +100,8 @@ private void Draw(Bounds bounds) { var rect = new Rectangle { - Stroke = new SolidColorBrush(Colors.Red), - Fill = new SolidColorBrush(Colors.Transparent), + Stroke = Brushes.Red, + Fill = Brushes.Transparent, Width = bounds.Width, Height = bounds.Height, }; From 8806d6dbab59a923020e9404438adc712e6eef28 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 11:52:17 +0200 Subject: [PATCH 31/35] Create ICommand.cs --- .../Commands/Common/ICommand.cs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/Common/ICommand.cs diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/Common/ICommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/Common/ICommand.cs new file mode 100644 index 0000000..d8d0a71 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/Common/ICommand.cs @@ -0,0 +1,7 @@ +namespace Avans.FlatGalaxy.Simulation.Commands.Common +{ + public interface ICommand + { + void Execute(ISimulator simulator); + } +} From 53840f90dabfbb69ebec554446141242b170218b Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:05:55 +0200 Subject: [PATCH 32/35] Create ListExtensions.cs --- .../Extensions/ListExtensions.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Simulation/Extensions/ListExtensions.cs diff --git a/src/Avans.FlatGalaxy.Simulation/Extensions/ListExtensions.cs b/src/Avans.FlatGalaxy.Simulation/Extensions/ListExtensions.cs new file mode 100644 index 0000000..a68ddaa --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Extensions/ListExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Avans.FlatGalaxy.Simulation.Extensions +{ + public static class ListExtensions + { + public static T Random(this IEnumerable source) + { + return source.Random(1).Single(); + } + + public static IEnumerable Random(this IEnumerable source, int count) + { + return source.Shuffle().Take(count); + } + + public static IEnumerable Shuffle(this IEnumerable source) + { + return source.OrderBy(x => Guid.NewGuid()); + } + } +} From 334842ce4dd6eb008e80739310134b9195f4c183 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:06:22 +0200 Subject: [PATCH 33/35] Update Simulator --- src/Avans.FlatGalaxy.Simulation/ISimulator.cs | 18 +++++++ src/Avans.FlatGalaxy.Simulation/Simulator.cs | 51 +++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs index 42fc251..334a102 100644 --- a/src/Avans.FlatGalaxy.Simulation/ISimulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/ISimulator.cs @@ -9,14 +9,32 @@ public interface ISimulator public const int Height = 600; + public const double SpeedDiff = 0.1; + + public const double BookmarkTime = 5000; + Galaxy Galaxy { get; set; } QuadTree QuadTree { get; set; } + bool CollisionVisible { get; set; } + void Resume(); void Pause(); void Restore(); + + void SpeedUp(double speed); + + void SpeedDown(double speed); + + void SwitchCollisionAlgo(); + + void AddAsteroid(); + + void RemoveAsteroid(); + + void ToggleCollisionVisibility(); } } diff --git a/src/Avans.FlatGalaxy.Simulation/Simulator.cs b/src/Avans.FlatGalaxy.Simulation/Simulator.cs index 2d19973..48c73e5 100644 --- a/src/Avans.FlatGalaxy.Simulation/Simulator.cs +++ b/src/Avans.FlatGalaxy.Simulation/Simulator.cs @@ -1,11 +1,16 @@ using System; +using System.Drawing; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Avans.FlatGalaxy.Models; +using Avans.FlatGalaxy.Models.CelestialBodies; +using Avans.FlatGalaxy.Models.CelestialBodies.States; using Avans.FlatGalaxy.Simulation.Bookmark; using Avans.FlatGalaxy.Simulation.Bookmark.Common; using Avans.FlatGalaxy.Simulation.Collision; using Avans.FlatGalaxy.Simulation.Data; +using Avans.FlatGalaxy.Simulation.Extensions; namespace Avans.FlatGalaxy.Simulation { @@ -17,7 +22,7 @@ public class Simulator : ISimulator private DateTime _lastTick = DateTime.UtcNow; - private int _speed = 50; + private double _speed = 25; private bool _running = false; private DateTime _lastBookmark; @@ -37,6 +42,8 @@ public Simulator(Galaxy galaxy) public QuadTree QuadTree { get; set; } + public bool CollisionVisible { get; set; } = false; + public void Resume() { if (_running) return; @@ -60,7 +67,45 @@ public void Restore() _caretaker.Undo(); } - public void Tick(CancellationToken token) + public void SpeedUp(double speed) + { + _speed += speed; + } + + public void SpeedDown(double speed) + { + _speed -= speed; + } + + public void SwitchCollisionAlgo() + { + _collisionHandler.Toggle(); + } + + public void ToggleCollisionVisibility() + { + CollisionVisible = !CollisionVisible; + } + + public void AddAsteroid() + { + var rnd = new Random(); + var x = rnd.NextDouble() * ISimulator.Width; + 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())); + } + + public void RemoveAsteroid() + { + var asteroid = Galaxy.CelestialBodies.OfType().Random(); + Galaxy.Remove(asteroid); + } + + private void Tick(CancellationToken token) { if (_running) { @@ -74,7 +119,7 @@ public void Tick(CancellationToken token) _collisionHandler.Detect(this); _lastTick = DateTime.UtcNow; - if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= 1) + if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= ISimulator.BookmarkTime) { _caretaker.Save(); _lastBookmark = DateTime.UtcNow; From 7191e26ea61ea96370b38ad874233b94d9059b70 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:06:28 +0200 Subject: [PATCH 34/35] Create Commands --- .../Commands/AddAsteroidCommand.cs | 12 ++++++++++++ .../Commands/CollisionSwitchCommand.cs | 12 ++++++++++++ .../Commands/CollisionVisibilityCommand.cs | 12 ++++++++++++ .../Commands/PauseCommand.cs | 12 ++++++++++++ .../Commands/RemoveAsteroidCommand.cs | 12 ++++++++++++ .../Commands/RestoreCommand.cs | 12 ++++++++++++ .../Commands/ResumeCommand.cs | 12 ++++++++++++ .../Commands/SpeedDownCommand.cs | 12 ++++++++++++ .../Commands/SpeedUpCommand.cs | 12 ++++++++++++ 9 files changed, 108 insertions(+) create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/AddAsteroidCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/CollisionSwitchCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/CollisionVisibilityCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/PauseCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/RemoveAsteroidCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/RestoreCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/ResumeCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs create mode 100644 src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/AddAsteroidCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/AddAsteroidCommand.cs new file mode 100644 index 0000000..f1547ec --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/AddAsteroidCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class AddAsteroidCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.AddAsteroid(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/CollisionSwitchCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/CollisionSwitchCommand.cs new file mode 100644 index 0000000..35ebc32 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/CollisionSwitchCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class CollisionSwitchCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.SwitchCollisionAlgo(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/CollisionVisibilityCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/CollisionVisibilityCommand.cs new file mode 100644 index 0000000..9695350 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/CollisionVisibilityCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class CollisionVisibilityCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.ToggleCollisionVisibility(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/PauseCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/PauseCommand.cs new file mode 100644 index 0000000..cf4481f --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/PauseCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class PauseCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.Pause(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/RemoveAsteroidCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/RemoveAsteroidCommand.cs new file mode 100644 index 0000000..cdb25e0 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/RemoveAsteroidCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class RemoveAsteroidCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.RemoveAsteroid(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/RestoreCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/RestoreCommand.cs new file mode 100644 index 0000000..18ebc12 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/RestoreCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class RestoreCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.Restore(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/ResumeCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/ResumeCommand.cs new file mode 100644 index 0000000..9c09e83 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/ResumeCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class ResumeCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.Resume(); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs new file mode 100644 index 0000000..c7cb9b4 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class SpeedDownCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.SpeedDown(ISimulator.SpeedDiff); + } + } +} diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs new file mode 100644 index 0000000..5cae4a7 --- /dev/null +++ b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs @@ -0,0 +1,12 @@ +using Avans.FlatGalaxy.Simulation.Commands.Common; + +namespace Avans.FlatGalaxy.Simulation.Commands +{ + public class SpeedUpCommand : ICommand + { + public void Execute(ISimulator simulator) + { + simulator.SpeedUp(ISimulator.SpeedDiff); + } + } +} From 3b279675f0ee42573311de67a4d3b4664c85724c Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 28 Oct 2021 12:11:39 +0200 Subject: [PATCH 35/35] Update Speed commands --- .../Commands/SpeedDownCommand.cs | 9 ++++++++- .../Commands/SpeedUpCommand.cs | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs index c7cb9b4..10d9746 100644 --- a/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs +++ b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs @@ -4,9 +4,16 @@ namespace Avans.FlatGalaxy.Simulation.Commands { public class SpeedDownCommand : ICommand { + private readonly double _speedDiff; + + public SpeedDownCommand(double speedDiff = ISimulator.SpeedDiff) + { + _speedDiff = speedDiff; + } + public void Execute(ISimulator simulator) { - simulator.SpeedDown(ISimulator.SpeedDiff); + simulator.SpeedDown(_speedDiff); } } } diff --git a/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs index 5cae4a7..1cdb1cd 100644 --- a/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs +++ b/src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs @@ -4,9 +4,16 @@ namespace Avans.FlatGalaxy.Simulation.Commands { public class SpeedUpCommand : ICommand { + private readonly double _speedDiff; + + public SpeedUpCommand(double speedDiff = ISimulator.SpeedDiff) + { + _speedDiff = speedDiff; + } + public void Execute(ISimulator simulator) { - simulator.SpeedUp(ISimulator.SpeedDiff); + simulator.SpeedUp(_speedDiff); } } }