146 lines
5.0 KiB
C#
146 lines
5.0 KiB
C#
using Cruiser.MovementStrategy;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Cruiser.Drawing;
|
||
|
||
namespace Cruiser.Generics
|
||
{
|
||
/// <summary>
|
||
/// Параметризованный класс для набора объектов DrawingCruiser
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="U"></typeparam>
|
||
internal class CarsGenericCollection<T, U>
|
||
where T : DrawingCruiser
|
||
where U : IMoveableObject
|
||
{
|
||
/// <summary>
|
||
/// Ширина окна прорисовки
|
||
/// </summary>
|
||
private readonly int _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна прорисовки
|
||
/// </summary>
|
||
private readonly int _pictureHeight;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (ширина)
|
||
/// </summary>
|
||
private readonly int _placeSizeWidth = 160;
|
||
/// <summary>
|
||
/// Размер занимаемого объектом места (высота)
|
||
/// </summary>
|
||
private readonly int _placeSizeHeight = 60;
|
||
/// <summary>
|
||
/// Набор объектов
|
||
/// </summary>
|
||
private readonly SetGeneric<T> _collection;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="picWidth"></param>
|
||
/// <param name="picHeight"></param>
|
||
public CarsGenericCollection(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>
|
||
public static bool operator +(CarsGenericCollection<T, U> collect, T? obj)
|
||
{
|
||
if (obj == null)
|
||
{
|
||
return false;
|
||
}
|
||
return collect._collection.Insert(obj);
|
||
}
|
||
/// <summary>
|
||
/// Перегрузка оператора вычитания
|
||
/// </summary>
|
||
/// <param name="collect"></param>
|
||
/// <param name="pos"></param>
|
||
/// <returns></returns>
|
||
public static T? operator -(CarsGenericCollection<T, U> collect, int pos)
|
||
{
|
||
T? obj = collect._collection[pos];
|
||
if (obj != null)
|
||
{
|
||
collect._collection.Remove(pos);
|
||
}
|
||
return obj;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта IMoveableObject
|
||
/// </summary>
|
||
/// <param name="pos"></param>
|
||
/// <returns></returns>
|
||
public U? GetU(int pos)
|
||
{
|
||
return (U?)_collection[pos]?.GetMoveableObject;
|
||
}
|
||
/// <summary>
|
||
/// Вывод всего набора объектов
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public Bitmap ShowCruiser()
|
||
{
|
||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawObjects(gr);
|
||
DrawBackground(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++)
|
||
{
|
||
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
|
||
{
|
||
g.DrawRectangle(pen, i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
|
||
}
|
||
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// /// Метод прорисовки объектов
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
private void DrawObjects(Graphics g)
|
||
{
|
||
int Ix = 0;
|
||
int Iy = 0;
|
||
int i = 0;
|
||
foreach (var cruiser in _collection.GetCruisers())
|
||
{
|
||
cruiser._pictureHeight = _pictureHeight;// добавил для починки отрисовки на форме коллекций
|
||
cruiser._pictureWidth = _pictureWidth;// добавил для починки отрисовки на форме коллекций
|
||
_collection[i]?.SetPosition(Ix, Iy);
|
||
_collection[i]?.DrawTransport(g);
|
||
Ix += _placeSizeWidth;
|
||
if (Ix + _placeSizeHeight > _pictureWidth)
|
||
{
|
||
Ix = 0;
|
||
Iy = _placeSizeHeight;
|
||
}
|
||
i++;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|