using ProjectBulldozer.MovementStrategy; using ProjectBulldozer.Drawning; namespace ProjectBulldozer.Generics { internal class TractorGenericCollection 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 _collection; public TractorGenericCollection(int picWidth, int picHeight) { // высчитываем размер массива для setgeneric int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } /// Перегрузка оператора сложения public static int operator +(TractorGenericCollection collect, T tract) { if (tract == null) { return -1; } return collect._collection.Insert(tract); } /// Перегрузка оператора вычитания /// /// Перегрузка оператора вычитания /// /// /// /// public static bool operator -(TractorGenericCollection 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); } } } } } }