2023-10-15 20:20:06 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AirBomber
|
|
|
|
|
{
|
2023-10-15 22:06:38 +04:00
|
|
|
|
internal class PlanesGenericCollection<T, U>
|
|
|
|
|
where T : DrawningAirPlane
|
|
|
|
|
where U : IMoveableObject
|
2023-10-15 20:20:06 +04:00
|
|
|
|
{
|
2023-10-15 22:06:38 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна прорисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна прорисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Размер занимаемого объектом места (ширина)
|
|
|
|
|
/// </summary>
|
2023-10-16 00:43:49 +04:00
|
|
|
|
private readonly int _placeSizeWidth = 170;
|
2023-10-15 22:06:38 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Размер занимаемого объектом места (высота)
|
|
|
|
|
/// </summary>
|
2023-10-16 00:43:49 +04:00
|
|
|
|
private readonly int _placeSizeHeight = 120;
|
2023-10-15 22:06:38 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly SetGeneric<T> _collection;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="picWidth"></param>
|
|
|
|
|
/// <param name="picHeight"></param>
|
|
|
|
|
public PlanesGenericCollection(int picWidth, int picHeight)
|
|
|
|
|
{
|
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_collection = new SetGeneric<T>(width * height);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перегрузка оператора сложения
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collect"></param>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-15 23:48:33 +04:00
|
|
|
|
public static int operator +(PlanesGenericCollection<T, U> collect, T? obj)
|
2023-10-15 22:06:38 +04:00
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
2023-10-16 11:36:12 +04:00
|
|
|
|
{
|
2023-10-15 23:48:33 +04:00
|
|
|
|
return -1;
|
2023-10-16 11:36:12 +04:00
|
|
|
|
}
|
2023-10-15 23:48:33 +04:00
|
|
|
|
return collect?._collection.Insert(obj) ?? -1;
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перегрузка оператора вычитания
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collect"></param>
|
|
|
|
|
/// <param name="pos"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-16 11:36:12 +04:00
|
|
|
|
public static bool operator -(PlanesGenericCollection<T, U> collect, int pos)
|
2023-10-15 22:06:38 +04:00
|
|
|
|
{
|
2023-10-22 14:05:39 +04:00
|
|
|
|
T? obj = collect._collection[pos];
|
2023-10-15 22:06:38 +04:00
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
collect._collection.Remove(pos);
|
2023-10-16 11:36:12 +04:00
|
|
|
|
return true;
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
2023-10-16 11:36:12 +04:00
|
|
|
|
return false;
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта IMoveableObject
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pos"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public U? GetU(int pos)
|
|
|
|
|
{
|
2023-10-22 14:05:39 +04:00
|
|
|
|
return (U?)_collection[pos]?.GetMoveableObject;
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вывод всего набора объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2023-10-15 23:48:33 +04:00
|
|
|
|
public Bitmap ShowPlanes()
|
2023-10-15 22:06:38 +04:00
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
Graphics gr = Graphics.FromImage(bmp);
|
|
|
|
|
DrawBackground(gr);
|
|
|
|
|
DrawObjects(gr);
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
/// <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++)
|
|
|
|
|
{
|
2023-10-29 22:25:43 +04:00
|
|
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
|
|
|
|
{
|
|
|
|
|
//линия рамзетки места
|
2023-10-15 22:06:38 +04:00
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, j *
|
2023-10-29 22:25:43 +04:00
|
|
|
|
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
2023-10-29 22:25:43 +04:00
|
|
|
|
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод прорисовки объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="g"></param>
|
|
|
|
|
private void DrawObjects(Graphics g)
|
|
|
|
|
{
|
2023-10-15 23:57:12 +04:00
|
|
|
|
int widthObjCount = _pictureWidth / _placeSizeWidth;
|
2023-10-29 22:25:43 +04:00
|
|
|
|
for (int i = 0; i < _collection.Count; i++)
|
2023-10-15 22:06:38 +04:00
|
|
|
|
{
|
2023-10-29 22:25:43 +04:00
|
|
|
|
T? type = _collection[i];
|
|
|
|
|
if (type != null)
|
2023-10-15 23:48:33 +04:00
|
|
|
|
{
|
2023-10-29 22:25:43 +04:00
|
|
|
|
int row = i / widthObjCount;
|
|
|
|
|
int col = widthObjCount - 1 - (i % widthObjCount);
|
2023-10-15 23:48:33 +04:00
|
|
|
|
|
2023-10-29 22:25:43 +04:00
|
|
|
|
type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight);
|
|
|
|
|
type?.DrawPlane(g);
|
2023-10-15 23:48:33 +04:00
|
|
|
|
}
|
2023-10-15 22:06:38 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-15 20:20:06 +04:00
|
|
|
|
}
|
|
|
|
|
}
|