PIbd-22_DozorovaAA_ArmoredV.../ArmoredVehicle/MapWithSetMachineGeneric.cs
2022-10-05 10:12:04 +04:00

203 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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