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

55 lines
2.1 KiB
C#
Raw Normal View History

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;
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
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-04-23 11:24:07 +02:00
public bool Invoke(IGarbageCollector _garbageCollector, object [,] 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-03-13 15:31:33 +01:00
var garbage = trashCans.Select(t => t.TakeGarbage()).Aggregate((a,b)=>a+b);
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-04-23 06:32:35 +02:00
_garbageCollector.TrashContainers.First(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage).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));
}
}
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
}
}
}