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

Commit

Permalink
Merge pull request #21 from avans-alga-dpa/feature/commands
Browse files Browse the repository at this point in the history
Commands
  • Loading branch information
Sander0542 authored Oct 28, 2021
2 parents f990251 + 3b27967 commit 872b8c2
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/AddAsteroidCommand.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 AddAsteroidCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.AddAsteroid();
}
}
}
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/CollisionSwitchCommand.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 CollisionSwitchCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.SwitchCollisionAlgo();
}
}
}
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 CollisionVisibilityCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.ToggleCollisionVisibility();
}
}
}
7 changes: 7 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/Common/ICommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Avans.FlatGalaxy.Simulation.Commands.Common
{
public interface ICommand
{
void Execute(ISimulator simulator);
}
}
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/PauseCommand.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 PauseCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.Pause();
}
}
}
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/RemoveAsteroidCommand.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 RemoveAsteroidCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.RemoveAsteroid();
}
}
}
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/RestoreCommand.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 RestoreCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.Restore();
}
}
}
12 changes: 12 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/ResumeCommand.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 ResumeCommand : ICommand
{
public void Execute(ISimulator simulator)
{
simulator.Resume();
}
}
}
19 changes: 19 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/SpeedDownCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Avans.FlatGalaxy.Simulation.Commands.Common;

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(_speedDiff);
}
}
}
19 changes: 19 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Commands/SpeedUpCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Avans.FlatGalaxy.Simulation.Commands.Common;

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(_speedDiff);
}
}
}
24 changes: 24 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(this IEnumerable<T> source)
{
return source.Random(1).Single();
}

public static IEnumerable<T> Random<T>(this IEnumerable<T> source, int count)
{
return source.Shuffle().Take(count);
}

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
return source.OrderBy(x => Guid.NewGuid());
}
}
}
18 changes: 18 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/ISimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
51 changes: 48 additions & 3 deletions src/Avans.FlatGalaxy.Simulation/Simulator.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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<Asteroid>().Random();
Galaxy.Remove(asteroid);
}

private void Tick(CancellationToken token)
{
if (_running)
{
Expand All @@ -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;
Expand Down

0 comments on commit 872b8c2

Please sign in to comment.