2019-03-13 15:31:33 +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;
|
2019-05-13 14:12:28 +02:00
|
|
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
|
2019-04-22 09:24:16 +02:00
|
|
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
|
|
|
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
|
|
|
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
|
2019-04-23 06:32:35 +02:00
|
|
|
|
using MonoGameView.DataModels.Models;
|
2019-03-13 15:31:33 +01:00
|
|
|
|
|
2019-04-22 09:24:16 +02:00
|
|
|
|
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
|
2019-03-13 15:31:33 +01:00
|
|
|
|
{
|
|
|
|
|
public class CollectStep : IStep
|
|
|
|
|
{
|
2019-04-23 06:32:35 +02:00
|
|
|
|
public CollectStep(GarbageType typeOfGarbage)
|
2019-03-13 15:31:33 +01:00
|
|
|
|
{
|
|
|
|
|
this._typeOfGarbage = typeOfGarbage;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 06:32:35 +02:00
|
|
|
|
private GarbageType _typeOfGarbage;
|
2019-03-13 15:31:33 +01:00
|
|
|
|
|
2019-05-05 17:54:12 +02:00
|
|
|
|
public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid)
|
2019-03-13 15:31:33 +01:00
|
|
|
|
{
|
2019-04-23 06:32:35 +02:00
|
|
|
|
|
|
|
|
|
var _garbageLocalization = (IGarbageLocalization) grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y];
|
2019-04-23 11:24:07 +02:00
|
|
|
|
|
2019-04-23 06:32:35 +02:00
|
|
|
|
var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage);
|
2019-04-23 11:24:07 +02:00
|
|
|
|
if (!trashCans.Any()) return false;
|
2019-05-13 14:12:28 +02:00
|
|
|
|
var preGarbage = trashCans.Select(t => t.TakeGarbage()).ToList();
|
|
|
|
|
if (preGarbage.Any(x => x == null)) return false;
|
|
|
|
|
var garbage = preGarbage.Aggregate((a,b)=>a+b);
|
2019-03-13 15:31:33 +01:00
|
|
|
|
|
2019-04-23 06:32:35 +02:00
|
|
|
|
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage))
|
2019-04-23 11:24:07 +02:00
|
|
|
|
return false;
|
2019-03-13 15:31:33 +01:00
|
|
|
|
|
2019-05-13 14:12:28 +02:00
|
|
|
|
var r = _garbageCollector.TrashContainers.First(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage).AddGarbage(garbage);
|
|
|
|
|
if (!r) return false;
|
2019-04-23 06:32:35 +02:00
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_garbageLocalization.TrashCans.All(x => x.FillPercent == 0.0))
|
2019-04-23 11:24:07 +02:00
|
|
|
|
{
|
2019-04-23 06:32:35 +02:00
|
|
|
|
grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y));
|
2019-04-23 11:24:07 +02:00
|
|
|
|
|
|
|
|
|
_garbageCollector.HouseCounter++;
|
|
|
|
|
}
|
|
|
|
|
_garbageCollector.Counter = 0;
|
|
|
|
|
return true;
|
2019-03-13 15:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|