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

59 lines
2.1 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-06-11 23:05:31 +02:00
public GarbageType GetTypeOfGarbage()
{
return _typeOfGarbage;
}
2019-05-05 17:54:12 +02:00
public bool Invoke(IGarbageCollector _garbageCollector, ICloneable [,] grid)
2019-04-23 06:32:35 +02:00
{
2019-04-23 11:24:07 +02:00
var _dump = (ADump)grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y];
2019-03-13 16:55:34 +01:00
2019-04-23 11:24:07 +02:00
if (_dump.TypeOfGarbage.GarbageType != _typeOfGarbage)
return false;
2019-03-13 16:55:34 +01:00
2019-04-23 11:24:07 +02:00
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage))
return false;
var containers = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage && t.FillPercent > 0);
if (containers.Any())
2019-04-23 06:32:35 +02:00
{
2019-05-14 19:55:00 +02:00
var preGarbage = containers.Select(t => t.TakeGarbage(double.MaxValue)).ToList();
2019-05-13 14:12:28 +02:00
if (preGarbage.Any(x => x == null)) return false;
var garbage = preGarbage.Aggregate((a, b) => a + b);
if (!_dump.AddGarbage(garbage)) return false;
2019-04-23 11:24:07 +02:00
for (int x = 0; x < grid.GetLength(0); x++)
2019-04-23 06:32:35 +02:00
{
2019-04-23 11:24:07 +02:00
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-04-23 06:32:35 +02:00
}
2019-04-23 11:24:07 +02:00
_garbageCollector.Counter = 0;
return true;
2019-04-23 06:32:35 +02:00
}
2019-04-23 11:24:07 +02:00
return false;
2019-03-13 16:55:34 +01:00
}
}
}