using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectGasolineTanker.Drawings; using ProjectGasolineTanker.Generics; using ProjectGasolineTanker.MovementStratg; namespace ProjectGasolineTanker.Generic { internal class TruckGenericCollection where T : DrawingTruck where U : IMoveableObject { private readonly int _pictureWidth; private readonly int _pictureHeight; // Размер занимаемого места private readonly int _placeSizeWidth = 200; private readonly int _placeSizeHeight = 80; // коллекция private readonly SetGeneric _collection; public TruckGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } public void Sort(IComparer comparer) => _collection.SortSet(comparer); public static int operator +(TruckGenericCollection collect, T? obj) { if (obj != null) { return collect._collection.Insert(obj, new DrawingTruckEqutables()); } return -1; } public static bool operator -(TruckGenericCollection collect, int pos) { if (collect._collection.GetTruck(pos) == null) { return false; } return collect?._collection.Remove(pos) ?? false; } // получение объектов коллекции public IEnumerable GetTruck => _collection.GetTruck(); // получение объекта IMoveableObjecr public U? GetU(int pos) { return (U?)_collection[pos]?.GetMoveableObject; } // вывод всего набора public Bitmap ShowTruck() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawObjects(gr); return bmp; } // прорисовка фона private void DrawBackground(Graphics gr) { Pen pen = new(Color.Black, 3); for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { // линия разметки gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } } private void DrawObjects(Graphics g) { // координаты int x = 0; int y = _pictureHeight / _placeSizeHeight - 1; foreach (var truck in _collection.GetTruck()) { if (truck != null) { // TODO получение объекта if (x > _pictureWidth / _placeSizeWidth - 1) { x = 0; --y; } // TODO установка позиции truck.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); // TODO прорисовка объекта truck.DrawTransport(g); ++x; } } } } }