namespace ArmoredVehicle
{
///
/// Карта с набром объектов под нее
///
///
///
internal class MapWithSetMachineGeneric
where T : class, IDrawningObject
where U : AbstractMap
{
///
/// Ширина окна отрисовки
///
private readonly int _pictureWidth;
///
/// Высота окна отрисовки
///
private readonly int _pictureHeight;
///
/// Размер занимаемого объектом места (ширина)
///
private readonly int _placeSizeWidth = 210;
///
/// Размер занимаемого объектом места (высота)
///
private readonly int _placeSizeHeight = 90;
///
/// Набор объектов
///
private readonly SetMachineGeneric _setMachines;
///
/// Карта
///
private readonly U _map;
///
/// Конструктор
///
///
///
///
public MapWithSetMachineGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setMachines = new SetMachineGeneric(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
///
/// Перегрузка оператора сложения
///
///
///
///
public static bool operator +(MapWithSetMachineGeneric map, T car)
{
return map._setMachines.Insert(car);
}
///
/// Перегрузка оператора вычитания
///
///
///
///
public static bool operator -(MapWithSetMachineGeneric map, int position)
{
return map._setMachines.Remove(position);
}
///
/// Вывод всего набора объектов
///
///
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawMachine(gr);
return bmp;
}
///
/// Просмотр объекта на карте
///
///
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setMachines.Count; i++)
{
var car = _setMachines.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
}
return new(_pictureWidth, _pictureHeight);
}
///
/// Перемещение объекта по крате
///
///
///
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
///
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
///
private void Shaking()
{
int j = _setMachines.Count - 1;
for (int i = 0; i < _setMachines.Count; i++)
{
if (_setMachines.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setMachines.Get(j);
if (car != null)
{
_setMachines.Insert(car, i);
_setMachines.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
///
/// Метод отрисовки фона
///
///
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 DrawMachine(Graphics g)
{
for (int i = 0; i < _setMachines.Count; i++)
{
// TODO установка позиции
_setMachines.Get(i)?.DrawningObject(g);
}
}
}
}