From f97d2694ef97ffd5583ef7414229271205f06273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B0=D1=82=20=D0=97=D0=B0=D1=80=D0=B3?= =?UTF-8?q?=D0=B0=D1=80=D0=BE=D0=B2?= Date: Tue, 15 Nov 2022 09:13:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DoubleDeckerBus/AbstractMap.cs | 74 +++++++++++++++---- .../DoubleDeckerBus/DrawningBus.cs | 4 +- .../DrawningDoubleDeckerBus.cs | 19 ++++- .../DoubleDeckerBus/DrawningObjectBus.cs | 10 ++- .../DoubleDeckerBus/EntityDoubleDeckerBus.cs | 8 +- .../DoubleDeckerBus/FormMap.Designer.cs | 4 +- DoubleDeckerBus/DoubleDeckerBus/FormMap.cs | 17 ++--- DoubleDeckerBus/DoubleDeckerBus/MyMap.cs | 52 +++++++++++++ 8 files changed, 154 insertions(+), 34 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MyMap.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/AbstractMap.cs b/DoubleDeckerBus/DoubleDeckerBus/AbstractMap.cs index a1d54e2..4ce9f31 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/AbstractMap.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/AbstractMap.cs @@ -8,21 +8,22 @@ namespace DoubleDeckerBus { internal abstract class AbstractMap { - private IDrawningObject _drawningObject = null; + private IDrawningObject _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 Random _random = new Random(); protected readonly int _freeRoad = 0; protected readonly int _barrier = 1; - public Bitmap CreateMap(int width, int height, IDrawningObject - drawningObject) + + + public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject) { _width = width; _height = height; - _drawningObject = drawningObject; + _drawingObject = drawningObject; GenerateMap(); while (!SetObjectOnMap()) { @@ -32,26 +33,48 @@ namespace DoubleDeckerBus } public Bitmap MoveObject(Direction direction) { - _drawningObject.MoveObject(direction); + + _drawingObject.MoveObject(direction); + + bool collision = CheckCollision(); + + if (collision) + { + switch (direction) + { + case Direction.Left: + _drawingObject.MoveObject(Direction.Right); + break; + case Direction.Right: + _drawingObject.MoveObject(Direction.Left); + break; + case Direction.Up: + _drawingObject.MoveObject(Direction.Down); + break; + case Direction.Down: + _drawingObject.MoveObject(Direction.Up); + break; + } } + return DrawMapWithObject(); } private bool SetObjectOnMap() { - if (_drawningObject == null || _map == null) + if (_drawingObject == null || _map == null) { return false; } int x = _random.Next(0, 10); int y = _random.Next(0, 10); - _drawningObject.SetObject(x, y, _width, _height); - // TODO првоерка, что объект не "накладывается" на закрытые участки - return true; + _drawingObject.SetObject(x, y, _width, _height); + + return !CheckCollision(); } private Bitmap DrawMapWithObject() { Bitmap bmp = new(_width, _height); - if (_drawningObject == null || _map == null) + if (_drawingObject == null || _map == null) { return bmp; } @@ -70,12 +93,37 @@ namespace DoubleDeckerBus } } } - _drawningObject.DrawningObject(gr); + _drawingObject.DrawningObject(gr); return bmp; + + } + private bool CheckCollision() + { + var pos = _drawingObject.GetCurrentPosition(); + int startX = (int)((pos.Left) / _size_x); + int endX = (int)((pos.Right) / _size_x); + int startY = (int)((pos.Top) / _size_y); + int endY = (int)((pos.Bottom) / _size_y); + + if (startX < 0 || startY < 0 || endX > _map.GetLength(1) || endY > _map.GetLength(0)) { return false; } + + + + for (int y = startY; y < endY; y++) + { + for (int x = startX; x < endX; x++) + { + if (_map[x, y] == _barrier) + { + return true; + } + } + } + + return false; } 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/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs index 4b68c4b..5fbe44e 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs @@ -34,7 +34,7 @@ namespace DoubleDeckerBus /// /// Ширина отрисовки автобуса /// - private readonly int _busWidth = 112; + private readonly int _busWidth = 115; /// /// Высота отрисовки автобуса /// @@ -183,7 +183,7 @@ namespace DoubleDeckerBus /// public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { - return (_startPosX, _startPosY, _startPosX + _busWidth, _startPosY +_busHeight); + return (_startPosX, _startPosX + _busWidth, _startPosY, _startPosY + _busHeight); } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningDoubleDeckerBus.cs index 2f73043..bd7a924 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningDoubleDeckerBus.cs @@ -18,10 +18,10 @@ namespace DoubleDeckerBus /// Признак наличия обвеса /// Признак наличия второго этажа /// Признак наличия полосы - public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool line) : - base(speed, weight, bodyColor, 112, 50) + public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool stair) : + base(speed, weight, bodyColor, 115, 100) { - Bus = new EntityDoubleDeckerBus(speed, weight, bodyColor, dopColor, secondFloor, bodyKit, line); + Bus = new EntityDoubleDeckerBus(speed, weight, bodyColor, dopColor, secondFloor, bodyKit, stair); } public override void DrawTransport(Graphics g) { @@ -50,11 +50,22 @@ namespace DoubleDeckerBus _startPosY += 40; base.DrawTransport(g); _startPosY -= 40; - if (doubleDeckerBus.Line) + Brush brBlack = new SolidBrush(Color.Black); + if (doubleDeckerBus.Stair) { + g.FillRectangle(brBlack, _startPosX + 73, _startPosY + 57, 4, 5); + g.FillRectangle(brBlack, _startPosX + 76, _startPosY + 53, 3, 5); + g.FillRectangle(brBlack, _startPosX + 78, _startPosY + 49, 4, 5); + g.FillEllipse(brBlack, _startPosX + 73, _startPosY + 57, 8, 5); + g.FillEllipse(brBlack, _startPosX + 76, _startPosY + 53, 6, 5); + g.FillEllipse(brBlack, _startPosX + 78, _startPosY + 49, 4, 5); } + Brush brGray = new SolidBrush(Color.DarkGray); if (doubleDeckerBus.BodyKit) { + g.FillRectangle(brGray, _startPosX, _startPosY + 75, 14, 8); + g.FillRectangle(brGray, _startPosX + 32, _startPosY + 75, 53, 8); + g.FillRectangle(brGray, _startPosX + 102, _startPosY + 75, 14, 8); } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjectBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjectBus.cs index 51a06b5..906fba3 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjectBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjectBus.cs @@ -28,7 +28,15 @@ namespace DoubleDeckerBus } public void DrawningObject(Graphics g) { - _bus.DrawTransport(g); + if (_bus == null) return; + if (_bus is DrawningDoubleDeckerBus doubleDeckerBus) + { + doubleDeckerBus.DrawTransport(g); + } + else + { + _bus.DrawTransport(g); + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs index be33785..9a8c5cb 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs @@ -23,7 +23,7 @@ namespace DoubleDeckerBus /// /// Признак наличия гоночной полосы /// - public bool Line { get; private set; } + public bool Stair { get; private set; } /// /// Инициализация свойств /// @@ -33,14 +33,14 @@ namespace DoubleDeckerBus /// Дополнительный цвет /// Признак наличия второго этажа /// Признак наличия обвеса - /// Признак наличия полосы - public EntityDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool line) : + /// Признак наличия лестницы + public EntityDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool stair) : base(speed, weight, bodyColor) { DopColor = dopColor; SecondFloor = secondFloor; BodyKit = bodyKit; - Line = line; + Stair = stair; } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMap.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMap.Designer.cs index f7a0c1a..d2196bf 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMap.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMap.Designer.cs @@ -157,11 +157,13 @@ this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта"}); + "Простая карта", + "Моя карта"}); this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); this.comboBoxSelectorMap.TabIndex = 8; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // FormMap // diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMap.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMap.cs index b29287a..ea2921e 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMap.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMap.cs @@ -16,7 +16,7 @@ namespace DoubleDeckerBus public FormMap() { InitializeComponent(); - _abstractMap = new SimpleMap(); + _abstractMap = new MyMap(); } /// /// Заполнение информации по объекту @@ -75,14 +75,10 @@ namespace DoubleDeckerBus private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - var car = new DrawningDoubleDeckerBus(rnd.Next(100, 300), rnd.Next(1000, - 2000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, - 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, - 256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, - 2)), Convert.ToBoolean(rnd.Next(0, 2))); + var car = new DrawningDoubleDeckerBus(rnd.Next(100, 300), rnd.Next(1000,2000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)), + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(car); } /// @@ -97,6 +93,9 @@ namespace DoubleDeckerBus case "Простая карта": _abstractMap = new SimpleMap(); break; + case "Моя карта": + _abstractMap = new MyMap(); + break; } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MyMap.cs b/DoubleDeckerBus/DoubleDeckerBus/MyMap.cs new file mode 100644 index 0000000..b36ec20 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MyMap.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus +{ + internal class MyMap : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierColor = new SolidBrush(Color.Red); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.LightBlue); + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + 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++; + } + } + } + } +}