83 lines
2.9 KiB
C#
83 lines
2.9 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 IEnumerable<T?> GetTractors => _collection.GetTractors();
|
|
public TractorGenericCollection(int picWidth, int picHeight)
|
|
{
|
|
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;
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|