100 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ProjectBulldozer.MovementStrategy;
using ProjectBulldozer.Drawning;
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 = 170;
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);
}
/// Перегрузка оператора вычитания
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(TractorGenericCollection<T, U> collect, int
pos)
{
T obj = collect._collection.Get(pos);
if (obj != null)
{
return collect.Remove(pos);
}
return false;
}
private bool Remove(int pos)
{
throw new NotImplementedException();
}
// получение объекта imoveableObj
public U? GetU(int pos)
{
return (U?)_collection.Get(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 HeightObjCount = _pictureHeight / _placeSizeHeight;
int WidthObjCount = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
{
T t = _collection.Get(i);
if (t != null)
{
{
t.SetPosition(((_collection.Count - i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection.Count - i - 1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
t.DrawTransport(g);
}
}
}
}
}
}