184 lines
6.8 KiB
C#
184 lines
6.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AirBomber
|
||
{
|
||
internal class MapWithSetAirplanesGeneric<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 = 190;
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
public SetAirplanesGeneric<T> SetAirplanes { get; private set; }
|
||
/// <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="airplane"></param>
|
||
/// <returns>Возвращает позицию вставленого объекта либо -1, если не получилось его добавить</returns>
|
||
public static int operator +(MapWithSetAirplanesGeneric<T, U> map, T airplane)
|
||
{
|
||
return map.SetAirplanes.Insert(airplane);
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора вычитания
|
||
/// </summary>
|
||
/// <param name="map"></param>
|
||
/// <param name="position"></param>
|
||
/// <returns>Возвращает удаленный объект, либо null если его не удалось удалить</returns>
|
||
public static T operator -(MapWithSetAirplanesGeneric<T, U> map, int position)
|
||
{
|
||
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();
|
||
for (int i = 0; i < SetAirplanes.Count; i++)
|
||
{
|
||
var airplane = SetAirplanes[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 = SetAirplanes.Count - 1;
|
||
for (int i = 0; i < SetAirplanes.Count; i++)
|
||
{
|
||
if (SetAirplanes[i] == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var airplane = SetAirplanes[j];
|
||
if (airplane != null)
|
||
{
|
||
SetAirplanes.Insert(airplane, i);
|
||
SetAirplanes.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 = 0; j < _pictureHeight / _placeSizeHeight + 1; j++)
|
||
{
|
||
DrawHangar(g, pen, new RectangleF(i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth / 1.8F, _placeSizeHeight / 1.6F));
|
||
}
|
||
}
|
||
}
|
||
|
||
private void DrawHangar(Graphics g, Pen pen, RectangleF rect)
|
||
{
|
||
g.DrawLine(pen, rect.Left , rect.Top , rect.Right, rect.Top );
|
||
g.DrawLine(pen, rect.Right, rect.Top , rect.Right, rect.Bottom);
|
||
g.DrawLine(pen, rect.Right, rect.Bottom, rect.Left , rect.Bottom);
|
||
|
||
// Края ворот ангара
|
||
g.DrawLine(pen, rect.Left, rect.Top , rect.Left, rect.Top + rect.Height / 10);
|
||
g.DrawLine(pen, rect.Left, rect.Bottom, rect.Left, rect.Bottom - rect.Height / 10);
|
||
}
|
||
/// <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 < SetAirplanes.Count; i++)
|
||
{
|
||
var airplane = SetAirplanes[i];
|
||
airplane?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
|
||
airplane?.DrawObject(g);
|
||
}
|
||
}
|
||
}
|
||
}
|