200 lines
7.1 KiB
C#
200 lines
7.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Airbus
|
||
{
|
||
internal class MapWithSetPlanesGeneric<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 SetPlanesGeneric<T> _setPlanes;
|
||
/// <summary>
|
||
/// Карта
|
||
/// </summary>
|
||
private readonly U _map;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="picWidth"></param>
|
||
/// <param name="picHeight"></param>
|
||
/// <param name="map"></param>
|
||
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;
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора сложения
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="planes"></param>
|
||
/// <returns></returns>
|
||
public static int operator +(MapWithSetPlanesGeneric<T, U> map, T planes)
|
||
{
|
||
return map._setPlanes.Insert(planes);
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора вычитания
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public static T operator -(MapWithSetPlanesGeneric<T, U> map, int position)
|
||
{
|
||
return map._setPlanes.Remove(position);
|
||
}
|
||
/// <summary>
|
||
/// Вывод всего набора объектов
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowSet()
|
||
{
|
||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawBackground(gr);
|
||
DrawPlanes(gr);
|
||
return bmp;
|
||
}
|
||
/// <summary>
|
||
/// Просмотр объекта на карте
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowOnMap()
|
||
{
|
||
Shaking();
|
||
for (int i = 0; i < _setPlanes.Count; i++)
|
||
{
|
||
var plane = _setPlanes.Get(i);
|
||
if (plane != null)
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, plane);
|
||
}
|
||
}
|
||
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 = _setPlanes.Count - 1;
|
||
for (int i = 0; i < _setPlanes.Count; i++)
|
||
{
|
||
if (_setPlanes.Get(i) == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var plane = _setPlanes.Get(j);
|
||
if (plane != null)
|
||
{
|
||
_setPlanes.Insert(plane, i);
|
||
_setPlanes.Remove(j);
|
||
break;
|
||
}
|
||
}
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод отрисовки фона
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawBackground(Graphics g)
|
||
{
|
||
Brush road = new SolidBrush(Color.Gray);
|
||
g.FillRectangle(road, 0, 0, _pictureWidth, _pictureHeight);
|
||
Brush lines = new SolidBrush(Color.White);
|
||
for (int y = 0; y < _pictureHeight; y+=80)
|
||
{
|
||
g.FillRectangle(lines, _pictureWidth / 2 - 10, y, 20, 70);
|
||
}
|
||
int lastLine = 0;
|
||
for (int y = 50; y < _pictureHeight; y += _pictureHeight/3 - 50)
|
||
{
|
||
g.FillRectangle(lines, _pictureWidth / 3 - 10, y, 20, 70);
|
||
g.FillRectangle(lines, _pictureWidth - _pictureWidth / 3 - 10, y, 20, 70);
|
||
lastLine = y;
|
||
}
|
||
g.FillRectangle(lines, _pictureWidth / 3 - 40, lastLine, 20, 70);
|
||
g.FillRectangle(lines, _pictureWidth - _pictureWidth / 3 + 20, lastLine, 20, 70);
|
||
|
||
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);
|
||
}
|
||
|
||
|
||
}
|
||
/// <summary>
|
||
/// Метод прорисовки объектов
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawPlanes(Graphics g)
|
||
{
|
||
int width = _pictureWidth / _placeSizeWidth;
|
||
int height = _pictureHeight / _placeSizeHeight;
|
||
|
||
for (int i = 0; i < _setPlanes.Count; i++)
|
||
{
|
||
// TODO установка позиции
|
||
if(_setPlanes.Get(i) != null)
|
||
{
|
||
_setPlanes.Get(i).SetObject((width - i % width - 1) * _placeSizeWidth,(height - i / width - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||
_setPlanes.Get(i)?.DrawningObject(g);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|