namespace ArmoredVehicle { /// /// Карта с набром объектов под нее /// /// /// internal class MapWithSetMachineGeneric where T : class, IDrawningObject where U : AbstractMap { /// /// Ширина окна отрисовки /// private readonly int _pictureWidth; /// /// Высота окна отрисовки /// private readonly int _pictureHeight; /// /// Размер занимаемого объектом места (ширина) /// private readonly int _placeSizeWidth = 210; /// /// Размер занимаемого объектом места (высота) /// private readonly int _placeSizeHeight = 90; /// /// Набор объектов /// private readonly SetMachineGeneric _setMachines; /// /// Карта /// private readonly U _map; /// /// Конструктор /// /// /// /// public MapWithSetMachineGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setMachines = new SetMachineGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; } /// /// Перегрузка оператора сложения /// /// /// /// public static int operator +(MapWithSetMachineGeneric map, T machine) { return map._setMachines.Insert(machine); } /// /// Перегрузка оператора вычитания /// /// /// /// public static T operator -(MapWithSetMachineGeneric map, int position) { return map._setMachines.Remove(position); } /// /// Вывод всего набора объектов /// /// public Bitmap ShowSet() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawMachine(gr); return bmp; } /// /// Просмотр объекта на карте /// /// public Bitmap ShowOnMap() { Shaking(); foreach (var machine in _setMachines.GetMachine()) { return _map.CreateMap(_pictureWidth, _pictureHeight, machine); } return new(_pictureWidth, _pictureHeight); } /// /// Перемещение объекта по крате /// /// /// public Bitmap MoveObject(Direction direction) { if (_map != null) { return _map.MoveObject(direction); } return new(_pictureWidth, _pictureHeight); } /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() { int j = _setMachines.Count - 1; for (int i = 0; i < _setMachines.Count; i++) { if (_setMachines[i] == null) { for (; j > i; j--) { var car = _setMachines[j]; if (car != null) { _setMachines.Insert(car, i); _setMachines.Remove(j); break; } } if (j <= i) { return; } } } } /// /// Метод отрисовки фона /// /// private void DrawBackground(Graphics g) { Pen pen = new(Color.LawnGreen, 5); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {//линия рамзетки места int dop = 0; if(i>0 && j>0) { dop = 30; } g.DrawRectangle(pen, new Rectangle(i * _placeSizeWidth + dop, j * _placeSizeHeight + dop, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight)); } } } /// /// Метод прорисовки объектов /// /// private void DrawMachine(Graphics g) { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; int currentWidth = 0; int currentHeight = 0; foreach (var machine in _setMachines.GetMachine()) { int dop = 0; if (currentWidth > 0 && currentHeight > 0) { dop = 30; } machine.SetObject(currentWidth * _placeSizeWidth + dop, currentHeight * _placeSizeHeight + dop, _pictureWidth, _pictureHeight); machine?.DrawningObject(g); if (currentWidth < width - 1) { currentWidth++; } else { currentWidth = 0; currentHeight++; } if (currentHeight > height) return; } } } }