diff --git a/Monorail/Monorail/AbstractMap.cs b/Monorail/Monorail/AbstractMap.cs new file mode 100644 index 0000000..189be65 --- /dev/null +++ b/Monorail/Monorail/AbstractMap.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal abstract class AbstractMap + { + private IDrawingObject _drawingObject = null; + protected int[,] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected readonly Random _random = new(); + protected readonly int _freeRoad = 0; + protected readonly int _barrier = 1; + + public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject) + { + _width = width; + _height = height; + _drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public Bitmap MoveObject(Direction direction) + { + (float leftX, float topY, float rightX, float bottomY) = _drawingObject.GetCurrentPosition(); + + float locomotiveWidth = rightX - leftX; + float locomotiveHeight = bottomY - topY; + + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] == _barrier) + { + switch (direction) + { + case Direction.Up: + if (_size_y * (j + 1) >= topY - _drawingObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX + && _size_x * (i + 1) <= rightX) + { + return DrawMapWithObject(); + } + break; + case Direction.Down: + if (_size_y * j <= bottomY + _drawingObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX + && _size_x * (i + 1) <= rightX) + { + return DrawMapWithObject(); + } + break; + case Direction.Left: + if (_size_x * (i + 1) >= leftX - _drawingObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY + && _size_y * (j + 1) >= topY) + { + return DrawMapWithObject(); + } + break; + case Direction.Right: + if (_size_x * i <= rightX + _drawingObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY + && _size_y * (j + 1) >= topY) + { + return DrawMapWithObject(); + } + break; + } + } + } + } + if (true) + { + _drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private bool SetObjectOnMap() + { + (float leftX, float topY, float rightX, float bottomY) = _drawingObject.GetCurrentPosition(); + if (_drawingObject == null || _map == null) + { + return false; + } + + float locomotiveWidth = rightX - leftX; + float locomotiveHeight = bottomY - topY; + + int x = _random.Next(0, 10); + int y = _random.Next(0, 10); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (_map[i, j] == _barrier) + { + if (x + locomotiveWidth >= _size_x * i && x <= _size_x * i && y + locomotiveHeight > _size_y * j && y <= _size_y * j) + { + return false; + } + } + } + } + _drawingObject.SetObject(x, y, _width, _height); + return true; + } + private Bitmap DrawMapWithObject() + { + Bitmap bmp = new(_width, _height); + if (_drawingObject == null || _map == null) + { + return bmp; + } + Graphics gr = Graphics.FromImage(bmp); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (_map[i, j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i, j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawingObject.DrawingObject(gr); + return bmp; + } + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); + } +} diff --git a/Monorail/Monorail/DrawingLocomotive.cs b/Monorail/Monorail/DrawingLocomotive.cs index f6bc65d..148f349 100644 --- a/Monorail/Monorail/DrawingLocomotive.cs +++ b/Monorail/Monorail/DrawingLocomotive.cs @@ -8,7 +8,7 @@ namespace Monorail { internal class DrawingLocomotive { - public EntityLocomotive Locomotive { get; private set; } + public EntityLocomotive Locomotive { get; protected set; } private float _startPosX; private float _startPosY; private int? _pictureWidth = null; @@ -16,12 +16,6 @@ namespace Monorail private readonly int _locomotiveWidth = 80; private readonly int _locomotiveHeight = 50; - public void Init(int speed, float weight, Color bodyColor) - { - Locomotive = new EntityLocomotive(); - Locomotive.Init(speed, weight, bodyColor); - } - public void SetPosition(int x, int y, int width, int height) { //Сделать проверки (все параметры больше 0 и координаты не выходят за границы полей) @@ -99,7 +93,19 @@ namespace Monorail } - public void DrawTransport(Graphics g) + public DrawingLocomotive(int speed, float weight, Color bodyColor) + { + Locomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + protected DrawingLocomotive(int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) : + this(speed, weight, bodyColor) + { + _locomotiveWidth = locomotiveWidth; + _locomotiveHeight = locomotiveHeight; + } + + public virtual void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue) @@ -175,5 +181,10 @@ namespace Monorail } } + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() + { + return (_startPosX, _startPosY, _startPosX + _locomotiveWidth, _startPosY + _locomotiveHeight); + } + } } diff --git a/Monorail/Monorail/DrawingMonorailLocomotive.cs b/Monorail/Monorail/DrawingMonorailLocomotive.cs new file mode 100644 index 0000000..562ff49 --- /dev/null +++ b/Monorail/Monorail/DrawingMonorailLocomotive.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class DrawingMonorailLocomotive : DrawingLocomotive + { + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес автомобиля + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия магнитного рельса + /// Признак наличия второй кабины сзади + + public DrawingMonorailLocomotive(int speed, float weight, Color bodyColor, + Color dopColor, bool monorail, bool dopCabin) : + base(speed, weight, bodyColor, 110,60) + { + Locomotive = new EntityMonorailLocomotive(speed, weight, bodyColor, dopColor, monorail, dopCabin); + } + + public override void DrawTransport(Graphics g) + { + if (Locomotive is not EntityMonorailLocomotive monorailLocomotive) + { + return; + } + + Pen pen = new(Color.Black); + Brush dopBrush = new SolidBrush(monorailLocomotive.DopColor); + + base.DrawTransport(g); + + if (monorailLocomotive.DopCabin) + { + // задняя кабина + } + + if (monorailLocomotive.Monorail) + { + //монорельса + } + } + + } +} diff --git a/Monorail/Monorail/DrawingObjectLocomotive.cs b/Monorail/Monorail/DrawingObjectLocomotive.cs new file mode 100644 index 0000000..9784293 --- /dev/null +++ b/Monorail/Monorail/DrawingObjectLocomotive.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class DrawingObjectLocomotive : IDrawingObject + { + private DrawingLocomotive _locomotive = null; + + public DrawingObjectLocomotive(DrawingLocomotive locomotive) + { + _locomotive = locomotive; + } + + public float Step => _locomotive?.Locomotive?.Step ?? 0; + + public (float Left, float Right, float Top, float Bottom) + GetCurrentPosition() + { + return _locomotive?.GetCurrentPosition() ?? default; + } + + public void MoveObject(Direction direction) + { + _locomotive?.MoveTransport(direction); + } + public void SetObject(int x, int y, int width, int height) + { + _locomotive.SetPosition(x, y, width, height); + } + + void IDrawingObject.DrawingObject(Graphics g) + { + _locomotive.DrawTransport(g); + } + + } +} diff --git a/Monorail/Monorail/EntityLocomotive.cs b/Monorail/Monorail/EntityLocomotive.cs index 414fc7a..0ea9564 100644 --- a/Monorail/Monorail/EntityLocomotive.cs +++ b/Monorail/Monorail/EntityLocomotive.cs @@ -12,12 +12,14 @@ namespace Monorail public float Weight { get; private set; } public Color BodyColor { get; private set; } public float Step => Speed * 100 / Weight; - public void Init(int speed, float weight, Color bodyColor) + public EntityLocomotive(int speed, float weight, Color bodyColor) { Random rnd = new(); Speed = speed <= 0 ? rnd.Next(50, 150) : speed; Weight = weight <= 0 ? rnd.Next(40, 70) : weight; BodyColor = bodyColor; } + + } } diff --git a/Monorail/Monorail/EntityMonorailLocomotive.cs b/Monorail/Monorail/EntityMonorailLocomotive.cs new file mode 100644 index 0000000..694f98b --- /dev/null +++ b/Monorail/Monorail/EntityMonorailLocomotive.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class EntityMonorailLocomotive : EntityLocomotive + { + public Color DopColor { get; private set; } + public bool Monorail { get; private set; } + public bool DopCabin { get; private set; } + + public EntityMonorailLocomotive(int speed, float weight, Color bodyColor, + Color dopColor, bool monorail, bool dopCabin) : + base(speed,weight,bodyColor) + { + DopColor = dopColor; + Monorail = monorail; + DopCabin = dopCabin; + + } + + } +} diff --git a/Monorail/Monorail/IDrawingObject.cs b/Monorail/Monorail/IDrawingObject.cs new file mode 100644 index 0000000..2e7174f --- /dev/null +++ b/Monorail/Monorail/IDrawingObject.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal interface IDrawingObject + { + /// + /// Шаг перемещения объекта + /// + public float Step { get; } + /// + /// Установка позиции объекта + /// + /// Координата X + /// Координата Y + /// Ширина полотна + /// Высота полотна + void SetObject(int x, int y, int width, int height); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + /// + void MoveObject(Direction direction); + /// + /// Отрисовка объекта + /// + /// + void DrawingObject(Graphics g); + /// + /// Получение текущей позиции объекта + /// + /// + (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + + } +} diff --git a/Monorail/Monorail/SimpleMap.cs b/Monorail/Monorail/SimpleMap.cs new file mode 100644 index 0000000..c7657cc --- /dev/null +++ b/Monorail/Monorail/SimpleMap.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class SimpleMap : AbstractMap + { + + private readonly Brush barrierColor = new SolidBrush(Color.Black); + private readonly Brush roadColor = new SolidBrush(Color.Gray); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y)); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y)); + } + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +}