From 96b1b664a7b7ec8e773b1713acdc483e341a994f Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Sun, 16 Oct 2022 12:44:39 +0400 Subject: [PATCH] generic classes --- Airbus/Airbus/MapWithSetPlanesGeneric.cs | 173 +++++++++++++++++++++++ Airbus/Airbus/SetPlanesGeneric.cs | 77 ++++++++++ 2 files changed, 250 insertions(+) create mode 100644 Airbus/Airbus/MapWithSetPlanesGeneric.cs create mode 100644 Airbus/Airbus/SetPlanesGeneric.cs diff --git a/Airbus/Airbus/MapWithSetPlanesGeneric.cs b/Airbus/Airbus/MapWithSetPlanesGeneric.cs new file mode 100644 index 0000000..2cbbb46 --- /dev/null +++ b/Airbus/Airbus/MapWithSetPlanesGeneric.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + internal class MapWithSetPlanesGeneric + 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 SetPlanesGeneric _setPlanes; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setPlanes = new SetPlanesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(MapWithSetPlanesGeneric map, T planes) + { + return map._setPlanes.Insert(planes); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MapWithSetPlanesGeneric map, int position) + { + return map._setPlanes.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawPlanes(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setPlanes.Count; i++) + { + var plane = _setPlanes.Get(i); + if (plane != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, plane); + } + } + 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 = _setPlanes.Count - 1; + for (int i = 0; i < _setPlanes.Count; i++) + { + if (_setPlanes.Get(i) == null) + { + for (; j > i; j--) + { + var plane = _setPlanes.Get(j); + if (plane != null) + { + _setPlanes.Insert(plane, i); + _setPlanes.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + 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 / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, + (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawPlanes(Graphics g) + { + for (int i = 0; i < _setPlanes.Count; i++) + { + // TODO установка позиции + _setPlanes.Get(i)?.DrawningObject(g); + } + } + } + +} diff --git a/Airbus/Airbus/SetPlanesGeneric.cs b/Airbus/Airbus/SetPlanesGeneric.cs new file mode 100644 index 0000000..8e20371 --- /dev/null +++ b/Airbus/Airbus/SetPlanesGeneric.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + internal class SetPlanesGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetPlanesGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public bool Insert(T plane) + { + // TODO вставка в начало набора + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public bool Insert(T plane, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + _places[position] = plane; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присовив элементу массива значение null + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + // TODO проверка позиции + return _places[position]; + } + } + +}