Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Merge branch 'feature/shortcuts' of https://github.com/avans-alga-dpa…
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyhosewol committed Oct 28, 2021
2 parents a019f96 + 7bdaa34 commit 396ca7f
Show file tree
Hide file tree
Showing 24 changed files with 391 additions and 96 deletions.
1 change: 1 addition & 0 deletions .idea/.idea.Avans.FlatGalaxy/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void Collide(CelestialBody other)
CollisionState.Collide(this, other);
}

public double DistanceTo(CelestialBody target) => Math.Pow(CenterX - target.CenterX, 2) + Math.Pow(CenterY - target.CenterY, 2);

public IDisposable Subscribe(IObserver<CelestialBody> observer)
{
_observers.Add(observer);
Expand Down
14 changes: 12 additions & 2 deletions src/Avans.FlatGalaxy.Models/Galaxy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Avans.FlatGalaxy.Models.CelestialBodies;
using Avans.FlatGalaxy.Models.CelestialBodies.States;

Expand Down Expand Up @@ -42,14 +43,23 @@ public void Remove(CelestialBody celestialBody)
_celestialBodies.Remove(celestialBody);
}

public void MapNeighbours(IDictionary<Planet, string[]> planetNeighbours)
{
foreach (var (planet, neighbours) in planetNeighbours)
{
foreach (var neighbour in neighbours)
{
planet.Neighbours.Add(CelestialBodies.OfType<Planet>().First(b => b.Name == neighbour));
}
}
}

public void OnCompleted()
{
throw new NotImplementedException();
}

public void OnError(Exception error)
{
throw new NotImplementedException();
}

public void OnNext(CelestialBody celestialBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public CelestialBody Create(string type, double x, double y, double vx, double v
};
}

private static ICollisionState GetCollisionState(string collisionName)
{
return collisionName.ToLower() switch
private static ICollisionState GetCollisionState(string collisionName) => collisionName.ToLower() switch
{
"blink" => new BlinkState(),
"bounce" => new BounceState(),
Expand All @@ -34,6 +32,5 @@ private static ICollisionState GetCollisionState(string collisionName)
"grow" => new GrowState(),
_ => new NullCollisionState()
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,5 @@ protected ConfigurationParserBase(ICelestialBodyFactory celestialBodyFactory)
public abstract Galaxy Parse(string content);

public abstract bool CanParse(string content);

protected static void MapNeighbours(Galaxy galaxy, Dictionary<Planet, string[]> planetNeighbours)
{
foreach (var (planet, neighbours) in planetNeighbours)
{
foreach (var neighbour in neighbours)
{
planet.Neighbours.Add(galaxy.CelestialBodies.OfType<Planet>().First(b => b.Name == neighbour));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override Galaxy Parse(string content)
}
}

MapNeighbours(galaxy, planetNeighbours);
galaxy.MapNeighbours(planetNeighbours);
return galaxy;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override Galaxy Parse(string content)
if (body is Planet planet) planetNeighbours.Add(planet, neighbours.ToArray());
}

MapNeighbours(galaxy, planetNeighbours);
galaxy.MapNeighbours(planetNeighbours);
return galaxy;
}
}
Expand Down
41 changes: 29 additions & 12 deletions src/Avans.FlatGalaxy.Presentation/Commands/ShortcutList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Avans.FlatGalaxy.Simulation;
using Avans.FlatGalaxy.Simulation.Commands;

