98 lines
3.5 KiB
C#
98 lines
3.5 KiB
C#
using ProjectBulldozer.Drawning;
|
||
using ProjectBulldozer.MovementStrategy;
|
||
|
||
namespace ProjectBulldozer.Generics
|
||
{
|
||
internal class TractorGenericCollection<T, U> where T : DrawingTractor where U : IMoveableObject
|
||
{
|
||
//ширина/высота окна
|
||
private readonly int _pictureWidth;
|
||
private readonly int _pictureHeight;
|
||
//ширина/высота занимаемого места
|
||
private readonly int _placeSizeWidth = 150;
|
||
private readonly int _placeSizeHeight = 130;
|
||
/// Набор объектов
|
||
private readonly SetGeneric<T> _collection;
|
||
public TractorGenericCollection(int picWidth, int picHeight)
|
||
{
|
||
|
||
// высчитываем размер массива для setgeneric
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_collection = new SetGeneric<T>(width * height);
|
||
}
|
||
/// Перегрузка оператора сложения
|
||
public static int operator +(TractorGenericCollection<T, U> collect, T? tract)
|
||
{
|
||
if (tract == null)
|
||
{
|
||
return -1;
|
||
}
|
||
return collect._collection.Insert(tract);
|
||
}
|
||
/// Перегрузка оператора вычитания
|
||
public static T? operator -(TractorGenericCollection<T, U> collect, int pos)
|
||
{
|
||
T? obj = collect._collection[pos];
|
||
if (obj != null)
|
||
{
|
||
collect._collection.Remove(pos);
|
||
}
|
||
return obj;
|
||
}
|
||
// получение объекта imoveableObj
|
||
public U? GetU(int pos)
|
||
{
|
||
return (U?)_collection[pos]?.GetMoveableObject;
|
||
}
|
||
/// Вывод всего набора объектов
|
||
public Bitmap ShowTractors()
|
||
{
|
||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||
Graphics gr = Graphics.FromImage(bmp);
|
||
DrawBackground(gr);
|
||
DrawObjects(gr);
|
||
return bmp;
|
||
}
|
||
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)
|
||
{
|
||
//линия рамзетки места
|
||
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight);
|
||
}
|
||
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||
}
|
||
}
|
||
|
||
private void DrawObjects(Graphics g)
|
||
{
|
||
int width = _pictureWidth / _placeSizeWidth;
|
||
int height = _pictureHeight / _placeSizeHeight;
|
||
for (int i = 0; i < _collection.Count; i++)
|
||
{
|
||
// Получение объекта
|
||
var obj = _collection[i];
|
||
// Установка позиции
|
||
obj?.SetPosition(
|
||
(int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)),
|
||
(int)((height - 1) * _placeSizeHeight - (i / width * _placeSizeHeight))
|
||
);
|
||
// Прорисовка объекта
|
||
obj?.DrawTransport(g);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|