public void Multiple_units_can_move_east_in_parallel() { const int repetitions = 1; var movementProcessor = new MovementProcessor(); INewLocationCalculator newLocationCalculator = NewLocationCalculatorFactory.GetNewLocationCalculator(CompassDirection.East); var requests = new ProcessRequest[repetitions]; for (int i = 0; i < repetitions; i++) { requests[i] = new ProcessRequest(Point2.Empty, 2.0f + i); } Stopwatch sw = Stopwatch.StartNew(); ProcessResponse[] response = movementProcessor.Process(requests, newLocationCalculator); sw.Stop(); Console.WriteLine($"Time taken: {sw.ElapsedMilliseconds} ms"); int cnt = 0; foreach (ProcessResponse item in response) { Assert.AreEqual(1.0f + cnt, item.NewMovementPoints, "NewMovementPoints incorrect."); cnt++; } }
public Unit Execute(Unit unit, object parameters) { var compassDirection = (CompassDirection)parameters; INewLocationCalculator newLocationCalculator = NewLocationCalculatorFactory.GetNewLocationCalculator(compassDirection); ProcessResponse response = Globals.Instance.GameWorld.MovementProcessor.Process(new ProcessRequest(unit.Location, unit.MovementPoints), newLocationCalculator); Unit ret = Unit.Create(unit.UnitType, response.NewLocation, response.NewMovementPoints); return(ret); }
public ProcessResponse Process(ProcessRequest request, INewLocationCalculator newLocationCalculator) { Point2 newLocation = DetermineNewPosition(request.Location, newLocationCalculator); int movementCost = DetermineMovementCost(newLocation); bool canMoveIntoCell = CanMoveIntoCell(request.MovementPoints, movementCost); if (canMoveIntoCell) { float newMovementPoints = request.MovementPoints - movementCost; return(new ProcessResponse(newLocation, newMovementPoints)); } return(new ProcessResponse(request.Location, request.MovementPoints)); }
public ProcessResponse[] Process(ProcessRequest[] requests, INewLocationCalculator newLocationCalculator) { ProcessResponse[] response = new ProcessResponse[requests.Length]; //int cnt = 0; //foreach (ProcessRequest request in requests) Parallel.ForEach(requests, (request, state, cnt) => { var resp = Process(request, newLocationCalculator); response[cnt] = resp; } ); return(response); }
public void Unit_can_move_east_in_parallel() { var movementProcessor = new MovementProcessor(); INewLocationCalculator newLocationCalculator = NewLocationCalculatorFactory.GetNewLocationCalculator(CompassDirection.East); ProcessRequest[] requests = { new ProcessRequest(Point2.Empty, 2.0f) }; Stopwatch sw = Stopwatch.StartNew(); ProcessResponse[] response = movementProcessor.Process(requests, newLocationCalculator); sw.Stop(); Console.WriteLine($"Time taken: {sw.ElapsedMilliseconds} ms"); Assert.AreEqual(Point2.Create(1, 0), response[0].NewLocation, "NewLocation incorrect."); Assert.AreEqual(1.0f, response[0].NewMovementPoints, "NewMovementPoints incorrect."); }
public void Unit_can_move_east() { var movementProcessor = new MovementProcessor(); INewLocationCalculator newLocationCalculator = NewLocationCalculatorFactory.GetNewLocationCalculator(CompassDirection.East); Stopwatch sw = Stopwatch.StartNew(); ProcessResponse response = movementProcessor.Process(new ProcessRequest(Point2.Empty, 2.0f), newLocationCalculator); sw.Stop(); Console.WriteLine($"Time taken: {sw.ElapsedMilliseconds} ms"); Unit unit = Unit.Create(0, response.NewLocation, response.NewMovementPoints); Assert.AreEqual(Point2.Create(1, 0), unit.Location, "Location incorrect."); Assert.AreEqual(1.0f, unit.MovementPoints, "MovementPoints incorrect."); }
public void Multiple_units_can_move_east_single_threaded() { var movementProcessor = new MovementProcessor(); INewLocationCalculator newLocationCalculator = NewLocationCalculatorFactory.GetNewLocationCalculator(CompassDirection.East); const int repetitions = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < repetitions; i++) { movementProcessor.Process(new ProcessRequest(Point2.Empty, 2.0f), newLocationCalculator); } sw.Stop(); Console.WriteLine($"Time taken: {sw.ElapsedMilliseconds} ms for {repetitions}."); }
private Point2 DetermineNewPosition(Point2 currentLocation, INewLocationCalculator newLocationCalculator) { return(newLocationCalculator.Calculate(currentLocation)); }