diff --git a/AirBomber/AirBomber/MapWithSetJetsGeneric.cs b/AirBomber/AirBomber/MapWithSetJetsGeneric.cs new file mode 100644 index 0000000..05be60f --- /dev/null +++ b/AirBomber/AirBomber/MapWithSetJetsGeneric.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class MapWithSetJetsGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 122; + + /// + /// Набор объектов + /// + private readonly SetJetGeneric _setJets; + + /// + /// Карта + /// + private readonly U _map; + public U Map => _map; + /// + /// Конструктор + /// + /// Ширина + /// Высота + /// Карта + public MapWithSetJetsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setJets = new SetJetGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(MapWithSetJetsGeneric map, T jet) + { + return map._setJets.Insert(jet); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetJetsGeneric map, int position) + { + return map._setJets.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawJets(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setJets.Count; i++) + { + var car = _setJets.Get(i); + if (car != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, car); + } + } + 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 = _setJets.Count - 1; + for (int i = 0; i < _setJets.Count; i++) + { + if (_setJets.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setJets.Get(j); + if (car != null) + { + _setJets.Insert(car, i); + _setJets.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + for (int x = 0; x < _pictureWidth; x++) + { + for (int y = 0; y < _pictureWidth; y++) + { + Brush hangarColor = new SolidBrush(Color.Silver); + g.FillRectangle(hangarColor, x * x, y * y, x * (x + 1), y * (y + 1)); + } + } + 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 / 4, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawJets(Graphics g) + { + // максимальное количество колонок и строк + int cols = _pictureWidth / _placeSizeWidth; + //int rows = _pictureHeight / _placeSizeHeight; + int rows = (cols - 1) * _placeSizeWidth; + // счетчик колонок и строк + int ccol = 0; + int crow = 0; + + for (int i = 0; i < _setJets.Count; i++) + { + // установка позиции + //_setJets.Get(i)?.SetObject(ccol * _placeSizeWidth, crow * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setJets.Get(i)?.SetObject(rows - i % cols * _placeSizeWidth, i / cols * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); + _setJets.Get(i)?.DrawningObject(g); + // (сначала передвигаемся влево) + ccol++; + if (ccol >= cols) + { + //(потом двигаемся вниз) + crow++; + ccol = 0; + } + } + } + } +} diff --git a/AirBomber/AirBomber/SetJetGeneric.cs b/AirBomber/AirBomber/SetJetGeneric.cs new file mode 100644 index 0000000..4abaf03 --- /dev/null +++ b/AirBomber/AirBomber/SetJetGeneric.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class SetJetGeneric where T : class + { + /// + /// Массив объектов, которые храним (самолетов) + /// + private readonly T[] _places; + + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + private int JetPlaces = 0; + + /// + /// Конструктор + /// + /// Количество самолетов + public SetJetGeneric(int count) + { + _places = new T[count]; + } + + /// + /// Добавление самолета в набор на конкретную позицию + /// + /// Добавляемый самолет + /// Позиция + /// + public int Insert(T jet) + { + return Insert(jet, 0); + } + public int Insert(T jet, int position = 0) + { + if (position < 0 || position >= _places.Length || JetPlaces == _places.Length) + { + return -1; + } + JetPlaces++; + while (_places[position] != null) + { + for (int i = _places.Length - 1; i > 0; --i) + { + if (_places[i] == null && _places[i - 1] != null) + { + _places[i] = _places[i - 1]; + _places[i - 1] = null; + } + } + } + _places[position] = jet; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T Remove(int position) + { + if (position < 0 || position >= _places.Length) return null; + T savedJet = _places[position]; + _places[position] = null; + return savedJet; + } + + /// + /// Получение объекта из набора по позиции + /// + /// Позиция получаемого самолета + /// + public T Get(int position) + { + // проверка позиции + if (position < 0 || position >= _places.Length) return null; + return _places[position]; + } + } +}