210 lines
8.3 KiB
C#
210 lines
8.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Airbus
|
||
{
|
||
internal class MapWithSetAirplaneGeneric<T, U>
|
||
where T : class, IDrawningObject, IEquatable<T>
|
||
where U : AbstractMap
|
||
{
|
||
/// <summary>
|
||
/// Ширина окна отрисовки
|
||
/// </summary>
|
||
private readonly int _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна отрисовки
|
||
/// </summary>
|
||
private readonly int _pictureHeight;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (ширина)
|
||
/// </summary>
|
||
private readonly int _placeSizeWidth = 300;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (высота)
|
||
/// </summary>
|
||
private readonly int _placeSizeHeight = 70;
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly SetAirplaneGeneric<T> _setAirplane;
|
||
/// <summary>
|
||
/// Карта
|
||
/// </summary>
|
||
private readonly U _map;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="picWidth"></param>
|
||
/// <param name="picHeight"></param>
|
||
/// <param name="map"></param>
|
||
public MapWithSetAirplaneGeneric(int picWidth, int picHeight, U map)
|
||
{
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_setAirplane = new SetAirplaneGeneric<T>(width * height);
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_map = map;
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора сложения
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="car"></param>
|
||
/// <returns></returns>
|
||
public static int operator +(MapWithSetAirplaneGeneric<T, U> map, T airplane)
|
||
{
|
||
return map._setAirplane.Insert(airplane);
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора вычитания
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public static T operator -(MapWithSetAirplaneGeneric<T, U> map, int
|
||
position)
|
||
{
|
||
return map._setAirplane.Remove(position);
|
||
}
|
||
/// <summary>
|
||
/// Вывод всего набора объектов
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowSet()
|
||
{
|
||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawBackground(gr);
|
||
DrawAirplanes(gr);
|
||
return bmp;
|
||
}
|
||
/// <summary>
|
||
/// Просмотр объекта на карте
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowOnMap()
|
||
{
|
||
Shaking();
|
||
for (int i = 0; i < _setAirplane.Count; i++)
|
||
{
|
||
var airplane = _setAirplane[i];
|
||
if (airplane != null)
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, airplane);
|
||
}
|
||
}
|
||
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 = _setAirplane.Count - 1;
|
||
for (int i = 0; i < _setAirplane.Count; i++)
|
||
{
|
||
if (_setAirplane[i] == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var airplane = _setAirplane[j];
|
||
if (airplane != null)
|
||
{
|
||
_setAirplane.Insert(airplane, i);
|
||
_setAirplane.Remove(j);
|
||
break;
|
||
}
|
||
}
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод отрисовки фона
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawBackground(Graphics g)
|
||
{
|
||
Pen pen = new(Color.Black, 3);
|
||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||
{
|
||
for (int j = 1; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||
{
|
||
pen = new(Color.Black, 3);
|
||
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 15, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight - 30);
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight - 30, i * _placeSizeWidth + _placeSizeWidth / 2 + 30, j * _placeSizeHeight - 30);
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 30, j * _placeSizeHeight - 30, i * _placeSizeWidth + _placeSizeWidth / 2 + 35, j * _placeSizeHeight );
|
||
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 15, j * _placeSizeHeight - 45, i * _placeSizeWidth + _placeSizeWidth / 2 + 35, j * _placeSizeHeight - 45);
|
||
pen = new(Color.Blue, 1);
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 30, j * _placeSizeHeight - 30, i * _placeSizeWidth + _placeSizeWidth / 2 + 45, j * _placeSizeHeight - 40);
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 45, j * _placeSizeHeight - 40, i * _placeSizeWidth + _placeSizeWidth / 2 + 35, j * _placeSizeHeight - 45);
|
||
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight - 30, i * _placeSizeWidth + _placeSizeWidth / 2 + 5, j * _placeSizeHeight - 40);
|
||
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth / 2 + 5, j * _placeSizeHeight - 40, i * _placeSizeWidth + _placeSizeWidth / 2 + 15, j * _placeSizeHeight - 45);
|
||
}
|
||
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Метод прорисовки объектов
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawAirplanes(Graphics g)
|
||
{
|
||
int countInLine = _pictureWidth / _placeSizeWidth;
|
||
int maxLeft = (countInLine - 1) * _placeSizeWidth;
|
||
for (int i = 0; i < _setAirplane.Count; i++)
|
||
{
|
||
_setAirplane[i]?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, (_pictureHeight - (i / 2 * _placeSizeHeight + _placeSizeHeight + 30)) + 25, _pictureWidth, _placeSizeHeight);
|
||
_setAirplane[i]?.DrawningObject(g);
|
||
}
|
||
}
|
||
public string GetData(char separatorType, char separatorData)
|
||
{
|
||
string data = $"{_map.GetType().Name}{separatorType}";
|
||
foreach (var airplane in _setAirplane.GetAirplanes())
|
||
{
|
||
data += $"{airplane.GetInfo()}{separatorData}";
|
||
}
|
||
return data;
|
||
}
|
||
/// <summary>
|
||
/// Загрузка списка из массива строк
|
||
/// </summary>
|
||
/// <param name="records"></param>
|
||
public void LoadData(string[] records)
|
||
{
|
||
foreach (var rec in records)
|
||
{
|
||
_setAirplane.Insert(DrawningObjectAirplane.Create(rec) as T);
|
||
}
|
||
}
|
||
public void Sort(IComparer<T> comparer)
|
||
{
|
||
_setAirplane.SortSet(comparer);
|
||
}
|
||
}
|
||
}
|