From 7bab8142b66368693a7d52ed57628452ef9a7373 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Fri, 1 Mar 2024 00:11:52 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DoubleDeckerBus/DoubleDeckerBus.csproj | 4 + .../{ => Drawnings}/DirectionType.cs | 3 +- .../DoubleDeckerBus/Drawnings/DrawingBus.cs | 203 ++++++++++++++++++ .../{ => Drawnings}/DrawingDoubleDeckerBus.cs | 14 +- .../DoubleDeckerBus/Entities/EntityBus.cs | 42 ++++ .../{ => Entities}/EntityDoubleDeckerBus.cs | 20 +- .../DoubleDeckerBus/FormDoubleDeckerBus.cs | 1 + 7 files changed, 263 insertions(+), 24 deletions(-) rename DoubleDeckerBus/DoubleDeckerBus/{ => Drawnings}/DirectionType.cs (88%) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs rename DoubleDeckerBus/DoubleDeckerBus/{ => Drawnings}/DrawingDoubleDeckerBus.cs (97%) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs rename DoubleDeckerBus/DoubleDeckerBus/{ => Entities}/EntityDoubleDeckerBus.cs (65%) diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj index 244387d..b2208b1 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj @@ -23,4 +23,8 @@ + + + + \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/DirectionType.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs similarity index 88% rename from DoubleDeckerBus/DoubleDeckerBus/DirectionType.cs rename to DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs index a05d391..abde908 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DirectionType.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs @@ -1,5 +1,4 @@ - -namespace DoubleDeckerBus; +namespace DoubleDeckerBus.Drawnings; public enum DirectionType { /// diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs new file mode 100644 index 0000000..b20aaeb --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -0,0 +1,203 @@ +using DoubleDeckerBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Drawnings; + +public class DrawingBus +{ + public EntityBus? EntityBus { get; private set; } + + private int? _pictureWidth; + + private int? _pictureHeight; + + private int? _startPosX; + + private int? _startPosY; + + private readonly int _DrawingBusWidth = 120; + + private readonly int _DrawingBusHight = 60; + + public void Init(int speed, double weight, Color bodyColor) + { + EntityBus = new EntityBus(speed, weight, bodyColor); + EntityBus.Init(speed, weight, bodyColor); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + /// + /// размер окна + /// + /// + /// + /// + public bool SetPictureSize(int width, int hight) + { + if (_DrawingBusWidth > width || _DrawingBusHight > hight) + { + return false; + } + + + _pictureWidth = width; + _pictureHeight = hight; + + if (_startPosX.HasValue && _startPosX.Value + _DrawingBusWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _DrawingBusWidth; + } + + if (_startPosY.HasValue && _startPosY + _DrawingBusHight > _pictureHeight) + { + _startPosY = _pictureHeight - _DrawingBusHight; + } + + return true; + + } + /// + /// установить начальную позицию + /// + /// + /// + public void SetPosition(int x, int y) + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + + if (x < 0) + { + x = -x; + } + if (y < 0) + { + y = -y; + } + + if (x + _DrawingBusWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _DrawingBusWidth; + } + else + { + _startPosX = x; + } + + if (y + _DrawingBusHight > _pictureHeight) + { + _startPosY = _pictureHeight - _DrawingBusHight; + } + else + { + _startPosY = y; + } + } + + public bool MoveTransport(DirectionType direction) + { + if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return false; + } + + switch (direction) + { + case DirectionType.Left: + if (_startPosX.Value - EntityBus.Step > 0) + { + _startPosX -= (int)EntityBus.Step; + } + return true; + + case DirectionType.Up: + if (_startPosY.Value - EntityBus.Step > 0) + { + _startPosY -= (int)EntityBus.Step; + } + return true; + case DirectionType.Right: + if (_startPosX.Value + _DrawingBusWidth + EntityBus.Step < _pictureWidth) + { + _startPosX += (int)EntityBus.Step; + } + return true; + case DirectionType.Down: + if (_startPosY.Value + _DrawingBusHight + EntityBus.Step < _pictureHeight) + { + _startPosY += (int)EntityBus.Step; + } + return true; + default: + return false; + + } + } + + public void DrawTrasnport(Graphics g) + { + if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush mainBrush = new SolidBrush(EntityBus.BodyColor); + Brush blueBr = new SolidBrush(Color.LightBlue); + + + //кузов 1го этажа + PointF[] bus = { new PointF((float)_startPosX + 5, (float)_startPosY + 20), + new PointF((float)_startPosX, (float)_startPosY + 45), + new PointF((float)_startPosX + 100, (float)_startPosY + 45), + new PointF((float)_startPosX + 100, (float)_startPosY + 25), + new PointF((float)_startPosX + 97, (float)_startPosY + 20) }; + g.FillPolygon(mainBrush, bus); + g.DrawPolygon(pen, bus); + + //окна 1ый этаж + g.FillRectangle(blueBr, _startPosX.Value + 2, _startPosY.Value + 25, 12, 10); + g.FillRectangle(blueBr, _startPosX.Value + 16, _startPosY.Value + 25, 12, 10); + g.FillRectangle(blueBr, _startPosX.Value + 42, _startPosY.Value + 25, 6, 10); + g.FillRectangle(blueBr, _startPosX.Value + 50, _startPosY.Value + 25, 13, 10); + g.FillRectangle(blueBr, _startPosX.Value + 66, _startPosY.Value + 25, 14, 10); + + g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 25, 12, 10); + g.DrawRectangle(pen, _startPosX.Value + 16, _startPosY.Value + 25, 12, 10); + g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value + 25, 6, 10); + g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 25, 13, 10); + g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 25, 14, 10); + + //переднее окно первый этаж + PointF[] window2 = { new PointF((float)_startPosX + 85, (float)_startPosY + 25), + new PointF((float)_startPosX + 100, (float)_startPosY + 25), + new PointF((float)_startPosX + 100, (float)_startPosY + 40), + new PointF((float)_startPosX + 85, (float)_startPosY + 35) }; + g.FillPolygon(blueBr, window2); + g.DrawPolygon(pen, window2); + + //дверь + Brush brownBr = new SolidBrush(Color.Brown); + g.FillRectangle(blueBr, _startPosX.Value + 30, _startPosY.Value + 25, 10, 15); + g.FillRectangle(brownBr, _startPosX.Value + 30, _startPosY.Value + 40, 10, 5); + + g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 25, 10, 15); + g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 40, 10, 5); + + //колёса + g.FillEllipse(brownBr, _startPosX.Value + 6, _startPosY.Value + 37, 16, 16); + g.FillEllipse(brownBr, _startPosX.Value + 78, _startPosY.Value + 37, 16, 16); + + g.DrawEllipse(pen, _startPosX.Value + 6, _startPosY.Value + 37, 16, 16); + g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 37, 16, 16); + + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs similarity index 97% rename from DoubleDeckerBus/DoubleDeckerBus/DrawingDoubleDeckerBus.cs rename to DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index 6d8f6ae..533472a 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -1,4 +1,6 @@ -namespace DoubleDeckerBus; +using DoubleDeckerBus.Entities; + +namespace DoubleDeckerBus.Drawnings; /// /// /// @@ -18,7 +20,7 @@ public class DrawingDoubleDeckerBus private readonly int _DrawingBusHight = 60; - public void Init(int speed, double weight, Color bodyColor, Color additionalColor,bool secondFloor, bool stripes) + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) { EntityDoubleDeckerBus = new EntityDoubleDeckerBus(); EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, secondFloor, stripes); @@ -44,12 +46,12 @@ public class DrawingDoubleDeckerBus _pictureWidth = width; _pictureHeight = hight; - if (_startPosX.HasValue && (_startPosX.Value + _DrawingBusWidth > _pictureWidth)) + if (_startPosX.HasValue && _startPosX.Value + _DrawingBusWidth > _pictureWidth) { _startPosX = _pictureWidth - _DrawingBusWidth; } - if (_startPosY.HasValue && (_startPosY + _DrawingBusHight > _pictureHeight)) + if (_startPosY.HasValue && _startPosY + _DrawingBusHight > _pictureHeight) { _startPosY = _pictureHeight - _DrawingBusHight; } @@ -149,7 +151,7 @@ public class DrawingDoubleDeckerBus Brush mainBrush = new SolidBrush(EntityDoubleDeckerBus.BodyColor); Brush blueBr = new SolidBrush(Color.LightBlue); - + //кузов 1го этажа PointF[] bus = { new PointF((float)_startPosX + 5, (float)_startPosY + 20), new PointF((float)_startPosX + 5, (float)_startPosY + 45), @@ -167,7 +169,7 @@ public class DrawingDoubleDeckerBus g.FillPolygon(additionalBrush, stripe); } - + //окна 1ый этаж g.FillRectangle(blueBr, _startPosX.Value + 7, _startPosY.Value + 25, 12, 10); g.FillRectangle(blueBr, _startPosX.Value + 21, _startPosY.Value + 25, 12, 10); diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs new file mode 100644 index 0000000..3ab2f79 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Entities; +/// +/// Класс сущность автобус +/// +public class EntityBus +{ + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public double Weight { get; private set; } + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + /// + /// Расстояние перемещения за раз + /// + public double Step => Speed * 100 / Weight; + /// + /// Конструктор базовой сущности + /// + /// + /// + /// + public EntityBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs similarity index 65% rename from DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs rename to DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs index bdfa702..ebdf755 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs @@ -1,16 +1,7 @@ -namespace DoubleDeckerBus; - public class EntityDoubleDeckerBus +namespace DoubleDeckerBus.Entities; +public class EntityDoubleDeckerBus { - public int Speed { get; private set; } - - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// + public Color AdditionalColor { get; private set; } /// /// Признак (опция) наличия второго этажа @@ -20,10 +11,7 @@ /// Признак (опция) наличия полосок на автобусе /// public bool Stripes { get; private set; } - /// - /// Шаг перемещения автобуса - /// - public double Step => Speed * 100 / Weight; + /// /// /// diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs index e44a6c5..bcb3a94 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using DoubleDeckerBus.Drawnings; namespace DoubleDeckerBus { -- 2.25.1 From d9f60fed993749966dd14467b3ae51f3e50971ec Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Sun, 3 Mar 2024 10:29:46 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B1=D0=B0=D0=B7=D0=B7=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BE=D1=87=D0=B5=D1=80=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DoubleDeckerBus/DoubleDeckerBus.csproj | 4 - .../DoubleDeckerBus/Drawnings/DrawingBus.cs | 101 +++++---- .../Drawnings/DrawingDoubleDeckerBus.cs | 196 ++---------------- .../Entities/EntityDoubleDeckerBus.cs | 14 +- .../MovementStrategy/IMoveableObject..cs | 12 ++ 5 files changed, 86 insertions(+), 241 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj index b2208b1..244387d 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj @@ -23,8 +23,4 @@ - - - - \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs index b20aaeb..9ce608e 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -9,29 +9,38 @@ namespace DoubleDeckerBus.Drawnings; public class DrawingBus { - public EntityBus? EntityBus { get; private set; } + public EntityBus? EntityBus { get; protected set; } private int? _pictureWidth; private int? _pictureHeight; - private int? _startPosX; + protected int? _startPosX; - private int? _startPosY; + protected int? _startPosY; - private readonly int _DrawingBusWidth = 120; + private readonly int _drawingBusWidth = 105; - private readonly int _DrawingBusHight = 60; + private readonly int _drawingBusHeight = 50; - public void Init(int speed, double weight, Color bodyColor) + private DrawingBus() { - EntityBus = new EntityBus(speed, weight, bodyColor); - EntityBus.Init(speed, weight, bodyColor); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + + public DrawingBus(int speed, int weight, Color bodyColor) : this() + { + EntityBus = new EntityBus(speed, weight, bodyColor); + } + + protected DrawingBus(int drawingBusWidth, int drawingBusHeight) : this() + { + _drawingBusWidth = drawingBusWidth; + _drawingBusHeight = drawingBusHeight; + } /// /// размер окна /// @@ -40,7 +49,7 @@ public class DrawingBus /// public bool SetPictureSize(int width, int hight) { - if (_DrawingBusWidth > width || _DrawingBusHight > hight) + if (_drawingBusWidth > width || _drawingBusHeight > hight) { return false; } @@ -49,14 +58,14 @@ public class DrawingBus _pictureWidth = width; _pictureHeight = hight; - if (_startPosX.HasValue && _startPosX.Value + _DrawingBusWidth > _pictureWidth) + if (_startPosX.HasValue && _startPosX.Value + _drawingBusWidth > _pictureWidth) { - _startPosX = _pictureWidth - _DrawingBusWidth; + _startPosX = _pictureWidth - _drawingBusWidth; } - if (_startPosY.HasValue && _startPosY + _DrawingBusHight > _pictureHeight) + if (_startPosY.HasValue && _startPosY + _drawingBusHeight > _pictureHeight) { - _startPosY = _pictureHeight - _DrawingBusHight; + _startPosY = _pictureHeight - _drawingBusHeight; } return true; @@ -83,18 +92,18 @@ public class DrawingBus y = -y; } - if (x + _DrawingBusWidth > _pictureWidth) + if (x + _drawingBusWidth > _pictureWidth) { - _startPosX = _pictureWidth - _DrawingBusWidth; + _startPosX = _pictureWidth - _drawingBusWidth; } else { _startPosX = x; } - if (y + _DrawingBusHight > _pictureHeight) + if (y + _drawingBusHeight > _pictureHeight) { - _startPosY = _pictureHeight - _DrawingBusHight; + _startPosY = _pictureHeight - _drawingBusHeight; } else { @@ -125,13 +134,13 @@ public class DrawingBus } return true; case DirectionType.Right: - if (_startPosX.Value + _DrawingBusWidth + EntityBus.Step < _pictureWidth) + if (_startPosX.Value + _drawingBusWidth + EntityBus.Step < _pictureWidth) { _startPosX += (int)EntityBus.Step; } return true; case DirectionType.Down: - if (_startPosY.Value + _DrawingBusHight + EntityBus.Step < _pictureHeight) + if (_startPosY.Value + _drawingBusHeight + EntityBus.Step < _pictureHeight) { _startPosY += (int)EntityBus.Step; } @@ -142,7 +151,7 @@ public class DrawingBus } } - public void DrawTrasnport(Graphics g) + public virtual void DrawTrasnport(Graphics g) { if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) { @@ -155,49 +164,49 @@ public class DrawingBus //кузов 1го этажа - PointF[] bus = { new PointF((float)_startPosX + 5, (float)_startPosY + 20), - new PointF((float)_startPosX, (float)_startPosY + 45), - new PointF((float)_startPosX + 100, (float)_startPosY + 45), + PointF[] bus = { new PointF((float)_startPosX + 5, (float)_startPosY), + new PointF((float)_startPosX, (float)_startPosY + 25), new PointF((float)_startPosX + 100, (float)_startPosY + 25), - new PointF((float)_startPosX + 97, (float)_startPosY + 20) }; + new PointF((float)_startPosX + 100, (float)_startPosY + 5), + new PointF((float)_startPosX + 97, (float)_startPosY) }; g.FillPolygon(mainBrush, bus); g.DrawPolygon(pen, bus); //окна 1ый этаж - g.FillRectangle(blueBr, _startPosX.Value + 2, _startPosY.Value + 25, 12, 10); - g.FillRectangle(blueBr, _startPosX.Value + 16, _startPosY.Value + 25, 12, 10); - g.FillRectangle(blueBr, _startPosX.Value + 42, _startPosY.Value + 25, 6, 10); - g.FillRectangle(blueBr, _startPosX.Value + 50, _startPosY.Value + 25, 13, 10); - g.FillRectangle(blueBr, _startPosX.Value + 66, _startPosY.Value + 25, 14, 10); + g.FillRectangle(blueBr, _startPosX.Value + 2, _startPosY.Value + 5, 12, 10); + g.FillRectangle(blueBr, _startPosX.Value + 16, _startPosY.Value + 5, 12, 10); + g.FillRectangle(blueBr, _startPosX.Value + 42, _startPosY.Value + 5, 6, 10); + g.FillRectangle(blueBr, _startPosX.Value + 50, _startPosY.Value + 5, 13, 10); + g.FillRectangle(blueBr, _startPosX.Value + 66, _startPosY.Value + 5, 14, 10); - g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 25, 12, 10); - g.DrawRectangle(pen, _startPosX.Value + 16, _startPosY.Value + 25, 12, 10); - g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value + 25, 6, 10); - g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 25, 13, 10); - g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 25, 14, 10); + g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 5, 12, 10); + g.DrawRectangle(pen, _startPosX.Value + 16, _startPosY.Value + 5, 12, 10); + g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value + 5, 6, 10); + g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 5, 13, 10); + g.DrawRectangle(pen, _startPosX.Value + 66, _startPosY.Value + 5, 14, 10); //переднее окно первый этаж - PointF[] window2 = { new PointF((float)_startPosX + 85, (float)_startPosY + 25), - new PointF((float)_startPosX + 100, (float)_startPosY + 25), - new PointF((float)_startPosX + 100, (float)_startPosY + 40), - new PointF((float)_startPosX + 85, (float)_startPosY + 35) }; + PointF[] window2 = { new PointF((float)_startPosX + 85, (float)_startPosY + 5), + new PointF((float)_startPosX + 100, (float)_startPosY + 5), + new PointF((float)_startPosX + 100, (float)_startPosY + 20), + new PointF((float)_startPosX + 85, (float)_startPosY + 15) }; g.FillPolygon(blueBr, window2); g.DrawPolygon(pen, window2); //дверь Brush brownBr = new SolidBrush(Color.Brown); - g.FillRectangle(blueBr, _startPosX.Value + 30, _startPosY.Value + 25, 10, 15); - g.FillRectangle(brownBr, _startPosX.Value + 30, _startPosY.Value + 40, 10, 5); + g.FillRectangle(blueBr, _startPosX.Value + 30, _startPosY.Value + 5, 10, 15); + g.FillRectangle(brownBr, _startPosX.Value + 30, _startPosY.Value + 20, 10, 5); - g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 25, 10, 15); - g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 40, 10, 5); + g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 5, 10, 15); + g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 20, 10, 5); //колёса - g.FillEllipse(brownBr, _startPosX.Value + 6, _startPosY.Value + 37, 16, 16); - g.FillEllipse(brownBr, _startPosX.Value + 78, _startPosY.Value + 37, 16, 16); + g.FillEllipse(brownBr, _startPosX.Value + 6, _startPosY.Value + 17, 16, 16); + g.FillEllipse(brownBr, _startPosX.Value + 78, _startPosY.Value + 17, 16, 16); - g.DrawEllipse(pen, _startPosX.Value + 6, _startPosY.Value + 37, 16, 16); - g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 37, 16, 16); + g.DrawEllipse(pen, _startPosX.Value + 6, _startPosY.Value + 17, 16, 16); + g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 17, 16, 16); } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index 533472a..f13bae5 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -4,165 +4,33 @@ namespace DoubleDeckerBus.Drawnings; /// /// /// -public class DrawingDoubleDeckerBus +public class DrawingDoubleDeckerBus : DrawingBus { - public EntityDoubleDeckerBus? EntityDoubleDeckerBus { get; private set; } - - private int? _pictureWidth; - - private int? _pictureHeight; - - private int? _startPosX; - - private int? _startPosY; - - private readonly int _DrawingBusWidth = 120; - - private readonly int _DrawingBusHight = 60; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) + public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (105,50) { - EntityDoubleDeckerBus = new EntityDoubleDeckerBus(); - EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, secondFloor, stripes); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - } - /// - /// размер окна - /// - /// - /// - /// - public bool SetPictureSize(int width, int hight) - { - if (_DrawingBusWidth > width || _DrawingBusHight > hight) - { - return false; - } - - - _pictureWidth = width; - _pictureHeight = hight; - - if (_startPosX.HasValue && _startPosX.Value + _DrawingBusWidth > _pictureWidth) - { - _startPosX = _pictureWidth - _DrawingBusWidth; - } - - if (_startPosY.HasValue && _startPosY + _DrawingBusHight > _pictureHeight) - { - _startPosY = _pictureHeight - _DrawingBusHight; - } - - return true; - - } - /// - /// установить начальную позицию - /// - /// - /// - public void SetPosition(int x, int y) - { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) - { - return; - } - - if (x < 0) - { - x = -x; - } - if (y < 0) - { - y = -y; - } - - if (x + _DrawingBusWidth > _pictureWidth) - { - _startPosX = _pictureWidth - _DrawingBusWidth; - } - else - { - _startPosX = x; - } - - if (y + _DrawingBusHight > _pictureHeight) - { - _startPosY = _pictureHeight - _DrawingBusHight; - } - else - { - _startPosY = y; - } + EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); } - public bool MoveTransport(DirectionType direction) + public override void DrawTrasnport(Graphics g) { - if (EntityDoubleDeckerBus == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return false; - } - - switch (direction) - { - case DirectionType.Left: - if (_startPosX.Value - EntityDoubleDeckerBus.Step > 0) - { - _startPosX -= (int)EntityDoubleDeckerBus.Step; - } - return true; - - case DirectionType.Up: - if (_startPosY.Value - EntityDoubleDeckerBus.Step > 0) - { - _startPosY -= (int)EntityDoubleDeckerBus.Step; - } - return true; - case DirectionType.Right: - if (_startPosX.Value + _DrawingBusWidth + EntityDoubleDeckerBus.Step < _pictureWidth) - { - _startPosX += (int)EntityDoubleDeckerBus.Step; - } - return true; - case DirectionType.Down: - if (_startPosY.Value + _DrawingBusHight + EntityDoubleDeckerBus.Step < _pictureHeight) - { - _startPosY += (int)EntityDoubleDeckerBus.Step; - } - return true; - default: - return false; - - } - } - - public void DrawTrasnport(Graphics g) - { - if (EntityDoubleDeckerBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityBus == null || EntityBus is not EntityDoubleDeckerBus doubleDeckerBus || !_startPosX.HasValue || !_startPosY.HasValue ) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityDoubleDeckerBus.AdditionalColor); - Brush mainBrush = new SolidBrush(EntityDoubleDeckerBus.BodyColor); + Brush additionalBrush = new SolidBrush(doubleDeckerBus.AdditionalColor); + Brush mainBrush = new SolidBrush(doubleDeckerBus.BodyColor); Brush blueBr = new SolidBrush(Color.LightBlue); - - //кузов 1го этажа - PointF[] bus = { new PointF((float)_startPosX + 5, (float)_startPosY + 20), - new PointF((float)_startPosX + 5, (float)_startPosY + 45), - new PointF((float)_startPosX + 105, (float)_startPosY + 45), - new PointF((float)_startPosX + 105, (float)_startPosY + 25), - new PointF((float)_startPosX + 102, (float)_startPosY + 20) }; - g.FillPolygon(mainBrush, bus); - g.DrawPolygon(pen, bus); + _startPosX += 5; + _startPosY += 20; + base.DrawTrasnport(g); + _startPosX -= 5; + _startPosY -= 20; //полоски - if (EntityDoubleDeckerBus.Stripes) + if (doubleDeckerBus.Stripes) { g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value + 39, 100, 3); PointF[] stripe = { new PointF((float)_startPosX + 5, (float)_startPosY + 22), new PointF((float)_startPosX + 5, (float)_startPosY + 25), new PointF((float)_startPosX + 104, (float)_startPosY + 25), new PointF((float)_startPosX + 103, (float)_startPosY + 22) }; @@ -170,44 +38,8 @@ public class DrawingDoubleDeckerBus } - //окна 1ый этаж - g.FillRectangle(blueBr, _startPosX.Value + 7, _startPosY.Value + 25, 12, 10); - g.FillRectangle(blueBr, _startPosX.Value + 21, _startPosY.Value + 25, 12, 10); - g.FillRectangle(blueBr, _startPosX.Value + 47, _startPosY.Value + 25, 6, 10); - g.FillRectangle(blueBr, _startPosX.Value + 55, _startPosY.Value + 25, 13, 10); - g.FillRectangle(blueBr, _startPosX.Value + 71, _startPosY.Value + 25, 14, 10); - - g.DrawRectangle(pen, _startPosX.Value + 7, _startPosY.Value + 25, 12, 10); - g.DrawRectangle(pen, _startPosX.Value + 21, _startPosY.Value + 25, 12, 10); - g.DrawRectangle(pen, _startPosX.Value + 47, _startPosY.Value + 25, 6, 10); - g.DrawRectangle(pen, _startPosX.Value + 55, _startPosY.Value + 25, 13, 10); - g.DrawRectangle(pen, _startPosX.Value + 71, _startPosY.Value + 25, 14, 10); - - //переднее окно первый этаж - PointF[] window2 = { new PointF((float)_startPosX + 90, (float)_startPosY + 25), - new PointF((float)_startPosX + 105, (float)_startPosY + 25), - new PointF((float)_startPosX + 105, (float)_startPosY + 40), - new PointF((float)_startPosX + 90, (float)_startPosY + 35) }; - g.FillPolygon(blueBr, window2); - g.DrawPolygon(pen, window2); - - //дверь - Brush brownBr = new SolidBrush(Color.Brown); - g.FillRectangle(blueBr, _startPosX.Value + 35, _startPosY.Value + 25, 10, 15); - g.FillRectangle(brownBr, _startPosX.Value + 35, _startPosY.Value + 40, 10, 5); - - g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 25, 10, 15); - g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 40, 10, 5); - - //колёса - g.FillEllipse(brownBr, _startPosX.Value + 11, _startPosY.Value + 37, 16, 16); - g.FillEllipse(brownBr, _startPosX.Value + 83, _startPosY.Value + 37, 16, 16); - - g.DrawEllipse(pen, _startPosX.Value + 11, _startPosY.Value + 37, 16, 16); - g.DrawEllipse(pen, _startPosX.Value + 83, _startPosY.Value + 37, 16, 16); - //второй этаж с всякими зафуфрючками - if (EntityDoubleDeckerBus.SecondFloor) + if (doubleDeckerBus.SecondFloor) { //верх кузова PointF[] bus_second_floor = { new PointF((float)_startPosX + 5, (float)_startPosY + 5), diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs index ebdf755..310f73d 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs @@ -1,6 +1,6 @@ namespace DoubleDeckerBus.Entities; -public class EntityDoubleDeckerBus -{ +public class EntityDoubleDeckerBus : EntityBus +{ public Color AdditionalColor { get; private set; } /// @@ -13,22 +13,18 @@ public class EntityDoubleDeckerBus public bool Stripes { get; private set; } /// - /// + /// Конструктор наследуемого класса /// /// /// /// /// - /// - /// /// - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) + public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; SecondFloor = secondFloor; Stripes = stripes; + } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs new file mode 100644 index 0000000..069bcbb --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy +{ + internal interface Interface1 + { + } +} -- 2.25.1 From d5621e0d6d123762ccd6e2a06acc24438f40895c Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 4 Mar 2024 01:43:50 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=202=20=D0=BD=D0=B5?= =?UTF-8?q?=20=D0=B4=D0=BE=20=D0=BA=D0=BE=D0=BD=D1=86=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DoubleDeckerBus/Drawnings/DrawingBus.cs | 10 ++- .../Drawnings/DrawingDoubleDeckerBus.cs | 2 +- .../Entities/EntityDoubleDeckerBus.cs | 4 +- .../FormDoubleDeckerBus.Designer.cs | 18 +++- .../DoubleDeckerBus/FormDoubleDeckerBus.cs | 59 ++++++++----- .../MovementStrategy/AbstractStrategy.cs | 84 +++++++++++++++++++ .../MovementStrategy/IMoveableObject..cs | 12 ++- .../MovementStrategy/MoveableBus.cs | 20 +++++ .../MovementStrategy/MovementDirection.cs | 30 +++++++ .../MovementStrategy/ObjectParametrs.cs | 38 +++++++++ .../MovementStrategy/StrategyStatus.cs | 23 +++++ 11 files changed, 270 insertions(+), 30 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs index 9ce608e..6df7581 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -23,6 +23,14 @@ public class DrawingBus private readonly int _drawingBusHeight = 50; + public int? GetPosX => _startPosX; + + public int? GetPosY => _startPosY; + + public int? GetWidth => _drawingBusWidth; + + public int? GetHeight => _drawingBusHeight; + private DrawingBus() { _pictureWidth = null; @@ -164,7 +172,7 @@ public class DrawingBus //кузов 1го этажа - PointF[] bus = { new PointF((float)_startPosX + 5, (float)_startPosY), + PointF[] bus = { new PointF((float)_startPosX, (float)_startPosY), new PointF((float)_startPosX, (float)_startPosY + 25), new PointF((float)_startPosX + 100, (float)_startPosY + 25), new PointF((float)_startPosX + 100, (float)_startPosY + 5), diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index f13bae5..0716c14 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -6,7 +6,7 @@ namespace DoubleDeckerBus.Drawnings; /// public class DrawingDoubleDeckerBus : DrawingBus { - public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (105,50) + public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,50) { EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs index 310f73d..5585664 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs @@ -1,7 +1,9 @@ namespace DoubleDeckerBus.Entities; public class EntityDoubleDeckerBus : EntityBus { - + /// + /// Дополнительный цвет + /// public Color AdditionalColor { get; private set; } /// /// Признак (опция) наличия второго этажа diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index 752b2d6..e2bb000 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -34,6 +34,7 @@ buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); + CreateBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // @@ -42,9 +43,9 @@ buttonCreateDoubleDeckerBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateDoubleDeckerBus.Location = new Point(12, 415); buttonCreateDoubleDeckerBus.Name = "buttonCreateDoubleDeckerBus"; - buttonCreateDoubleDeckerBus.Size = new Size(75, 23); + buttonCreateDoubleDeckerBus.Size = new Size(159, 23); buttonCreateDoubleDeckerBus.TabIndex = 1; - buttonCreateDoubleDeckerBus.Text = "Create"; + buttonCreateDoubleDeckerBus.Text = "Create DoubleDeckerBus"; buttonCreateDoubleDeckerBus.UseVisualStyleBackColor = true; buttonCreateDoubleDeckerBus.Click += buttonCreateDoubleDeckerBus_Click; // @@ -105,11 +106,23 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // CreateBus + // + CreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + CreateBus.Location = new Point(188, 415); + CreateBus.Name = "CreateBus"; + CreateBus.Size = new Size(159, 23); + CreateBus.TabIndex = 7; + CreateBus.Text = "Create Bus"; + CreateBus.UseVisualStyleBackColor = true; + CreateBus.Click += buttonCreateBus_Click; + // // FormDoubleDeckerBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(CreateBus); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); @@ -130,5 +143,6 @@ private Button buttonUp; private Button buttonDown; private Button buttonRight; + private Button CreateBus; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs index bcb3a94..dc611b8 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -13,7 +13,7 @@ namespace DoubleDeckerBus { public partial class FormDoubleDeckerBus : Form { - private DrawingDoubleDeckerBus? _drawingDoubleDeckerBus; + private DrawingBus? _drawingBus; public FormDoubleDeckerBus() { InitializeComponent(); @@ -21,37 +21,53 @@ namespace DoubleDeckerBus private void Draw() { - if (_drawingDoubleDeckerBus == null) + if (_drawingBus == null) { return; } Bitmap bmp = new(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingDoubleDeckerBus.DrawTrasnport(gr); + _drawingBus.DrawTrasnport(gr); pictureBoxDoubleDeckerBus.Image = bmp; } + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawingBus): + _drawingBus = new DrawingBus(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0,256), random.Next(0,256))); + break; + case nameof(DrawingDoubleDeckerBus): + _drawingBus = new DrawingDoubleDeckerBus(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + + _drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void buttonCreateDoubleDeckerBus_Click(object sender, EventArgs e) { - Random random = new(); - _drawingDoubleDeckerBus = new DrawingDoubleDeckerBus(); - _drawingDoubleDeckerBus.Init(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - _drawingDoubleDeckerBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); - _drawingDoubleDeckerBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); - - Draw(); - - - + CreateObject(nameof(DrawingDoubleDeckerBus)); + } + private void buttonCreateBus_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingBus)); } private void buttonMove_Click(object sender, EventArgs e) { - if (_drawingDoubleDeckerBus == null) + if (_drawingBus == null) { return; } @@ -61,19 +77,19 @@ namespace DoubleDeckerBus switch (name) { case "buttonLeft": - result = _drawingDoubleDeckerBus.MoveTransport(DirectionType.Left); + result = _drawingBus.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = _drawingDoubleDeckerBus.MoveTransport(DirectionType.Right); + result = _drawingBus.MoveTransport(DirectionType.Right); break; case "buttonUp": - result = _drawingDoubleDeckerBus.MoveTransport(DirectionType.Up); + result = _drawingBus.MoveTransport(DirectionType.Up); break; case "buttonDown": - result = _drawingDoubleDeckerBus.MoveTransport(DirectionType.Down); + result = _drawingBus.MoveTransport(DirectionType.Down); break; } @@ -82,5 +98,6 @@ namespace DoubleDeckerBus Draw(); } } + } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..2fef05a --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +public abstract class AbstractStrategy +{ + private IMoveableObject? _moveableObject; + + private StrategyStatus _state = StrategyStatus.NotInit; + + protected int FieldWidth { get; private set; } + + protected int FieldHeight { get; private set; } + + public StrategyStatus GetStatus() { return _state; } + + public void SetData(IMoveableObject movableObject, int width, int height) + { + if (movableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + + _state = StrategyStatus.InProgress; + _moveableObject = movableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + + if(IsTargetDestination()) + { + _state = StrategyStatus.Finish; + return; + } + + MoveToTarget(); + } + + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + + protected bool MoveRight() => MoveTo(MovementDirection.Right); + + protected bool MoveUp() => MoveTo(MovementDirection.Up); + + protected bool MoveDown() => MoveTo(MovementDirection.Down); + + protected ObjectParametrs? GetObjectParaments => _moveableObject?.GetObjectPosition; + + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + + return _moveableObject?.GetStep; + } + + protected abstract void MoveToTarget(); + + protected abstract bool IsTargetDestination(); + + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs index 069bcbb..5805ce2 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs @@ -4,9 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace DoubleDeckerBus.MovementStrategy +namespace DoubleDeckerBus.MovementStrategy; + +public interface IMoveableObject { - internal interface Interface1 - { - } + ObjectParametrs? GetObjectPosition { get; } + + int GetStep { get; } + + bool TryMoveObject(MovementDirection direction); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs new file mode 100644 index 0000000..a6a07dc --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +private DrawningBus? _drawningBus; +public class MoveableBus : IMoveableObject +{ + public ObjectParametrs? GetObjectPosition => throw new NotImplementedException(); + + public int GetStep => throw new NotImplementedException(); + + public bool TryMoveObject(MovementDirection direction) + { + throw new NotImplementedException(); + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..1ce4c9c --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +public enum MovementDirection +{ + /// + /// вверх + /// + Up = 1, + + /// + /// вниз + /// + Down = 2, + + /// + /// влево + /// + Left = 3, + + /// + /// вправо + /// + Right = 4 +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs new file mode 100644 index 0000000..60610ba --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +public class ObjectParametrs +{ + private readonly int _x; + + private readonly int _y; + + private readonly int _width; + + private readonly int _height; + + public int LeftBorder => _x; + + public int TopBorder => _y; + + public int RightBorder => _x + _width; + + public int DownBorder => _y + _height; + + public int ObjectMiddleHorizontal => _x + _width / 2; + + public int ObjectMiddleVertical => _y + _height / 2; + + public ObjectParametrs(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..54d76ed --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +} -- 2.25.1 From f10745536b02a8489c10159595aa8c70feb2b924 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 4 Mar 2024 14:08:32 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 4 +- .../DoubleDeckerBus/Drawnings/DrawingBus.cs | 4 +- .../Drawnings/DrawingDoubleDeckerBus.cs | 2 +- .../FormDoubleDeckerBus.Designer.cs | 28 +++++++++ .../DoubleDeckerBus/FormDoubleDeckerBus.cs | 52 ++++++++++++++++- .../MovementStrategy/MoveToCenter.cs | 58 +++++++++++++++++++ .../MovementStrategy/MoveableBus.cs | 48 +++++++++++++-- .../MovementStrategy/MovetoBorder.cs | 41 +++++++++++++ 8 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs index abde908..e569314 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs @@ -1,7 +1,9 @@ namespace DoubleDeckerBus.Drawnings; public enum DirectionType -{ /// +{ + Unknown = -1, + /// /// вверх /// Up = 1, diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs index 6df7581..1035a15 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -27,9 +27,9 @@ public class DrawingBus public int? GetPosY => _startPosY; - public int? GetWidth => _drawingBusWidth; + public int GetWidth => _drawingBusWidth; - public int? GetHeight => _drawingBusHeight; + public int GetHeight => _drawingBusHeight; private DrawingBus() { diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index 0716c14..6e24d22 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -6,7 +6,7 @@ namespace DoubleDeckerBus.Drawnings; /// public class DrawingDoubleDeckerBus : DrawingBus { - public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,50) + public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,55) { EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index e2bb000..a56dad7 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonRight = new Button(); CreateBus = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); SuspendLayout(); // @@ -117,11 +119,35 @@ CreateBus.UseVisualStyleBackColor = true; CreateBus.Click += buttonCreateBus_Click; // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "To center", "To border" }); + comboBoxStrategy.Location = new Point(667, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 8; + // + // buttonStrategyStep + // + buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStrategyStep.Location = new Point(713, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 9; + buttonStrategyStep.Text = "Do step"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormDoubleDeckerBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(CreateBus); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -144,5 +170,7 @@ private Button buttonDown; private Button buttonRight; private Button CreateBus; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs index dc611b8..e3168c9 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -8,15 +8,20 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using DoubleDeckerBus.Drawnings; +using DoubleDeckerBus.MovementStrategy; namespace DoubleDeckerBus { public partial class FormDoubleDeckerBus : Form { private DrawingBus? _drawingBus; + + private AbstractStrategy? _strategy; public FormDoubleDeckerBus() { InitializeComponent(); + _strategy = null; + } private void Draw() @@ -39,7 +44,7 @@ namespace DoubleDeckerBus { case nameof(DrawingBus): _drawingBus = new DrawingBus(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0,256), random.Next(0,256))); + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); break; case nameof(DrawingDoubleDeckerBus): _drawingBus = new DrawingDoubleDeckerBus(random.Next(100, 300), random.Next(1000, 3000), @@ -53,6 +58,8 @@ namespace DoubleDeckerBus _drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -99,5 +106,48 @@ namespace DoubleDeckerBus } } + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingBus == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MovetoBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableBus(_drawingBus), pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + + } + else + { + if (_strategy == null) + { + return; + } + _strategy.MakeStep(); + Draw(); + } + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..86cf34f --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return false; + } + + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return; + } + + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if ( diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs index a6a07dc..36006fd 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs @@ -1,4 +1,5 @@ -using System; +using DoubleDeckerBus.Drawnings; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,15 +7,50 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; -private DrawningBus? _drawningBus; + public class MoveableBus : IMoveableObject { - public ObjectParametrs? GetObjectPosition => throw new NotImplementedException(); + private DrawingBus? _bus = null; - public int GetStep => throw new NotImplementedException(); + + public MoveableBus(DrawingBus bus) + { + _bus = bus; + } + public ObjectParametrs? GetObjectPosition + { + get + { + if (_bus == null || _bus.EntityBus == null || !_bus.GetPosX.HasValue || !_bus.GetPosY.HasValue) + { + return null; + } + return new ObjectParametrs(_bus.GetPosX.Value, _bus.GetPosY.Value, _bus.GetWidth, _bus.GetHeight); + } + } + + public int GetStep => (int)(_bus?.EntityBus?.Step ?? 0); public bool TryMoveObject(MovementDirection direction) { - throw new NotImplementedException(); + if (_bus == null || _bus.EntityBus == null) + { + return false; + } + + return _bus.MoveTransport(GetDirectionType(direction)); } -} + + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknown, + } ; + } + +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs new file mode 100644 index 0000000..f2edf42 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy; + +internal class MovetoBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return false; + } + return objParams.DownBorder + GetStep() > FieldHeight - 5 && objParams.RightBorder + GetStep() > FieldWidth - 5; + } + + protected override void MoveToTarget() + { + ObjectParametrs? objParams = GetObjectParaments; + if (objParams == null) + { + return; + } + + int diffX = objParams.RightBorder - FieldWidth; + if(Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = objParams.DownBorder - FieldHeight; + if(Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} -- 2.25.1 From 7a98888c0e66c4b1d8159845dca0e82f3f61fb65 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Mon, 18 Mar 2024 12:42:44 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20+=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D1=8B=20=D0=BE=D0=B1=D1=8A?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 8 +- .../DoubleDeckerBus/Drawnings/DrawingBus.cs | 84 ++++++++++++++++--- .../Drawnings/DrawingDoubleDeckerBus.cs | 11 ++- .../DoubleDeckerBus/Entities/EntityBus.cs | 12 ++- .../Entities/EntityDoubleDeckerBus.cs | 15 ++-- .../MovementStrategy/AbstractStrategy.cs | 68 ++++++++++++++- .../MovementStrategy/IMoveableObject..cs | 14 ++++ .../MovementStrategy/MoveToCenter.cs | 3 + .../MovementStrategy/MoveableBus.cs | 18 +++- .../MovementStrategy/MovementDirection.cs | 3 + .../MovementStrategy/MovetoBorder.cs | 6 +- .../MovementStrategy/ObjectParametrs.cs | 39 ++++++++- .../MovementStrategy/StrategyStatus.cs | 5 ++ 13 files changed, 254 insertions(+), 32 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs index e569314..347c4b9 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DirectionType.cs @@ -1,8 +1,14 @@ namespace DoubleDeckerBus.Drawnings; - +/// +/// Направление перемещения +/// public enum DirectionType { + /// + /// Неизвестное направление + /// Unknown = -1, + /// /// вверх /// diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs index 1035a15..92599a0 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingBus.cs @@ -7,30 +7,69 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.Drawnings; +/// +/// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности +/// public class DrawingBus { + /// + /// Класс-сущность + /// public EntityBus? EntityBus { get; protected set; } + /// + /// Ширина окна + /// private int? _pictureWidth; + /// + /// Высота окна + /// private int? _pictureHeight; + /// + /// Левая координата прорисовки автобуса + /// protected int? _startPosX; + /// + /// Верхняя кооридната прорисовки автобуса + /// protected int? _startPosY; - private readonly int _drawingBusWidth = 105; + /// + /// Ширина прорисовки автобуса + /// + private readonly int _drawingBusWidth = 100; - private readonly int _drawingBusHeight = 50; + /// + /// Высота прорисовки автобуса + /// + private readonly int _drawingBusHeight = 40; + /// + /// Координата X объекта + /// public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// public int GetWidth => _drawingBusWidth; + /// + /// Высота объекта + /// public int GetHeight => _drawingBusHeight; + /// + /// Пустой конструктор + /// private DrawingBus() { _pictureWidth = null; @@ -39,32 +78,44 @@ public class DrawingBus _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет public DrawingBus(int speed, int weight, Color bodyColor) : this() { EntityBus = new EntityBus(speed, weight, bodyColor); } + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки автобуса + /// Высота прорисовки автобуса protected DrawingBus(int drawingBusWidth, int drawingBusHeight) : this() { _drawingBusWidth = drawingBusWidth; _drawingBusHeight = drawingBusHeight; } + /// - /// размер окна + /// Установка границ поля /// - /// - /// - /// - public bool SetPictureSize(int width, int hight) + /// Ширина поля + /// Высота поля + /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах + public bool SetPictureSize(int width, int height) { - if (_drawingBusWidth > width || _drawingBusHeight > hight) + if (_drawingBusWidth > width || _drawingBusHeight > height) { return false; } _pictureWidth = width; - _pictureHeight = hight; + _pictureHeight = height; if (_startPosX.HasValue && _startPosX.Value + _drawingBusWidth > _pictureWidth) { @@ -80,10 +131,10 @@ public class DrawingBus } /// - /// установить начальную позицию + /// Установка позиции /// - /// - /// + /// Координата X + /// Координа Y public void SetPosition(int x, int y) { if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) @@ -119,6 +170,11 @@ public class DrawingBus } } + /// + /// Изменение направления перемещения + /// + /// Направление + /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) @@ -159,6 +215,10 @@ public class DrawingBus } } + /// + /// Прорисовка объекта + /// + /// public virtual void DrawTrasnport(Graphics g) { if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) diff --git a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs index 6e24d22..f3f2f04 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Drawnings/DrawingDoubleDeckerBus.cs @@ -2,10 +2,19 @@ namespace DoubleDeckerBus.Drawnings; /// -/// +/// Класс, отвечающий за прорисовку и перемещение продвинутого объекта сущности /// public class DrawingDoubleDeckerBus : DrawingBus { + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Доп цвет + /// Признак наличия второго этажа + /// Признак наличия полос на кузове public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,55) { EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs index 3ab2f79..e1ae78c 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.Entities; /// -/// Класс сущность автобус +/// Класс сущность "Автобус" /// public class EntityBus { @@ -14,24 +14,28 @@ public class EntityBus /// Скорость /// public int Speed { get; private set; } + /// /// Вес /// public double Weight { get; private set; } + /// /// Основной цвет /// public Color BodyColor { get; private set; } + /// /// Расстояние перемещения за раз /// public double Step => Speed * 100 / Weight; + /// /// Конструктор базовой сущности /// - /// - /// - /// + /// Скорость + /// Вес автобуса + /// Основной цвет public EntityBus(int speed, double weight, Color bodyColor) { Speed = speed; diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs index 5585664..d955e88 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs @@ -5,23 +5,26 @@ public class EntityDoubleDeckerBus : EntityBus /// Дополнительный цвет /// public Color AdditionalColor { get; private set; } + /// /// Признак (опция) наличия второго этажа /// public bool SecondFloor { get; private set; } + /// /// Признак (опция) наличия полосок на автобусе /// public bool Stripes { get; private set; } /// - /// Конструктор наследуемого класса + /// Конструктор продвинутой сущности /// - /// - /// - /// - /// - /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия второго этажа + /// Призна наличия полос на кузове public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs index 2fef05a..fd85537 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs @@ -6,18 +6,42 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; +/// +/// Класс-стратегия перемещения объекта +/// public abstract class AbstractStrategy { + /// + /// Перемещаемый объект + /// private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// private StrategyStatus _state = StrategyStatus.NotInit; + /// + /// Ширина поля + /// protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// public StrategyStatus GetStatus() { return _state; } - + + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля public void SetData(IMoveableObject movableObject, int width, int height) { if (movableObject == null) @@ -31,7 +55,10 @@ public abstract class AbstractStrategy FieldWidth = width; FieldHeight = height; } - + + /// + /// Шаг перемещения + /// public void MakeStep() { if (_state != StrategyStatus.InProgress) @@ -48,16 +75,39 @@ public abstract class AbstractStrategy MoveToTarget(); } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) protected bool MoveLeft() => MoveTo(MovementDirection.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) protected bool MoveRight() => MoveTo(MovementDirection.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) protected bool MoveUp() => MoveTo(MovementDirection.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) protected bool MoveDown() => MoveTo(MovementDirection.Down); + /// + /// Параметры объекта + /// protected ObjectParametrs? GetObjectParaments => _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// protected int? GetStep() { if (_state != StrategyStatus.InProgress) @@ -68,10 +118,22 @@ public abstract class AbstractStrategy return _moveableObject?.GetStep; } + /// + /// Перемещение к цели + /// protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// protected abstract bool IsTargetDestination(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) private bool MoveTo(MovementDirection movementDirection) { if (_state != StrategyStatus.InProgress) @@ -81,4 +143,4 @@ public abstract class AbstractStrategy return _moveableObject?.TryMoveObject(movementDirection) ?? false; } -} +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs index 5805ce2..b27cf22 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject..cs @@ -6,11 +6,25 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; +/// +/// Интерфейс для работы с перемещаемым объектом +/// public interface IMoveableObject { + /// + /// Получение координаты объекта + /// ObjectParametrs? GetObjectPosition { get; } + /// + /// Шаг объекта + /// int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно bool TryMoveObject(MovementDirection direction); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs index 86cf34f..17f1a2d 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; +/// +/// Стратегия перемещения объекта в центр экрана +/// public class MoveToCenter : AbstractStrategy { protected override bool IsTargetDestination() diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs index 36006fd..3dc16bb 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveableBus.cs @@ -7,16 +7,25 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; - +/// +/// Класс-реализация IMoveableObject с использованием DrawningBus +/// public class MoveableBus : IMoveableObject { + /// + /// Поле-объект класса DrawningBus или его наследника + /// private DrawingBus? _bus = null; - + /// + /// Конструктор + /// + /// Объект класса DrawningBus public MoveableBus(DrawingBus bus) { _bus = bus; } + public ObjectParametrs? GetObjectPosition { get @@ -41,6 +50,11 @@ public class MoveableBus : IMoveableObject return _bus.MoveTransport(GetDirectionType(direction)); } + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType private static DirectionType GetDirectionType(MovementDirection direction) { return direction switch diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs index 1ce4c9c..898f4c5 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovementDirection.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; +/// +/// Направление перемещения +/// public enum MovementDirection { /// diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs index f2edf42..e1eb81d 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MovetoBorder.cs @@ -5,7 +5,9 @@ using System.Text; using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; - +/// +/// Стратегия перемещения объекта в правый нижний угол +/// internal class MovetoBorder : AbstractStrategy { protected override bool IsTargetDestination() @@ -15,7 +17,7 @@ internal class MovetoBorder : AbstractStrategy { return false; } - return objParams.DownBorder + GetStep() > FieldHeight - 5 && objParams.RightBorder + GetStep() > FieldWidth - 5; + return objParams.DownBorder + GetStep() >= FieldHeight && objParams.RightBorder + GetStep() >= FieldWidth; } protected override void MoveToTarget() diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs index 60610ba..0eb7c60 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParametrs.cs @@ -8,26 +8,63 @@ namespace DoubleDeckerBus.MovementStrategy; public class ObjectParametrs { + /// + /// Координата X + /// private readonly int _x; + /// + /// Координата Y + /// private readonly int _y; + /// + /// Ширина объекта + /// private readonly int _width; + /// + /// Высота объекта + /// private readonly int _height; + /// + /// Левая граница + /// public int LeftBorder => _x; + /// + /// Верхняя граница + /// public int TopBorder => _y; - + + /// + /// Правая граница + /// public int RightBorder => _x + _width; + /// + /// Нижняя граница + /// public int DownBorder => _y + _height; + /// + /// Середина обхекта по горизонтали + /// public int ObjectMiddleHorizontal => _x + _width / 2; + /// + /// Середина объекта по вертикали + /// public int ObjectMiddleVertical => _y + _height / 2; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина объекта + /// Высота объекта public ObjectParametrs(int x, int y, int width, int height) { _x = x; diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs index 54d76ed..85556b7 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/StrategyStatus.cs @@ -6,16 +6,21 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.MovementStrategy; +/// +/// Статус выполнения операции перемещения +/// public enum StrategyStatus { /// /// Все готово к началу /// NotInit, + /// /// Выполняется /// InProgress, + /// /// Завершено /// -- 2.25.1