From b956941018eebec2acdb6e9996d32b434a38f4ac Mon Sep 17 00:00:00 2001 From: Anya Date: Thu, 11 Apr 2024 11:18:53 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=80=D0=BE=D0=B4=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=B9=20=D0=B8=20=D0=B2=D0=B2=D0=BE=D0=B4=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab_0/{ => Drawnings}/DirectionType.cs | 10 +- lab_0/Drawnings/DrawningBus.cs | 91 +++++++++++ .../DrawningSimpleBus.cs} | 148 ++++++------------ lab_0/{ => Entities}/EntityBus.cs | 36 +---- lab_0/Entities/EntitySimpleBus.cs | 42 +++++ lab_0/FormBus.Designer.cs | 32 ++-- lab_0/FormBus.cs | 70 ++++++--- 7 files changed, 268 insertions(+), 161 deletions(-) rename lab_0/{ => Drawnings}/DirectionType.cs (68%) create mode 100644 lab_0/Drawnings/DrawningBus.cs rename lab_0/{DrawingBus.cs => Drawnings/DrawningSimpleBus.cs} (56%) rename lab_0/{ => Entities}/EntityBus.cs (65%) create mode 100644 lab_0/Entities/EntitySimpleBus.cs diff --git a/lab_0/DirectionType.cs b/lab_0/Drawnings/DirectionType.cs similarity index 68% rename from lab_0/DirectionType.cs rename to lab_0/Drawnings/DirectionType.cs index 0fab0ff..bcea611 100644 --- a/lab_0/DirectionType.cs +++ b/lab_0/Drawnings/DirectionType.cs @@ -1,10 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ProjectBus. Drawings; -namespace ProjectBus; +/// +/// Направление перемещения +/// public enum DirectionType { diff --git a/lab_0/Drawnings/DrawningBus.cs b/lab_0/Drawnings/DrawningBus.cs new file mode 100644 index 0000000..b98a55b --- /dev/null +++ b/lab_0/Drawnings/DrawningBus.cs @@ -0,0 +1,91 @@ +using ProjectBus.Entities; +using System.Drawing; + +namespace ProjectBus.Drawnings; + +/// +/// Класс, отвечающий за прорисовку и перемещение объекта-сущности +/// +public class DrawningBus : DrawningSimpleBus +{ + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия дополнительного отсека + /// Признак наличия гармошки + public DrawningBus(int speed, double weight, Color bodyColor, Color additionalColor, bool additionalCompartment, bool accordion) : base(310,52) + { + EntitySimpleBus = new EntityBus(speed, weight, bodyColor, additionalColor, additionalCompartment, accordion); + } + + public override void DrawTransport(Graphics g) + { + if (EntitySimpleBus == null || EntitySimpleBus is not EntityBus bus || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush additionalBrush= new SolidBrush(bus.AdditionalColor); + + //доп отсек + if (bus.AdditionalCompartment) + { + //корпус + g.DrawRectangle(pen, _startPosX.Value + 204, _startPosY.Value - 1, 105, 50); + g.DrawRectangle(pen, _startPosX.Value + 205, _startPosY.Value, 105, 50); + g.FillRectangle(additionalBrush, _startPosX.Value + 205, _startPosY.Value, 105, 50); + + + //окна + g.FillEllipse(additionalBrush, _startPosX.Value + 247, _startPosY.Value + 6, 16, 25); + g.FillEllipse(additionalBrush, _startPosX.Value + 275, _startPosY.Value + 6, 16, 25); + g.DrawEllipse(pen, _startPosX.Value + 247, _startPosY.Value + 6, 16, 25); + g.DrawEllipse(pen, _startPosX.Value + 275, _startPosY.Value + 6, 16, 25); + + + //дверь + g.FillRectangle(additionalBrush, _startPosX.Value + 215, _startPosY.Value + 10, 20, 40); + g.DrawRectangle(pen, _startPosX.Value + 215, _startPosY.Value + 10, 20, 40); + + //колеса + g.FillEllipse(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20); + g.DrawEllipse(pen, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20); + } + + _startPosX += 0; + _startPosY += 0; + base.DrawTransport(g); + + + //гармошка + if (bus.Accordion) + { + Brush brGray = new SolidBrush(Color.LightGray); + + g.FillRectangle(brGray, _startPosX.Value + 175, _startPosY.Value + 4, 30, 42); + g.FillRectangle(brGray, _startPosX.Value + 180, _startPosY.Value + 4, 5, 42); + g.FillRectangle(brGray, _startPosX.Value + 185, _startPosY.Value + 4, 5, 42); + g.FillRectangle(brGray, _startPosX.Value + 190, _startPosY.Value + 4, 5, 42); + g.FillRectangle(brGray, _startPosX.Value + 195, _startPosY.Value + 4, 5, 42); + + g.DrawRectangle(pen, _startPosX.Value + 175, _startPosY.Value + 4, 30, 42); + g.DrawRectangle(pen, _startPosX.Value + 180, _startPosY.Value + 4, 5, 42); + g.DrawRectangle(pen, _startPosX.Value + 185, _startPosY.Value + 4, 5, 42); + g.DrawRectangle(pen, _startPosX.Value + 190, _startPosY.Value + 4, 5, 42); + g.DrawRectangle(pen, _startPosX.Value + 195, _startPosY.Value + 4, 5, 42); + } + } +} + + + + + + + + diff --git a/lab_0/DrawingBus.cs b/lab_0/Drawnings/DrawningSimpleBus.cs similarity index 56% rename from lab_0/DrawingBus.cs rename to lab_0/Drawnings/DrawningSimpleBus.cs index 24592a5..31fa21a 100644 --- a/lab_0/DrawingBus.cs +++ b/lab_0/Drawnings/DrawningSimpleBus.cs @@ -1,15 +1,14 @@ -namespace ProjectBus; +using ProjectBus.Drawings; +using ProjectBus.Entities; -/// -/// Класс, отвечающий за прорисовку и перемещение объекта-сущности -/// +namespace ProjectBus.Drawnings; -public class DrawingBus +public class DrawningSimpleBus { /// /// Класс-сущность /// - public EntityBus? EntityBus { get; private set; } + public EntitySimpleBus? EntitySimpleBus { get; protected set; } /// /// Ширина окна @@ -24,16 +23,16 @@ public class DrawingBus /// /// Левая координата прорисовки автобуса /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя кооридната прорисовки автобуса /// - private int? _startPosY; + protected int? _startPosY; /// - /// /// Ширина прорисовки автобуса + /// Ширина прорисовки автобуса /// /// private readonly int _drawningBusWidth = 310; @@ -43,22 +42,37 @@ public class DrawingBus private readonly int _drawningBusHeight = 52; /// - /// Инициализация свойств + /// Пустой конструктор /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия дополнительного отсека - /// Признак наличия гармошки - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool additionalCompartment, bool accordion, bool v) + private DrawningSimpleBus() { - EntityBus = new EntityBus(); - EntityBus.Init(speed, weight, bodyColor, additionalColor, additionalCompartment, accordion); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; + + } + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + public DrawningSimpleBus(int speed, double weight, Color bodyColor) : this() + { + EntitySimpleBus = new EntitySimpleBus(speed, weight, bodyColor); + } + + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки автобуса + /// Высота прорисовки автобуса + protected DrawningSimpleBus(int drawningBusWidth, int drawningBusHeight) : this() + { + _drawningBusWidth = drawningBusWidth; + _drawningBusHeight= drawningBusHeight; } /// @@ -67,16 +81,6 @@ public class DrawingBus /// Ширина поля /// Высота поля /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах - //public bool SetPictureSize(int width, int height) - //{ - // // TODO проверка, что объект "влезает" в размеры поля - // // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - - // _pictureWidth = width; - // _pictureHeight = height; - - // return true; - //} public bool SetPictureSize(int width, int height) { if (width >= _drawningBusWidth && height >= _drawningBusWidth) @@ -148,7 +152,7 @@ public class DrawingBus /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntitySimpleBus == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } @@ -157,30 +161,30 @@ public class DrawingBus { //влево case DirectionType.Left: - if (_startPosX.Value - EntityBus.Step > 0) + if (_startPosX.Value - EntitySimpleBus.Step > 0) { - _startPosX -= (int)EntityBus.Step; + _startPosX -= (int)EntitySimpleBus.Step; } return true; //вверх case DirectionType.Up: - if (_startPosY.Value - EntityBus.Step > 0) + if (_startPosY.Value - EntitySimpleBus.Step > 0) { - _startPosY -= (int)EntityBus.Step; + _startPosY -= (int)EntitySimpleBus.Step; } return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityBus.Step <_pictureWidth - _drawningBusWidth) + if (_startPosX.Value + EntitySimpleBus.Step <_pictureWidth - _drawningBusWidth) { - _startPosX += (int)EntityBus.Step; + _startPosX += (int)EntitySimpleBus.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityBus.Step < _pictureHeight - _drawningBusHeight) + if (_startPosY.Value + EntitySimpleBus.Step < _pictureHeight - _drawningBusHeight) { - _startPosY += (int)EntityBus.Step; + _startPosY += (int)EntitySimpleBus.Step; } return true; default: @@ -192,15 +196,15 @@ public class DrawingBus /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntitySimpleBus == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } + Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityBus.AdditionalColor); //корпус автобуса @@ -209,7 +213,8 @@ public class DrawingBus g.DrawRectangle(pen, _startPosX.Value + 0, _startPosY.Value, 175, 50); g.DrawRectangle(pen, _startPosX.Value + 0, _startPosY.Value - 1, 175, 50); - g.FillRectangle(additionalBrush, _startPosX.Value + 0, _startPosY.Value, 175, 50); + Brush br1 = new SolidBrush(EntitySimpleBus.BodyColor); + g.FillRectangle(br1, _startPosX.Value + 0, _startPosY.Value, 175, 50); //окна @@ -231,10 +236,10 @@ public class DrawingBus //дверь - Brush br = new SolidBrush(EntityBus.BodyColor); + Brush br2 = new SolidBrush(EntitySimpleBus.BodyColor); g.DrawRectangle(pen, _startPosX.Value + 49, _startPosY.Value + 9, 20, 40); g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 10, 20, 40); - g.FillRectangle(br, _startPosX.Value + 50, _startPosY.Value + 10, 20, 40); + g.FillRectangle(br2, _startPosX.Value + 50, _startPosY.Value + 10, 20, 40); @@ -247,60 +252,7 @@ public class DrawingBus g.DrawEllipse(pen, _startPosX.Value + 115, _startPosY.Value + 40, 20, 20); - //доп отсек - if (EntityBus.AdditionalCompartment) - { - //корпус - g.DrawRectangle(pen, _startPosX.Value + 204, _startPosY.Value - 1, 105, 50); - g.DrawRectangle(pen, _startPosX.Value + 205, _startPosY.Value, 105, 50); - g.FillRectangle(additionalBrush, _startPosX.Value + 205, _startPosY.Value, 105, 50); - - - //окна - g.FillEllipse(brBlue, _startPosX.Value + 247, _startPosY.Value + 6, 16, 25); - g.FillEllipse(brBlue, _startPosX.Value + 275, _startPosY.Value + 6, 16, 25); - g.DrawEllipse(pen, _startPosX.Value + 247, _startPosY.Value + 6, 16, 25); - g.DrawEllipse(pen, _startPosX.Value + 275, _startPosY.Value + 6, 16, 25); - - - - //дверь - g.FillRectangle(br, _startPosX.Value + 215, _startPosY.Value + 10, 20, 40); - g.DrawRectangle(pen, _startPosX.Value + 215, _startPosY.Value + 10, 20, 40); - //колеса - - - g.FillEllipse(brGrey, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20); - g.DrawEllipse(pen, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20); - - - } - - //гармошка - if (EntityBus.Accordion) - { - Brush brGray = new SolidBrush(Color.LightGray); - - g.FillRectangle(brGray, _startPosX.Value + 175, _startPosY.Value + 4, 30, 42); - g.FillRectangle(brGray, _startPosX.Value + 180, _startPosY.Value + 4, 5, 42); - g.FillRectangle(brGray, _startPosX.Value + 185, _startPosY.Value + 4, 5, 42); - g.FillRectangle(brGray, _startPosX.Value + 190, _startPosY.Value + 4, 5, 42); - g.FillRectangle(brGray, _startPosX.Value + 195, _startPosY.Value + 4, 5, 42); - - g.DrawRectangle(pen, _startPosX.Value + 175, _startPosY.Value + 4, 30, 42); - g.DrawRectangle(pen, _startPosX.Value + 180, _startPosY.Value + 4, 5, 42); - g.DrawRectangle(pen, _startPosX.Value + 185, _startPosY.Value + 4, 5, 42); - g.DrawRectangle(pen, _startPosX.Value + 190, _startPosY.Value + 4, 5, 42); - g.DrawRectangle(pen, _startPosX.Value + 195, _startPosY.Value + 4, 5, 42); - - } } -} - - - - - - + } diff --git a/lab_0/EntityBus.cs b/lab_0/Entities/EntityBus.cs similarity index 65% rename from lab_0/EntityBus.cs rename to lab_0/Entities/EntityBus.cs index 13fb1d3..9e89c00 100644 --- a/lab_0/EntityBus.cs +++ b/lab_0/Entities/EntityBus.cs @@ -1,45 +1,27 @@ -namespace ProjectBus; +namespace ProjectBus.Entities; /// /// Класс-сущность "Автобус" /// -public class EntityBus +public class EntityBus : EntitySimpleBus { - /// - /// Скорость - /// - public int Speed { get; set; } - - /// - /// Вес - /// - public double Weight { get; set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет (для опциональных элементов) /// + /// public Color AdditionalColor { get; private set; } + public Color BodyColor { get; private set; } /// - /// Признак (опция) наличия контейнеров + /// Признак (опция) наличия дополнительного отсека /// public bool AdditionalCompartment { get; private set; } /// - /// Признак (опция) наличия крана для разгрузки + /// Признак (опция) наличия гармошки /// public bool Accordion { get; private set; } - /// - /// Шаг перемещения автобуса - /// - public double Step => Speed * 100 / Weight; - /// /// Инициализация полей объекта-класса спортивного автомобиля /// @@ -49,12 +31,10 @@ public class EntityBus /// Дополнительный цвет /// Признак наличия дополнительного отсека /// Признак наличия гармошки - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool additionalCompartment, bool accordion) + public EntityBus(int speed, double weight, Color bodyColor, Color additionalColor, bool additionalCompartment, bool accordion) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; + BodyColor = bodyColor; AdditionalCompartment = additionalCompartment; Accordion = accordion; } diff --git a/lab_0/Entities/EntitySimpleBus.cs b/lab_0/Entities/EntitySimpleBus.cs new file mode 100644 index 0000000..9e68233 --- /dev/null +++ b/lab_0/Entities/EntitySimpleBus.cs @@ -0,0 +1,42 @@ +namespace ProjectBus.Entities; + +/// +/// Класс-сущность "Автобус" +/// + +public class EntitySimpleBus +{ + /// + /// Скорость + /// + public int Speed { get; set; } + + /// + /// Вес + /// + public double Weight { get; set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + + /// + /// Шаг перемещения автомобиля + /// + public double Step => Speed * 100 / Weight; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес автобуса + /// Основной цвет + public EntitySimpleBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/lab_0/FormBus.Designer.cs b/lab_0/FormBus.Designer.cs index bdd01e3..885c720 100644 --- a/lab_0/FormBus.Designer.cs +++ b/lab_0/FormBus.Designer.cs @@ -34,6 +34,7 @@ buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); + buttonCreateSimpleBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit(); SuspendLayout(); // @@ -42,18 +43,18 @@ pictureBoxBus.Dock = DockStyle.Fill; pictureBoxBus.Location = new Point(0, 0); pictureBoxBus.Name = "pictureBoxBus"; - pictureBoxBus.Size = new Size(800, 450); + pictureBoxBus.Size = new Size(1155, 557); pictureBoxBus.TabIndex = 7; pictureBoxBus.TabStop = false; // // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 392); + buttonCreate.Location = new Point(12, 501); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(150, 46); + buttonCreate.Size = new Size(410, 46); buttonCreate.TabIndex = 2; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать автобус с доп.отсеком"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += ButtonCreate_Click; // @@ -62,7 +63,7 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(626, 388); + buttonLeft.Location = new Point(981, 495); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(50, 50); buttonLeft.TabIndex = 3; @@ -74,7 +75,7 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(738, 390); + buttonRight.Location = new Point(1093, 497); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(50, 50); buttonRight.TabIndex = 4; @@ -86,7 +87,7 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.arrowUp; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(682, 332); + buttonUp.Location = new Point(1037, 439); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(50, 50); buttonUp.TabIndex = 5; @@ -98,18 +99,30 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(682, 388); + buttonDown.Location = new Point(1037, 495); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(50, 50); buttonDown.TabIndex = 6; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // + // buttonCreateSimpleBus + // + buttonCreateSimpleBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateSimpleBus.Location = new Point(428, 501); + buttonCreateSimpleBus.Name = "buttonCreateSimpleBus"; + buttonCreateSimpleBus.Size = new Size(431, 46); + buttonCreateSimpleBus.TabIndex = 8; + buttonCreateSimpleBus.Text = "Создать обычный автобус"; + buttonCreateSimpleBus.UseVisualStyleBackColor = true; + buttonCreateSimpleBus.Click += buttonCreateSimpleBus_Click; + // // FormBus // AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(1155, 557); + Controls.Add(buttonCreateSimpleBus); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonRight); @@ -129,5 +142,6 @@ private Button buttonRight; private Button buttonUp; private Button buttonDown; + private Button buttonCreateSimpleBus; } } \ No newline at end of file diff --git a/lab_0/FormBus.cs b/lab_0/FormBus.cs index d170e9e..e91af74 100644 --- a/lab_0/FormBus.cs +++ b/lab_0/FormBus.cs @@ -1,4 +1,6 @@ -using System; +using ProjectBus.Drawings; +using ProjectBus.Drawnings; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,7 +14,7 @@ namespace ProjectBus { public partial class FormBus : Form { - private DrawingBus? _drawingBus; + private DrawningSimpleBus? _drawningSimpleBus; public FormBus() { @@ -21,36 +23,65 @@ namespace ProjectBus private void Draw() { - if (_drawingBus == null) + if (_drawningSimpleBus == null) { return; } Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingBus.DrawTransport(gr); + _drawningSimpleBus.DrawTransport(gr); pictureBoxBus.Image = bmp; } - - private void ButtonCreate_Click(object sender, EventArgs e) + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) { Random random = new(); - _drawingBus = new DrawingBus(); - _drawingBus.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)), Convert.ToBoolean(random.Next(0, 2))); - _drawingBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height); - _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + switch (type) + { + case nameof(DrawningSimpleBus): + _drawningSimpleBus = new DrawningSimpleBus(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(DrawningBus): + _drawningSimpleBus = new DrawningBus(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; + } + + _drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height); + _drawningSimpleBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); - } + /// + /// Обработка нажатия кнопки "Создать автобус с доп отсеком" + /// + /// + /// + private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); + + /// + /// Обработка нажатия кнопки "Создать обычный автобус" + /// + /// + /// + private void buttonCreateSimpleBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSimpleBus)); + + + private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawingBus == null) + if (_drawningSimpleBus == null) { return; } @@ -61,19 +92,19 @@ namespace ProjectBus { case "buttonUp": result = - _drawingBus.MoveTransport(DirectionType.Up); + _drawningSimpleBus.MoveTransport(DirectionType.Up); break; case "buttonDown": result = - _drawingBus.MoveTransport(DirectionType.Down); + _drawningSimpleBus.MoveTransport(DirectionType.Down); break; case "buttonLeft": result = - _drawingBus.MoveTransport(DirectionType.Left); + _drawningSimpleBus.MoveTransport(DirectionType.Left); break; case "buttonRight": result = - _drawingBus.MoveTransport(DirectionType.Right); + _drawningSimpleBus.MoveTransport(DirectionType.Right); break; } @@ -81,7 +112,6 @@ namespace ProjectBus { Draw(); } - } } } -- 2.25.1 From 6323fb7bc8fb1232f33b84bee61cbb3ed7a2e130 Mon Sep 17 00:00:00 2001 From: Anya Date: Thu, 18 Apr 2024 14:03:03 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab_0/Drawnings/DirectionType.cs | 7 +- lab_0/Drawnings/DrawningBus.cs | 2 - lab_0/Drawnings/DrawningSimpleBus.cs | 20 +- lab_0/FormBus.Designer.cs | 26 +++ lab_0/FormBus.cs | 227 +++++++++++++------- lab_0/MovementStrategy/AbstractStrategy.cs | 139 ++++++++++++ lab_0/MovementStrategy/IMoveableObject.cs | 24 +++ lab_0/MovementStrategy/MoveToBorder.cs | 57 +++++ lab_0/MovementStrategy/MoveToCenter.cs | 55 +++++ lab_0/MovementStrategy/MoveableSimpleBus.cs | 66 ++++++ lab_0/MovementStrategy/MovementDirection.cs | 28 +++ lab_0/MovementStrategy/ObjectParameters.cs | 72 +++++++ lab_0/MovementStrategy/StrategyStatus.cs | 22 ++ 13 files changed, 658 insertions(+), 87 deletions(-) create mode 100644 lab_0/MovementStrategy/AbstractStrategy.cs create mode 100644 lab_0/MovementStrategy/IMoveableObject.cs create mode 100644 lab_0/MovementStrategy/MoveToBorder.cs create mode 100644 lab_0/MovementStrategy/MoveToCenter.cs create mode 100644 lab_0/MovementStrategy/MoveableSimpleBus.cs create mode 100644 lab_0/MovementStrategy/MovementDirection.cs create mode 100644 lab_0/MovementStrategy/ObjectParameters.cs create mode 100644 lab_0/MovementStrategy/StrategyStatus.cs diff --git a/lab_0/Drawnings/DirectionType.cs b/lab_0/Drawnings/DirectionType.cs index bcea611..8214c58 100644 --- a/lab_0/Drawnings/DirectionType.cs +++ b/lab_0/Drawnings/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectBus. Drawings; +namespace ProjectBus. Drawnings; /// /// Направление перемещения @@ -6,6 +6,11 @@ public enum DirectionType { + /// + /// Вверх + /// + Unknow = -1, + /// /// Вверх /// diff --git a/lab_0/Drawnings/DrawningBus.cs b/lab_0/Drawnings/DrawningBus.cs index b98a55b..e391f59 100644 --- a/lab_0/Drawnings/DrawningBus.cs +++ b/lab_0/Drawnings/DrawningBus.cs @@ -57,8 +57,6 @@ public class DrawningBus : DrawningSimpleBus g.DrawEllipse(pen, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20); } - _startPosX += 0; - _startPosY += 0; base.DrawTransport(g); diff --git a/lab_0/Drawnings/DrawningSimpleBus.cs b/lab_0/Drawnings/DrawningSimpleBus.cs index 31fa21a..873435a 100644 --- a/lab_0/Drawnings/DrawningSimpleBus.cs +++ b/lab_0/Drawnings/DrawningSimpleBus.cs @@ -1,4 +1,4 @@ -using ProjectBus.Drawings; +using ProjectBus.Drawnings; using ProjectBus.Entities; namespace ProjectBus.Drawnings; @@ -29,7 +29,6 @@ public class DrawningSimpleBus /// Верхняя кооридната прорисовки автобуса /// protected int? _startPosY; - /// /// Ширина прорисовки автобуса @@ -41,6 +40,23 @@ public class DrawningSimpleBus /// private readonly int _drawningBusHeight = 52; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _drawningBusWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningBusHeight; + /// /// Пустой конструктор /// diff --git a/lab_0/FormBus.Designer.cs b/lab_0/FormBus.Designer.cs index 885c720..cdcb98c 100644 --- a/lab_0/FormBus.Designer.cs +++ b/lab_0/FormBus.Designer.cs @@ -35,6 +35,8 @@ buttonUp = new Button(); buttonDown = new Button(); buttonCreateSimpleBus = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit(); SuspendLayout(); // @@ -117,11 +119,33 @@ buttonCreateSimpleBus.UseVisualStyleBackColor = true; buttonCreateSimpleBus.Click += buttonCreateSimpleBus_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(901, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(242, 40); + comboBoxStrategy.TabIndex = 9; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(993, 58); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(150, 46); + buttonStrategyStep.TabIndex = 10; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormBus // AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1155, 557); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateSimpleBus); Controls.Add(buttonDown); Controls.Add(buttonUp); @@ -143,5 +167,7 @@ private Button buttonUp; private Button buttonDown; private Button buttonCreateSimpleBus; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/lab_0/FormBus.cs b/lab_0/FormBus.cs index e91af74..f63567b 100644 --- a/lab_0/FormBus.cs +++ b/lab_0/FormBus.cs @@ -1,5 +1,5 @@ -using ProjectBus.Drawings; -using ProjectBus.Drawnings; +using ProjectBus.Drawnings; +using ProjectBus.MovementStrategy; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,109 +10,172 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace ProjectBus +namespace ProjectBus; +/// +/// форма работы с объектом "Автобус" +/// +public partial class FormBus : Form { - public partial class FormBus : Form + /// + /// поле-объект для прорисовки объекта + /// + private DrawningSimpleBus? _drawningSimpleBus; + + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + + + /// + /// конструктор формы + /// + public FormBus() { - private DrawningSimpleBus? _drawningSimpleBus; + InitializeComponent(); + _strategy = null; + } - public FormBus() + /// + /// метод прорисовки машины + /// + private void Draw() + { + if (_drawningSimpleBus == null) { - InitializeComponent(); + return; } - private void Draw() + Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningSimpleBus.DrawTransport(gr); + pictureBoxBus.Image = bmp; + } + + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) { - if (_drawningSimpleBus == null) - { + case nameof(DrawningSimpleBus): + _drawningSimpleBus = new DrawningSimpleBus(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(DrawningBus): + _drawningSimpleBus = new DrawningBus(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; - } - - Bitmap bmp = new(pictureBoxBus.Width, pictureBoxBus.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningSimpleBus.DrawTransport(gr); - pictureBoxBus.Image = bmp; } - /// - /// Создание объекта класса-перемещения - /// - /// Тип создаваемого объекта - private void CreateObject(string type) + _drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height); + _drawningSimpleBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + + Draw(); + } + + /// + /// Обработка нажатия кнопки "Создать автобус с доп отсеком" + /// + /// + /// + private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); + + /// + /// Обработка нажатия кнопки "Создать обычный автобус" + /// + /// + /// + private void buttonCreateSimpleBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSimpleBus)); + + + + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningSimpleBus == null) { - Random random = new(); - switch (type) - { - case nameof(DrawningSimpleBus): - _drawningSimpleBus = new DrawningSimpleBus(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(DrawningBus): - _drawningSimpleBus = new DrawningBus(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; - } + return; + } - _drawningSimpleBus.SetPictureSize(pictureBoxBus.Width, pictureBoxBus.Height); - _drawningSimpleBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = + _drawningSimpleBus.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = + _drawningSimpleBus.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = + _drawningSimpleBus.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = + _drawningSimpleBus.MoveTransport(DirectionType.Right); + break; + } + if (result) + { Draw(); } + } - /// - /// Обработка нажатия кнопки "Создать автобус с доп отсеком" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); - - /// - /// Обработка нажатия кнопки "Создать обычный автобус" - /// - /// - /// - private void buttonCreateSimpleBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningSimpleBus)); - - - - private void ButtonMove_Click(object sender, EventArgs e) + /// + /// + /// + /// + /// + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningSimpleBus == null) { - if (_drawningSimpleBus == null) + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) { return; } + _strategy.SetData(new MoveableSimpleBus(_drawningSimpleBus), pictureBoxBus.Width, pictureBoxBus.Height); + } - string name = ((Button)sender)?.Name ?? string.Empty; - bool result = false; - switch (name) - { - case "buttonUp": - result = - _drawningSimpleBus.MoveTransport(DirectionType.Up); - break; - case "buttonDown": - result = - _drawningSimpleBus.MoveTransport(DirectionType.Down); - break; - case "buttonLeft": - result = - _drawningSimpleBus.MoveTransport(DirectionType.Left); - break; - case "buttonRight": - result = - _drawningSimpleBus.MoveTransport(DirectionType.Right); - break; - } + if (_strategy == null) + { + return; + } - if (result) - { - Draw(); - } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; } } } + diff --git a/lab_0/MovementStrategy/AbstractStrategy.cs b/lab_0/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..aeb8914 --- /dev/null +++ b/lab_0/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,139 @@ +namespace ProjectBus.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 moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + + 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 ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} \ No newline at end of file diff --git a/lab_0/MovementStrategy/IMoveableObject.cs b/lab_0/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..6b7c9e4 --- /dev/null +++ b/lab_0/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,24 @@ +namespace ProjectBus.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/lab_0/MovementStrategy/MoveToBorder.cs b/lab_0/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..1a70a48 --- /dev/null +++ b/lab_0/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBus.MovementStrategy; +public class MoveToBorder : AbstractStrategy +{ + + + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder - GetStep() <= FieldWidth + && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight + && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} \ No newline at end of file diff --git a/lab_0/MovementStrategy/MoveToCenter.cs b/lab_0/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..59df3bb --- /dev/null +++ b/lab_0/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBus.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + 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() + { + ObjectParameters? objParams = GetObjectParameters; + 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(); + } + } + } +} \ No newline at end of file diff --git a/lab_0/MovementStrategy/MoveableSimpleBus.cs b/lab_0/MovementStrategy/MoveableSimpleBus.cs new file mode 100644 index 0000000..3c83c2c --- /dev/null +++ b/lab_0/MovementStrategy/MoveableSimpleBus.cs @@ -0,0 +1,66 @@ +using ProjectBus.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBus.MovementStrategy; + +/// +/// Класс-реализация IMoveableObject с использованием DrawingSimpleBus +/// + +public class MoveableSimpleBus : IMoveableObject +{ + // + /// Поле-объект класса DrawningTrans или его наследника + /// + private readonly DrawningSimpleBus? _simplebus = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningSimpleBus + public MoveableSimpleBus(DrawningSimpleBus simplebus) + { + _simplebus = simplebus; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_simplebus == null || _simplebus.EntitySimpleBus == null || !_simplebus.GetPosX.HasValue || !_simplebus.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_simplebus.GetPosX.Value, _simplebus.GetPosY.Value, _simplebus.GetWidth, _simplebus.GetHeight); + } + } + public int GetStep => (int)(_simplebus?.EntitySimpleBus ?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_simplebus == null || _simplebus.EntitySimpleBus == null) + { + return false; + } + return _simplebus.MoveTransport(GetDirectionType(direction)); + } + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + 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.Unknow, + }; + } +} diff --git a/lab_0/MovementStrategy/MovementDirection.cs b/lab_0/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..593381a --- /dev/null +++ b/lab_0/MovementStrategy/MovementDirection.cs @@ -0,0 +1,28 @@ +namespace ProjectBus.MovementStrategy; + +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Неизвестное направление + /// + Unknow = -1, + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/lab_0/MovementStrategy/ObjectParameters.cs b/lab_0/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..5d99dbb --- /dev/null +++ b/lab_0/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,72 @@ +namespace ProjectBus.MovementStrategy; + +/// +/// Параметры-координаты объекта +/// +public class ObjectParameters +{ + /// + /// Координата 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 ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/lab_0/MovementStrategy/StrategyStatus.cs b/lab_0/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..42abe03 --- /dev/null +++ b/lab_0/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,22 @@ +namespace ProjectBus.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +} \ No newline at end of file -- 2.25.1 From 11912072995f72b5439ba247de3c7b50999c518a Mon Sep 17 00:00:00 2001 From: Anya Date: Thu, 18 Apr 2024 14:28:39 +0400 Subject: [PATCH 3/3] lab2finish --- lab_0/MovementStrategy/MoveToBorder.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/lab_0/MovementStrategy/MoveToBorder.cs b/lab_0/MovementStrategy/MoveToBorder.cs index 1a70a48..c273b21 100644 --- a/lab_0/MovementStrategy/MoveToBorder.cs +++ b/lab_0/MovementStrategy/MoveToBorder.cs @@ -21,7 +21,6 @@ public class MoveToBorder : AbstractStrategy objParams.DownBorder - GetStep() <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; } - protected override void MoveToTarget() { ObjectParameters? objParams = GetObjectParameters; -- 2.25.1