using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawingObjects;
using DumpTruck.MovementStrategy;
namespace DumpTruck.Generics
{
///
/// Параметризованный класс для набора объектов DrawingTruck
///
///
///
internal class TrucksGenericCollection
where T : DrawingTruck
where U : IMoveableObject
{
///
/// Ширина окна прорисовки
///
private readonly int _pictureWidth;
///
/// Высота окна прорисовки
///
private readonly int _pictureHeight;
///
/// Размер занимаемого объектом места (ширина)
///
private readonly int _placeSizeWidth = 180;
///
/// Размер занимаемого объектом места (высота)
///
private readonly int _placeSizeHeight = 100;
///
/// Набор объектов
///
private readonly SetGeneric _collection;
///
/// Конструктор
///
///
///
public TrucksGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric(width * height);
}
///
/// Перегрузка оператора сложения
///
///
///
///
public static int operator +(TrucksGenericCollection collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect._collection.Insert(obj);
}
///
/// Перегрузка оператора вычитания
///
///
///
///
public static T? operator -(TrucksGenericCollection collect, int pos)
{
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
///
/// Получение объекта IMoveableObject
///
///
///
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
}
///
/// Вывод всего набора объектов
///
///
public Bitmap ShowTrucks()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
///
/// Метод отрисовки фона
///
///
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
///
/// Метод прорисовки объектов
///
///
private void DrawObjects(Graphics g)
{
int index = 0;
foreach (var truck in _collection.GetTrucks())
{
if (truck != null)
{
truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
truck.DrawTransport(g);
}
index++;
}
}
}
}