231 lines
8.7 KiB
C#
231 lines
8.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Numerics;
|
||
using System.Runtime.Intrinsics.Arm;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Airbus
|
||
{
|
||
//карта с набором объектов под неё
|
||
internal class MapWithSetPlanesGeneric<T, U>
|
||
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 SetPlanesGeneric<T> _setPlanes;
|
||
//карта
|
||
private readonly U _map;
|
||
|
||
//конструктор
|
||
public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map)
|
||
{
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_setPlanes = new SetPlanesGeneric<T>(width * height);
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_map = map;
|
||
}
|
||
|
||
//пеергрузка оператора сложения
|
||
public static int operator +(MapWithSetPlanesGeneric<T, U> map, T plane)
|
||
{
|
||
return map._setPlanes.Insert(plane);
|
||
}
|
||
|
||
//перегрузка оператора вычитания
|
||
public static T operator -(MapWithSetPlanesGeneric<T, U> map, int position)
|
||
{
|
||
return map._setPlanes.Remove(position);
|
||
}
|
||
|
||
//вывод всего набора объектов
|
||
public Bitmap ShowSet()
|
||
{
|
||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawBackground(gr);
|
||
DrawPlanes(gr);
|
||
return bmp;
|
||
}
|
||
|
||
//просмотр объекта на карте
|
||
public Bitmap ShowOnMap()
|
||
{
|
||
Shaking();
|
||
|
||
foreach (var plane in _setPlanes.GetAirbus())
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, plane);
|
||
}
|
||
|
||
return new(_pictureWidth, _pictureHeight);
|
||
}
|
||
|
||
//пеермещение объекта по карте
|
||
public Bitmap MoveObject(Direction direction)
|
||
{
|
||
if (_map != null)
|
||
{
|
||
return _map.MoveObject(direction);
|
||
}
|
||
|
||
return new(_pictureWidth, _pictureHeight);
|
||
}
|
||
//получение данных в виде строки
|
||
public string GetData(char separatorType, char separatorData)
|
||
{
|
||
string data = $"{_map.GetType().Name}{separatorType}";
|
||
|
||
foreach (var plane in _setPlanes.GetAirbus())
|
||
{
|
||
data += $"{plane.GetInfo()}{separatorData}";
|
||
}
|
||
|
||
return data;
|
||
}
|
||
|
||
//Загрузка списка из массива строк
|
||
public void LoadData(string[] records)
|
||
{
|
||
foreach (var rec in records)
|
||
{
|
||
_setPlanes.Insert(DrawningObjectPlane.Create(rec) as T);
|
||
}
|
||
}
|
||
|
||
//"взламываем" набор, чтобы все элементы оказались в начале
|
||
private void Shaking()
|
||
{
|
||
int j = _setPlanes.Count - 1;
|
||
|
||
for (int i = 0; i < _setPlanes.Count; i++)
|
||
{
|
||
if (_setPlanes[i] == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var plane = _setPlanes[j];
|
||
|
||
if (plane != null)
|
||
{
|
||
_setPlanes.Insert(plane, i);
|
||
_setPlanes.Remove(j);
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//метод отрисовки фона
|
||
public void DrawBackground(Graphics g)
|
||
{
|
||
Pen pen = new(Color.Black, 3);
|
||
Pen marcupPen = new(Color.White, 5);
|
||
Pen signalFirePen = new(Color.OrangeRed, 5);
|
||
Brush concreteBrush = new SolidBrush(Color.LightGray);
|
||
Brush asphaltBrush = new SolidBrush(Color.DarkGray);
|
||
Brush marcupBrush = new SolidBrush(Color.White);
|
||
|
||
//заливаем область в цвет бетона
|
||
g.FillRectangle(concreteBrush, 0, 0, _pictureWidth, _pictureHeight); //заливаем область в цвет бетона
|
||
|
||
|
||
for (int i = 0; i < _pictureWidth / _placeSizeWidth - 1; i++)
|
||
{
|
||
//линия разметки места
|
||
for (int j = 2; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||
{
|
||
g.DrawLine(pen, i * _placeSizeWidth +5, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 5, j * _placeSizeHeight);
|
||
g.DrawLine(pen, i * _placeSizeWidth +400, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 5, j * _placeSizeHeight);
|
||
}
|
||
|
||
g.DrawLine(pen, i * _placeSizeWidth +200, _placeSizeHeight *2, i * _placeSizeWidth +200, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight+100);
|
||
g.DrawLine(pen, i * _placeSizeWidth +5, _placeSizeHeight *2, i * _placeSizeWidth+5, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight+100);
|
||
|
||
}
|
||
//отрисовка разметки взлётной полосы
|
||
g.FillRectangle(marcupBrush, _placeSizeWidth * 2 + 30, 0, 185, _pictureHeight);
|
||
g.FillRectangle(asphaltBrush, _placeSizeWidth * 2 + 35, 0, 175, _pictureHeight);
|
||
g.DrawLine(marcupPen, _placeSizeWidth * 2 + 210, 0, _placeSizeWidth * 2 + 210, _pictureHeight);
|
||
g.DrawLine(marcupPen, _placeSizeWidth * 2 + 35, 0, _placeSizeWidth * 2 + 35, _pictureHeight);
|
||
g.DrawLine(marcupPen, _placeSizeWidth * 2 + 215, 0, _placeSizeWidth * 2 + 215, _pictureHeight);
|
||
g.DrawLine(marcupPen, _placeSizeWidth * 2 + 30, 0, _placeSizeWidth * 2 + 30, _pictureHeight);
|
||
|
||
for (int i = 0; i < _pictureHeight / _placeSizeHeight; ++i)
|
||
{
|
||
g.DrawLine(marcupPen, _placeSizeWidth * 2 + 125, 20 + i * _placeSizeHeight, _placeSizeWidth * 2 + 125, (i + 1) * _placeSizeHeight - 20);
|
||
}
|
||
|
||
for (int i = 0; i < _pictureHeight / 20; i++)
|
||
{
|
||
g.DrawLine(signalFirePen, _placeSizeWidth * 2 + 15, 20 + i * _placeSizeHeight / 2, _placeSizeWidth * 2 + 15, (i + 1) * _placeSizeHeight / 2 - 20);
|
||
g.DrawLine(signalFirePen, _placeSizeWidth * 3+20, 20 + i * _placeSizeHeight / 2, _placeSizeWidth * 3+20, (i + 1) * _placeSizeHeight / 2 - 20);
|
||
}
|
||
|
||
//отрисовка сочков
|
||
Brush pinkBrush = new SolidBrush(Color.LightPink);
|
||
for (int i = 1; i < 6; i++)
|
||
{
|
||
Point[] point = new Point[]
|
||
{
|
||
new Point((i * 70 - 10) + 45, 30),
|
||
new Point(i * 70 - 10, 50),
|
||
new Point( i * 70 - 10 , 10)
|
||
};
|
||
g.FillPolygon(pinkBrush, point);
|
||
|
||
g.DrawLine(pen, i * 70 - 10, 10, i * 70 - 10, 80);
|
||
g.DrawLine(pen, i * 70 - 10, 10, (i * 70 - 10) + 45, 30);
|
||
g.DrawLine(pen, i * 70 - 10, 50, (i * 70 - 10) + 45, 30);
|
||
|
||
}
|
||
}
|
||
|
||
|
||
//метод прорисовки объеков
|
||
public void DrawPlanes(Graphics g)
|
||
{
|
||
int width = _pictureWidth / _placeSizeWidth;
|
||
int height = _pictureHeight / _placeSizeHeight;
|
||
|
||
int position = 0;
|
||
int currentWidth = 1;
|
||
int currentHeight = 5;
|
||
foreach (var plane in _setPlanes.GetAirbus())
|
||
{
|
||
plane.SetObject(currentWidth * _placeSizeWidth + 20, currentHeight * _placeSizeHeight + 20, _pictureWidth, _pictureHeight);
|
||
plane.DrawningObject(g);
|
||
|
||
if (position % 2 == 0)
|
||
{
|
||
position++;
|
||
currentWidth--;
|
||
}
|
||
else
|
||
{
|
||
position = 0;
|
||
currentWidth = 1;
|
||
currentHeight--;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|