PIbd-22_DozorovaAA_ArmoredV.../ArmoredVehicle/MapWithSetMachineGeneric.cs

222 lines
7.5 KiB
C#
Raw Normal View History

2022-09-21 20:59:18 +04:00
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;
}
2022-10-05 10:12:04 +04:00
2022-09-21 20:59:18 +04:00
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
2022-10-05 10:12:04 +04:00
/// <param name="machine"></param>
2022-09-21 20:59:18 +04:00
/// <returns></returns>
2022-10-05 10:12:04 +04:00
public static int operator +(MapWithSetMachineGeneric<T, U> map, T machine)
2022-09-21 20:59:18 +04:00
{
2022-10-05 10:12:04 +04:00
return map._setMachines.Insert(machine);
2022-09-21 20:59:18 +04:00
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
2022-10-05 10:12:04 +04:00
public static T operator -(MapWithSetMachineGeneric<T, U> map, int position)
2022-09-21 20:59:18 +04:00
{
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();
2022-10-09 13:17:48 +04:00
foreach (var machine in _setMachines.GetMachine())
2022-09-21 20:59:18 +04:00
{
2022-10-09 13:17:48 +04:00
return _map.CreateMap(_pictureWidth, _pictureHeight, machine);
2022-09-21 20:59:18 +04:00
}
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);
}
2022-11-13 13:41:25 +04:00
/// <summary>
/// Получение данных в виде строки
/// </summary>
/// <param name="sep"></param>
/// <returns></returns>
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var machine in _setMachines.GetMachine())
{
data += $"{machine.GetInfo()}{separatorData}";
}
return data;
}
/// <summary>
/// Загрузка списка из массива строк
/// </summary>
/// <param name="records"></param>
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setMachines.Insert(DrawningObject.Create(rec) as T);
}
}
2022-09-21 20:59:18 +04:00
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()
{
int j = _setMachines.Count - 1;
for (int i = 0; i < _setMachines.Count; i++)
{
2022-10-09 13:17:48 +04:00
if (_setMachines[i] == null)
2022-09-21 20:59:18 +04:00
{
for (; j > i; j--)
{
2022-10-09 13:17:48 +04:00
var car = _setMachines[j];
2022-09-21 20:59:18 +04:00
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)
{
2022-10-05 10:12:04 +04:00
Pen pen = new(Color.LawnGreen, 5);
2022-09-21 20:59:18 +04:00
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
2022-10-05 10:12:04 +04:00
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));
2022-09-21 20:59:18 +04:00
}
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawMachine(Graphics g)
{
2022-10-05 10:12:04 +04:00
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int currentWidth = 0;
int currentHeight = 0;
2022-10-09 13:17:48 +04:00
foreach (var machine in _setMachines.GetMachine())
2022-09-21 20:59:18 +04:00
{
2022-10-05 10:12:04 +04:00
int dop = 0;
2022-10-09 13:17:48 +04:00
if (currentWidth > 0 && currentHeight > 0)
2022-10-05 10:12:04 +04:00
{
dop = 30;
}
2022-10-09 13:17:48 +04:00
machine.SetObject(currentWidth * _placeSizeWidth + dop,
2022-10-05 10:12:04 +04:00
currentHeight * _placeSizeHeight + dop,
_pictureWidth, _pictureHeight);
2022-10-09 13:17:48 +04:00
machine?.DrawningObject(g);
2022-10-05 10:12:04 +04:00
if (currentWidth < width - 1)
{
currentWidth++;
}
else
{
currentWidth = 0;
currentHeight++;
}
if (currentHeight > height) return;
2022-09-21 20:59:18 +04:00
}
2022-10-09 13:17:48 +04:00
2022-09-21 20:59:18 +04:00
}
}
}