using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AirplaneWithRadar { /// /// Карта с набром объектов под нее /// /// /// internal class MapWithSetAirplanesGeneric where T : class, IDrawingObject where U : AbstractMap { /// /// Ширина окна отрисовки /// private readonly int _pictureWidth; /// /// Высота окна отрисовки /// private readonly int _pictureHeight; /// /// Размер занимаемого объектом места (ширина) /// private readonly int _placeSizeWidth = 314; /// /// Размер занимаемого объектом места (высота) /// private readonly int _placeSizeHeight = 128; /// /// Набор объектов /// private readonly SetAirplanesGeneric _setAirplanes; /// /// Карта /// private readonly U _map; /// /// Конструктор /// /// /// /// public MapWithSetAirplanesGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setAirplanes = new SetAirplanesGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; } /// /// Перегрузка оператора сложения /// /// /// /// public static int operator +(MapWithSetAirplanesGeneric map, T airplane) { return map._setAirplanes.Insert(airplane); } /// /// Перегрузка оператора вычитания /// /// /// /// public static T operator -(MapWithSetAirplanesGeneric map, int position) { return map._setAirplanes.Remove(position); } /// /// Вывод всего набора объектов /// /// public Bitmap ShowSet() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawAirplanes(gr); return bmp; } /// /// Просмотр объекта на карте /// /// public Bitmap ShowOnMap() { Shaking(); for (int i = 0; i < _setAirplanes.Count; i++) { var airplane = _setAirplanes.Get(i); if (airplane != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, airplane); } } 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 = _setAirplanes.Count - 1; for (int i = 0; i < _setAirplanes.Count; i++) { if (_setAirplanes.Get(i) == null) { for (; j > i; j--) { var airplane = _setAirplanes.Get(j); if (airplane != null) { _setAirplanes.Insert(airplane, i); _setAirplanes.Remove(j); break; } } if (j <= i) { return; } } } } /// /// Метод отрисовки фона /// /// private void DrawHangar(Graphics g, int x, int y, int width, int height) { Pen pen = new(Color.Black, 3); g.DrawLine(pen, x, y, x + width, y); g.DrawLine(pen, x, y, x, y + height + 20); g.DrawLine(pen, x, y + height + 20, x + width, y + height + 20); } /// /// Метод отрисовки фона /// /// private void DrawBackground(Graphics g) { Pen pen = new(Color.White, 3); g.FillRectangle(Brushes.Gray, 0, 0, _pictureWidth, _pictureHeight); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) { g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth * 3/4, j * _placeSizeHeight); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } /// /// Метод прорисовки объектов /// /// private void DrawAirplanes(Graphics g) { int numInRow = _pictureWidth / _placeSizeWidth; int maxLeft = (numInRow - 1) * _placeSizeWidth; for (int i = 0; i < _setAirplanes.Count; i++) { var airplane = _setAirplanes.Get(i); airplane?.SetObject(maxLeft - i % numInRow * _placeSizeWidth + 5, i / numInRow * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); airplane?.DrawingObject(g); } } } }