using Cruiser.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Drawing;
namespace Cruiser.Generics
{
///
/// Параметризованный класс для набора объектов DrawingCruiser
///
///
///
internal class CarsGenericCollection
where T : DrawingCruiser
where U : IMoveableObject
{
///
/// Ширина окна прорисовки
///
private readonly int _pictureWidth;
///
/// Высота окна прорисовки
///
private readonly int _pictureHeight;
///
/// Размер занимаемого объектом места (ширина)
///
private readonly int _placeSizeWidth = 160;
///
/// Размер занимаемого объектом места (высота)
///
private readonly int _placeSizeHeight = 60;
///
/// Набор объектов
///
private readonly SetGeneric _collection;
///
/// Конструктор
///
///
///
public CarsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric(width * height);
}
///
/// Перегрузка оператора сложения
///
///
///
///
public static bool operator +(CarsGenericCollection collect, T? obj)
{
if (obj == null)
{
return false;
}
return collect._collection.Insert(obj);
}
///
/// Перегрузка оператора вычитания
///
///
///
///
public static T? operator -(CarsGenericCollection 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 ShowCruiser()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawObjects(gr);
DrawBackground(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; j++)
{
g.DrawRectangle(pen, i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
}
}
}
///
/// /// Метод прорисовки объектов
///
///
private void DrawObjects(Graphics g)
{
int Ix = 0;
int Iy = 0;
int i = 0;
foreach (var cruiser in _collection.GetCruisers())
{
_collection[i]?.SetPosition(Ix, Iy);
_collection[i]?.DrawTransport(g);
Ix += _placeSizeWidth;
if (Ix + _placeSizeHeight > _pictureWidth)
{
Ix = 0;
Iy = _placeSizeHeight;
}
i++;
}
}
}
}