2022-10-17 22:30:08 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-11-01 10:17:49 +04:00
|
|
|
|
using System.Drawing;
|
2022-10-17 22:30:08 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AirplaneWithRadar
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Карта с набром объектов под нее
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <typeparam name="U"></typeparam>
|
|
|
|
|
internal class MapWithSetAirplanesGeneric<T, U>
|
|
|
|
|
where T : class, IDrawingObject
|
|
|
|
|
where U : AbstractMap
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Размер занимаемого объектом места (ширина)
|
|
|
|
|
/// </summary>
|
2022-11-01 10:17:49 +04:00
|
|
|
|
private readonly int _placeSizeWidth = 314;
|
2022-10-17 22:30:08 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Размер занимаемого объектом места (высота)
|
|
|
|
|
/// </summary>
|
2022-11-01 10:17:49 +04:00
|
|
|
|
private readonly int _placeSizeHeight = 128;
|
2022-10-17 22:30:08 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly SetAirplanesGeneric<T> _setAirplanes;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Карта
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly U _map;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="picWidth"></param>
|
|
|
|
|
/// <param name="picHeight"></param>
|
|
|
|
|
/// <param name="map"></param>
|
|
|
|
|
public MapWithSetAirplanesGeneric(int picWidth, int picHeight, U map)
|
|
|
|
|
{
|
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
|
|
|
|
_setAirplanes = new SetAirplanesGeneric<T>(width * height);
|
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_map = map;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перегрузка оператора сложения
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="map"></param>
|
|
|
|
|
/// <param name="airplanes"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-01 10:17:49 +04:00
|
|
|
|
public static int operator +(MapWithSetAirplanesGeneric<T, U> map, T airplane)
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
|
|
|
|
return map._setAirplanes.Insert(airplane);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перегрузка оператора вычитания
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="map"></param>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-01 10:17:49 +04:00
|
|
|
|
public static T operator -(MapWithSetAirplanesGeneric<T, U> map, int position)
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
|
|
|
|
return map._setAirplanes.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();
|
2022-11-13 16:20:54 +04:00
|
|
|
|
foreach (var airplane in _setAirplanes.GetAirplanes())
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
2022-11-13 16:20:54 +04:00
|
|
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, airplane);
|
2022-10-17 22:30:08 +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);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Shaking()
|
|
|
|
|
{
|
|
|
|
|
int j = _setAirplanes.Count - 1;
|
|
|
|
|
for (int i = 0; i < _setAirplanes.Count; i++)
|
|
|
|
|
{
|
2022-11-13 16:20:54 +04:00
|
|
|
|
if (_setAirplanes[i] == null)
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
|
|
|
|
for (; j > i; j--)
|
|
|
|
|
{
|
2022-11-13 16:20:54 +04:00
|
|
|
|
var airplane = _setAirplanes[j];
|
2022-11-01 10:17:49 +04:00
|
|
|
|
if (airplane != null)
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
2022-11-01 10:17:49 +04:00
|
|
|
|
_setAirplanes.Insert(airplane, i);
|
2022-10-17 22:30:08 +04:00
|
|
|
|
_setAirplanes.Remove(j);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (j <= i)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод отрисовки фона
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="g"></param>
|
2022-11-01 10:17:49 +04:00
|
|
|
|
private void DrawHangar(Graphics g, int x, int y, int width, int height)
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
|
|
|
|
Pen pen = new(Color.Black, 3);
|
2022-11-01 10:17:49 +04:00
|
|
|
|
g.DrawLine(pen, x, y, x + width, y);
|
|
|
|
|
g.DrawLine(pen, x, y, x, y + height + 20);
|
|
|
|
|
g.DrawLine(pen, x, y + height + 20, x + width, y + height + 20);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод отрисовки фона
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="g"></param>
|
|
|
|
|
private void DrawBackground(Graphics g)
|
|
|
|
|
{
|
2022-11-13 09:50:31 +04:00
|
|
|
|
Pen pen = new(Color.White, 5);
|
|
|
|
|
g.FillRectangle(Brushes.DimGray, 0, 0, _pictureWidth, _pictureHeight);
|
2022-10-17 22:30:08 +04:00
|
|
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
|
|
|
|
{
|
2022-11-01 10:17:49 +04:00
|
|
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
|
|
|
|
|
{
|
2022-11-13 19:19:33 +04:00
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth * 4 / 5, j * _placeSizeHeight);
|
2022-10-17 22:30:08 +04:00
|
|
|
|
}
|
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод прорисовки объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="g"></param>
|
|
|
|
|
private void DrawAirplanes(Graphics g)
|
|
|
|
|
{
|
2022-11-13 19:19:33 +04:00
|
|
|
|
// TODO установка позиции
|
|
|
|
|
int numObjectsInRow = _pictureWidth / _placeSizeWidth;
|
|
|
|
|
int maxLeft = (numObjectsInRow - 1) * _placeSizeWidth;
|
|
|
|
|
for (int i = 0; i < _setAirplanes.Count; i++)
|
2022-10-17 22:30:08 +04:00
|
|
|
|
{
|
2022-11-13 19:19:33 +04:00
|
|
|
|
var airplane = _setAirplanes[i];
|
|
|
|
|
airplane?.SetObject(maxLeft - i % numObjectsInRow * _placeSizeWidth + 5, i / numObjectsInRow * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
|
|
|
|
|
airplane?.DrawingObject(g);
|
2022-10-17 22:30:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-13 19:19:33 +04:00
|
|
|
|
|
2022-10-17 22:30:08 +04:00
|
|
|
|
}
|
2022-11-13 19:19:33 +04:00
|
|
|
|
|