Czoko_Smieciarka/Trunk/MonoGameView/DataModels/Models/Steps/SpillStep.cs

50 lines
1.9 KiB
C#
Raw Normal View History

2019-03-13 16:55:34 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2019-04-23 06:32:35 +02:00
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
2019-03-13 16:55:34 +01:00
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
2019-03-13 16:55:34 +01:00
{
public class SpillStep : IStep
{
2019-04-23 06:32:35 +02:00
public SpillStep(GarbageType typeOfGarbage)
2019-03-13 16:55:34 +01:00
{
this._typeOfGarbage = typeOfGarbage;
}
2019-04-22 03:16:30 +02:00
2019-04-23 06:32:35 +02:00
private GarbageType _typeOfGarbage;
2019-03-13 16:55:34 +01:00
2019-04-23 06:32:35 +02:00
public void Invoke(IGarbageCollector _garbageCollector, object [,] grid)
{
var _dump = (ADump)grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y];
if(_garbageCollector.Coords != _dump.Coords)
2019-03-13 16:55:34 +01:00
throw new WrongPositionException("Śmieciarka nie na terenie podanego wyspiska");
2019-04-23 06:32:35 +02:00
if(_dump.TypeOfGarbage.GarbageType != _typeOfGarbage)
throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage}");
2019-03-13 16:55:34 +01:00
2019-04-23 06:32:35 +02:00
if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage))
throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage}!");
2019-03-13 16:55:34 +01:00
2019-04-23 06:32:35 +02:00
var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage)
2019-03-13 16:55:34 +01:00
.Select(t => t.TakeGarbage())
.Aggregate((a, b) => a + b);
2019-04-23 06:32:35 +02:00
_dump.AddGarbage(garbage);
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y));
}
}
2019-03-13 16:55:34 +01:00
}
}
}