namespace Avans.FlatGalaxy.Presentation.Commands
Expand All @@ -8,63 +10,70 @@ public class ShortcutList : List<Shortcut>
{
public ShortcutList()
{
Add(new Shortcut
Add(new()
{
Command = new PauseCommand(),
Description = "Pause simulation",
Key = Key.P
});
Add(new Shortcut

Add(new()
{
Command = new ResumeCommand(),
Description = "Resume simulation",
Key = Key.R
});
Add(new Shortcut

Add(new()
{
Command = new SpeedUpCommand(),
Description = "Speed up simulation",
Key = Key.OemPlus
});

Add(new Shortcut
Add(new()
{
Command = new SpeedUpCommand(),
Description = "Speed down simulation",
Key = Key.OemMinus,
});

Add(new Shortcut
Add(new()
{
Command = new RestoreCommand(),
Description = "Go back 5 seconds in simulation",
Key = Key.Back,
});

Add(new Shortcut
Add(new()
{
Command = new CollisionSwitchCommand(),
Description = "Switch collision (Quad tree / Naive)",
Key = Key.K,
});

Add(new Shortcut
Add(new()
{
Command = new CollisionVisibilityCommand(),
Description = "Switch collision visibility",
Key = Key.L,
});

Add(new Shortcut
Add(new()
{
Command = new PathSwitchCommand(),
Description = "Switch path finding (Breadth first / Dijkstra)",
Key = Key.J,
});

Add(new()
{
Command = new AddAsteroidCommand(),
Description = "Add asteroid to simulation",
Key = Key.U,
});

Add(new Shortcut
Add(new()
{
Command = new RemoveAsteroidCommand(),
Description = "Remove asteroid from simulation",
Expand All @@ -78,5 +87,13 @@ public ShortcutList()
Key = Key.I,
});
}

public void HandleKey(Key key, ISimulator simulator)
{
foreach (var shortcut in this.Where(shortcut => shortcut.Key == key))
{
shortcut.Command.Execute(simulator);
}
}
}
}
}
33 changes: 27 additions & 6 deletions src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Avans.FlatGalaxy.Models;
using Avans.FlatGalaxy.Models.CelestialBodies;
using Avans.FlatGalaxy.Presentation.Commands;
using Avans.FlatGalaxy.Presentation.Extensions;
using Avans.FlatGalaxy.Simulation;
using Avans.FlatGalaxy.Simulation.Data;
Expand All @@ -15,17 +19,26 @@ public partial class SimulationWindow : Window, IObserver<ISimulator>
{
private readonly MainWindow _mainWindow;
private ISimulator? _simulator;
private readonly ShortcutList _shortcutList;

public SimulationWindow(MainWindow mainWindow)
public SimulationWindow(MainWindow mainWindow, ShortcutList shortcutList)
{
_mainWindow = mainWindow;
_shortcutList = shortcutList;

InitializeComponent();

GalaxyCanvas.Width = ISimulator.Width;
GalaxyCanvas.Height = ISimulator.Height;

Loaded += OnLoaded;
Unloaded += OnUnloaded;
KeyUp += OnKeyUp;
}

private void OnKeyUp(object sender, KeyEventArgs e)
{
if (_simulator != null) _shortcutList.HandleKey(e.Key, _simulator);
}

private void OnLoaded(object sender, RoutedEventArgs e)
Expand All @@ -42,8 +55,10 @@ 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 == null) return;

if (_simulator.Galaxy != null) { Draw(_simulator.Galaxy, _simulator.PathSteps); }
if (_simulator.CollisionVisible && _simulator.QuadTree != null) { Draw(_simulator.QuadTree); }
}

public void Show(Galaxy galaxy)
Expand All @@ -55,15 +70,19 @@ public void Show(Galaxy galaxy)
_simulator.Resume();
}

private void Draw(Galaxy galaxy)
private void Draw(Galaxy galaxy, ICollection<Planet> pathSteps)
{
var pathColor = Colors.Chartreuse;

GalaxyCanvas.Children.Clear();

foreach (var celestialBody in galaxy.CelestialBodies)
{
var ellipse = new Ellipse
{
Height = celestialBody.Diameter,
Width = celestialBody.Diameter,
Fill = new SolidColorBrush(celestialBody.Color.ToColor()),
Fill = new SolidColorBrush(pathSteps?.Contains(celestialBody) ?? false ? pathColor : celestialBody.Color.ToColor()),
};

GalaxyCanvas.Children.Add(ellipse);
Expand All @@ -75,9 +94,11 @@ private void Draw(Galaxy galaxy)
{
foreach (var neighbour in planet.Neighbours)
{
var isStepLine = pathSteps != null && pathSteps.Contains(planet) && pathSteps.Contains(neighbour);

GalaxyCanvas.Children.Add(new Line
{
Stroke = new SolidColorBrush(Colors.Blue),
Stroke = new SolidColorBrush(isStepLine ? pathColor : Colors.Blue),
X1 = celestialBody.CenterX,
Y1 = celestialBody.CenterY,
X2 = neighbour.CenterX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class Caretaker<T> : ICaretaker

protected Caretaker()
{
_mementos = new Stack<IMemento<T>>();
_mementos = new();
}

protected abstract IMemento<T> Create();
Expand Down
12 changes: 8 additions & 4 deletions src/Avans.FlatGalaxy.Simulation/Bookmark/GalaxyMemento.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Avans.FlatGalaxy.Models;
using Avans.FlatGalaxy.Models.CelestialBodies;
Expand All @@ -8,16 +9,19 @@ namespace Avans.FlatGalaxy.Simulation.Bookmark
{
public class GalaxyMemento : IMemento<Galaxy>
{
private readonly IList<CelestialBody> _celestialBodies;
private readonly IDictionary<CelestialBody, string[]> _celestialBodies;

public GalaxyMemento(Galaxy galaxy)
{
_celestialBodies = galaxy.CelestialBodies.Select(body => body.Clone()).ToList();
_celestialBodies = galaxy.CelestialBodies.ToDictionary(body => body.Clone(), body => body is Planet planet ? planet.Neighbours.Select(planet1 => planet1.Name).ToArray() : Array.Empty<string>());
}

public Galaxy GetState()
{
return new Galaxy(_celestialBodies);
var galaxy = new Galaxy(_celestialBodies.Keys);
galaxy.MapNeighbours(_celestialBodies.Where(pair => pair.Key is Planet).ToDictionary(pair => pair.Key as Planet, pair => pair.Value));

return galaxy;
}
}
}
27 changes: 7 additions & 20 deletions src/Avans.FlatGalaxy.Simulation/Collision/CollisionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
using System.Collections.Generic;
using Avans.FlatGalaxy.Models.CelestialBodies;
using Avans.FlatGalaxy.Simulation.Common;

namespace Avans.FlatGalaxy.Simulation.Collision
{
public class CollisionHandler
public class CollisionHandler : ImplementationSwapper<ICollisionDetector>
{
private readonly List<ICollisionDetector> _detectors;
private readonly List<KeyValuePair<CelestialBody, CelestialBody>> _collisions;

private int _currentDetector;

public CollisionHandler()
{
_detectors = new List<ICollisionDetector>
{
new QuadTreeCollisionDetector(),
new NaiveCollisionDetector()
};
_collisions = new List<KeyValuePair<CelestialBody, CelestialBody>>();
}

public void Toggle()
{
if (++_currentDetector >= _detectors.Count)
{
_currentDetector = 0;
}
Add(new QuadTreeCollisionDetector());
Add(new NaiveCollisionDetector());
_collisions = new();
}

public void Detect(ISimulator simulator)
{
_detectors[_currentDetector].Detect(simulator, this);
Current.Detect(simulator, this);

TriggerEnd();
}
Expand All @@ -39,7 +26,7 @@ public void AddCollision(CelestialBody body1, CelestialBody body2)
{
if (body1 == body2) return;

var pair = body1.GetHashCode() < body2.GetHashCode() ? new KeyValuePair<CelestialBody, CelestialBody>(body1, body2) : new KeyValuePair<CelestialBody, CelestialBody>(body2, body1);
KeyValuePair<CelestialBody, CelestialBody> pair = body1.GetHashCode() < body2.GetHashCode() ? new(body1, body2) : new(body2, body1);

if (!_collisions.Contains(pair))
{
Expand Down
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/PathSwitchCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Avans.FlatGalaxy.Simulation.Commands.Common;

namespace Avans.FlatGalaxy.Simulation.Commands
{
public class PathSwitchCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.SwitchPathAlgo();
}
}
}
Loading

0 comments on commit 396ca7f

Please sign in to comment.