Czoko_Smieciarka/Trunk/MonoGameView/DataModels/Models/Steps/CollectStep.cs
Michał Dulski c3c0b57036 parser
2019-06-11 23:05:31 +02:00

67 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using MonoGameView.DataModels.Models;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
{
public class CollectStep : IStep
{
public CollectStep(GarbageType typeOfGarbage)
{
this._typeOfGarbage = typeOfGarbage;
}
private GarbageType _typeOfGarbage;
public GarbageType GetTypeOfGarbage()
{
return _typeOfGarbage;
}
public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid)
{
var _garbageLocalization = (IGarbageLocalization) grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y];
var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage);
if (!trashCans.Any()) return false;
var mainCan = _garbageCollector
.TrashContainers
.Where(x => x.TypeOfGarbage.GarbageType == _typeOfGarbage)
.First();
var maxVolume = mainCan.MaxVolume - mainCan.Garbage.Weight;
var preGarbage = trashCans.Select(t => t.TakeGarbage(maxVolume)).ToList();
if (preGarbage.Any(x => x == null)) return false;
var garbage = preGarbage.Aggregate((a,b)=>a+b);
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage))
return false;
var r = _garbageCollector.TrashContainers.First(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage).AddGarbage(garbage);
if (!r) return false;
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))
{
grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y));
_garbageCollector.HouseCounter++;
}
_garbageCollector.Counter = 0;
return true;
}
}
}