using ProjectBulldozer.Drawning; using ProjectBulldozer.MovementStrategy; namespace ProjectBulldozer.Generics { internal class TractorGenericCollection 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 _collection; public IEnumerable GetTractors => _collection.GetTractors(); public TractorGenericCollection(int picWidth, int picHeight) { 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 T? operator -(TractorGenericCollection 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); } } } }