diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs new file mode 100644 index 0000000..3a2dada --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs @@ -0,0 +1,198 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus +{ + internal class MapWithSetBusesGeneric + 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 SetBusesGeneric _setBuses; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetBusesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBuses = new SetBusesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + /// + public static T operator -(MapWithSetBusesGeneric map, int position) + { + return map._setBuses.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawBuses(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setBuses.Count; i++) + { + var bus = _setBuses.Get(i); + if (bus != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, bus); + } + } + 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 = _setBuses.Count - 1; + for (int i = 0; i < _setBuses.Count; i++) + { + if (_setBuses.Get(i) == null) + { + for (; j > i; j--) + { + var bus = _setBuses.Get(j); + if (bus != null) + { + _setBuses.Insert(bus, i); + _setBuses.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + Brush pointColor = new SolidBrush(Color.Orange); + 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 + _placeSizeWidth / 2, j * _placeSizeHeight, + i * _placeSizeWidth + _placeSizeWidth / 2, (int)((j * _placeSizeHeight) - (40))); + + g.FillPolygon(pointColor, new Point[] { + new Point(i * _placeSizeWidth + _placeSizeWidth / 2, (int)((j * _placeSizeHeight) - (40))), + new Point(i * _placeSizeWidth + _placeSizeWidth / 2 - 10, (int)((j * _placeSizeHeight) - (50))), + new Point(i * _placeSizeWidth + _placeSizeWidth / 2, (int)((j * _placeSizeHeight) - (60))), + new Point(i * _placeSizeWidth + _placeSizeWidth / 2 + 10, (int)((j * _placeSizeHeight) - (50))) + }); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawBuses(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int currentWidth = width - 1; + int currentHeight = 0; + + for (int i = 0; i < _setBuses.Count; i++) + { + + _setBuses.Get(i)?.SetObject(currentWidth * _placeSizeWidth, + currentHeight * _placeSizeHeight, + _pictureWidth, _pictureHeight); + _setBuses.Get(i)?.DrawningObject(g); + + if (currentWidth > 0) + currentWidth--; + else + { + currentWidth = width - 1; + currentHeight++; + } + if (currentHeight > height) return; + } + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs new file mode 100644 index 0000000..ae130b0 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus +{ + internal class SetBusesGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + private int BusyPlaces = 0; + /// + /// Конструктор + /// + /// + public SetBusesGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автобус + /// + public int Insert(T bus) + { + return Insert(bus, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T bus, int position) + { + if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return -1; + + BusyPlaces++; + 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] = bus; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T Remove(int position) + { + if (position < 0 || position >= _places.Length) return null; + T savedBus = _places[position]; + _places[position] = null; + return savedBus; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.Length) return null; + return _places[position]; + } + + } +}