From 5b001c5b2fb04d4b0f9e7eafb8bdab993e6794e7 Mon Sep 17 00:00:00 2001 From: russell Date: Wed, 11 Oct 2023 22:38:57 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=202?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DumpTruck/Direction.cs | 2 +- DumpTruck/DrawingDumpTruck.cs | 156 ------------ DumpTruck/DrawingObjects/DrawingDumpTruck.cs | 53 ++++ DumpTruck/DrawingObjects/DrawingTruck.cs | 190 ++++++++++++++ DumpTruck/Entities/EntityDumpTruck.cs | 46 ++++ DumpTruck/Entities/EntityTruck.cs | 41 +++ DumpTruck/EntityDumpTruck.cs | 66 ----- DumpTruck/FormDumpTruck.Designer.cs | 233 +++++++++++------- DumpTruck/FormDumpTruck.cs | 97 ++++++-- .../MovementStrategy/AbstractStrategy.cs | 129 ++++++++++ .../MovementStrategy/DrawningObjectTruck.cs | 36 +++ DumpTruck/MovementStrategy/IMoveableObject.cs | 31 +++ DumpTruck/MovementStrategy/MoveToBorder.cs | 49 ++++ DumpTruck/MovementStrategy/MoveToCenter.cs | 56 +++++ .../MovementStrategy/ObjectParameters.cs | 54 ++++ DumpTruck/MovementStrategy/Status.cs | 15 ++ 16 files changed, 913 insertions(+), 341 deletions(-) delete mode 100644 DumpTruck/DrawingDumpTruck.cs create mode 100644 DumpTruck/DrawingObjects/DrawingDumpTruck.cs create mode 100644 DumpTruck/DrawingObjects/DrawingTruck.cs create mode 100644 DumpTruck/Entities/EntityDumpTruck.cs create mode 100644 DumpTruck/Entities/EntityTruck.cs delete mode 100644 DumpTruck/EntityDumpTruck.cs create mode 100644 DumpTruck/MovementStrategy/AbstractStrategy.cs create mode 100644 DumpTruck/MovementStrategy/DrawningObjectTruck.cs create mode 100644 DumpTruck/MovementStrategy/IMoveableObject.cs create mode 100644 DumpTruck/MovementStrategy/MoveToBorder.cs create mode 100644 DumpTruck/MovementStrategy/MoveToCenter.cs create mode 100644 DumpTruck/MovementStrategy/ObjectParameters.cs create mode 100644 DumpTruck/MovementStrategy/Status.cs diff --git a/DumpTruck/Direction.cs b/DumpTruck/Direction.cs index a576395..b9a3a79 100644 --- a/DumpTruck/Direction.cs +++ b/DumpTruck/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace DumpTruck { - public enum Direction + public enum Direction { /// Вверх /// diff --git a/DumpTruck/DrawingDumpTruck.cs b/DumpTruck/DrawingDumpTruck.cs deleted file mode 100644 index afc6ef0..0000000 --- a/DumpTruck/DrawingDumpTruck.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck -{ - public class DrawingDumpTruck - { - /// - /// Класс-сущность - /// - public EntityDumpTruck? EntityDumpTruck { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки самосвала - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки самосвала - /// - private int _startPosY; - /// - /// Ширина прорисовки самосвала - /// - private readonly int _truckWidth = 160; - /// - /// Высота прорисовки самосвала - /// - private readonly int _truckHeight = 90; - - - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Основной цвет самосвала - /// Признак наличия тента - /// Признак наличия кузова - /// Цвет кузова - /// Цвет тента - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureHeight < _truckHeight || _pictureWidth < _truckWidth) return false; - EntityDumpTruck = new EntityDumpTruck(); - EntityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor); - return true; - } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; } - if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; } - _startPosX = x; - _startPosY = y; - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(Direction direction) - { - if (EntityDumpTruck == null) - { - return; - } - switch (direction) - { - //влево - case Direction.Left: - if (_startPosX - EntityDumpTruck.Step > 0) - { - _startPosX -= (int)EntityDumpTruck.Step; - } - break; - //вверх - case Direction.Up: - if (_startPosY - EntityDumpTruck.Step > 0) - { - _startPosY -= (int)EntityDumpTruck.Step; - } - break; - // вправо - case Direction.Right: - if (_startPosX + _truckWidth + EntityDumpTruck.Step < _pictureWidth) - { - _startPosX += (int)EntityDumpTruck.Step; - } - break; - //вниз - case Direction.Down: - if (_startPosY + _truckHeight + EntityDumpTruck.Step < _pictureHeight) - { - _startPosY += (int)EntityDumpTruck.Step; - } - break; - } - } - - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityDumpTruck == null) - { - return; - } - Brush brush = new SolidBrush(EntityDumpTruck.BodyColor); - g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10); - g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40); - g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40); - g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40); - g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40); - if (EntityDumpTruck.DumpBox) - { - Brush brDumpBox = new SolidBrush(EntityDumpTruck.DumpBoxColor); - Point point1 = new Point(_startPosX + 20, _startPosY); - Point point2 = new Point(_startPosX + 120, _startPosY); - Point point3 = new Point(_startPosX + 100, _startPosY + 39); - Point point4 = new Point(_startPosX, _startPosY + 39); - Point[] dumpBoxPoints = { point1, point2, point3, point4}; - g.FillPolygon(brDumpBox, dumpBoxPoints); - } - if (EntityDumpTruck.DumpBox && EntityDumpTruck.Tent) - { - Brush brTent = new SolidBrush(EntityDumpTruck.TentColor); - Point point1 = new Point(_startPosX + 15, _startPosY); - Point point2 = new Point(_startPosX + 120, _startPosY); - Point point3 = new Point(_startPosX + 115, _startPosY + 10); - Point point4 = new Point(_startPosX + 10, _startPosY + 10); - Point[] tentPoints = { point1, point2, point3, point4 }; - g.FillPolygon(brTent, tentPoints); - } - } - } -} \ No newline at end of file diff --git a/DumpTruck/DrawingObjects/DrawingDumpTruck.cs b/DumpTruck/DrawingObjects/DrawingDumpTruck.cs new file mode 100644 index 0000000..eb596c1 --- /dev/null +++ b/DumpTruck/DrawingObjects/DrawingDumpTruck.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + + +namespace DumpTruck.DrawingObjects +{ + public class DrawingDumpTruck : DrawingTruck + { + public DrawingDumpTruck(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height) + : base(speed, weight, bodyColor, width, height, 160, 90) + { + if (EntityTruck != null) + { + EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityTruck is not EntityDumpTruck dumpTruck) + { + return; + } + + base.DrawTransport(g); + + if (dumpTruck.DumpBox) + { + Brush brDumpBox = new SolidBrush(dumpTruck.DumpBoxColor); + Point point1 = new Point(_startPosX + 20, _startPosY); + Point point2 = new Point(_startPosX + 120, _startPosY); + Point point3 = new Point(_startPosX + 100, _startPosY + 39); + Point point4 = new Point(_startPosX, _startPosY + 39); + Point[] dumpBoxPoints = { point1, point2, point3, point4 }; + g.FillPolygon(brDumpBox, dumpBoxPoints); + } + if (dumpTruck.DumpBox && dumpTruck.Tent) + { + Brush brTent = new SolidBrush(dumpTruck.TentColor); + Point point1 = new Point(_startPosX + 15, _startPosY); + Point point2 = new Point(_startPosX + 120, _startPosY); + Point point3 = new Point(_startPosX + 115, _startPosY + 10); + Point point4 = new Point(_startPosX + 10, _startPosY + 10); + Point[] tentPoints = { point1, point2, point3, point4 }; + g.FillPolygon(brTent, tentPoints); + } + } + } +} diff --git a/DumpTruck/DrawingObjects/DrawingTruck.cs b/DumpTruck/DrawingObjects/DrawingTruck.cs new file mode 100644 index 0000000..d8e7566 --- /dev/null +++ b/DumpTruck/DrawingObjects/DrawingTruck.cs @@ -0,0 +1,190 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + +namespace DumpTruck.DrawingObjects +{ + public class DrawingTruck + { + /// + /// Класс-сущность + /// + public EntityTruck? EntityTruck { get; protected set; } + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки грузовика + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки грузовика + /// + protected int _startPosY; + /// + /// Ширина прорисовки грузовика + /// + protected readonly int _truckWidth = 160; + /// + /// Высота прорисовки грузовика + /// + protected readonly int _truckHeight = 90; + + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _truckWidth; + /// + /// Высота объекта + /// + public int GetHeight => _truckHeight; + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < _truckWidth || height < _truckHeight) return; + _pictureWidth = width; + _pictureHeight = height; + EntityTruck = new EntityTruck(speed, weight, bodyColor); + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки грузовика + /// Высота прорисовки грузовика + protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight) + { + if (width < truckWidth || height < truckHeight) return; + _pictureWidth = width; + _pictureHeight = height; + _truckWidth = truckWidth; + _truckHeight = truckHeight; + EntityTruck = new EntityTruck(speed, weight, bodyColor); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x + _truckWidth > _pictureWidth) x = 0; + if (y < 0 || y + _truckHeight > _pictureHeight) y = 0; + _startPosX = x; + _startPosY = y; + } + + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(Direction direction) + { + if (EntityTruck == null) + { + return false; + } + return direction switch + { + //влево + Direction.Left => _startPosX - EntityTruck.Step > 0, + //вверх + Direction.Up => _startPosY - EntityTruck.Step > 0, + // вправо + Direction.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth, + //вниз + Direction.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight, + _ => false, + }; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || EntityTruck == null) + { + return; + } + switch (direction) + { + //влево + case Direction.Left: + if (_startPosX - EntityTruck.Step > 0) + { + _startPosX -= (int)EntityTruck.Step; + } + break; + //вверх + case Direction.Up: + if (_startPosY - EntityTruck.Step > 0) + { + _startPosY -= (int)EntityTruck.Step; + } + break; + // вправо + case Direction.Right: + if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth) + { + _startPosX += (int)EntityTruck.Step; + } + break; + //вниз + case Direction.Down: + if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight) + { + _startPosY += (int)EntityTruck.Step; + } + break; + } + } + + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityTruck == null) + { + return; + } + Brush brush = new SolidBrush(EntityTruck.BodyColor); + g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10); + g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40); + g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40); + g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40); + g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40); + } + } +} diff --git a/DumpTruck/Entities/EntityDumpTruck.cs b/DumpTruck/Entities/EntityDumpTruck.cs new file mode 100644 index 0000000..514226c --- /dev/null +++ b/DumpTruck/Entities/EntityDumpTruck.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Entities +{ + public class EntityDumpTruck : EntityTruck + { + /// + /// Признак (опция) наличия тента + /// + public bool Tent { get; private set; } + /// + /// Признак (опция) наличия кузова + /// + public bool DumpBox { get; private set; } + /// + /// Цвет кузова + /// + public Color DumpBoxColor { get; private set; } + /// + /// Цвет тента + /// + public Color TentColor { get; private set; } + /// + /// Конструктор + /// + /// Скорость + /// Вес самосвала + /// Основной цвет самосвала + /// Признак наличия тента + /// Признак наличия кузова + /// Цвет кузова + /// Цвет тента + + public EntityDumpTruck(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor) : base(speed, weight, bodyColor) + { + Tent = tent; + DumpBox = dumpBox; + DumpBoxColor = dumpBoxColor; + TentColor = tentColor; + } + } +} diff --git a/DumpTruck/Entities/EntityTruck.cs b/DumpTruck/Entities/EntityTruck.cs new file mode 100644 index 0000000..488d044 --- /dev/null +++ b/DumpTruck/Entities/EntityTruck.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Entities +{ + public class EntityTruck + { + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public double Weight { get; private set; } + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + /// + /// Шаг + /// + public double Step => (double)Speed * 100 / Weight; + + /// + /// Конструктор + /// + /// Скорость + /// Вес грузовика + /// Основной цвет грузовика + public EntityTruck(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/DumpTruck/EntityDumpTruck.cs b/DumpTruck/EntityDumpTruck.cs deleted file mode 100644 index f9b054a..0000000 --- a/DumpTruck/EntityDumpTruck.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck -{ - public class EntityDumpTruck - { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Признак (опция) наличия тента - /// - public bool Tent { get; private set; } - /// - /// Признак (опция) наличия кузова - /// - public bool DumpBox { get; private set; } - /// - /// Цвет кузова - /// - public Color DumpBoxColor { get; private set; } - /// - /// Цвет тента - /// - public Color TentColor { get; private set; } - /// - /// Шаг - /// - public double Step => (double)Speed * 100 / Weight; - - /// - /// Инициализация полей объекта-класса самосвала - /// - /// Скорость - /// Вес самосвала - /// Основной цвет самосвала - /// Признак наличия тента - /// Признак наличия кузова - /// Цвет кузова - /// Цвет тента - public void Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - Tent = tent; - DumpBox = dumpBox; - DumpBoxColor = dumpBoxColor; - TentColor = tentColor; - } - } -} \ No newline at end of file diff --git a/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/FormDumpTruck.Designer.cs index 6482021..f4ff2da 100644 --- a/DumpTruck/FormDumpTruck.Designer.cs +++ b/DumpTruck/FormDumpTruck.Designer.cs @@ -28,107 +28,152 @@ /// private void InitializeComponent() { - pictureBoxDumpTruck = new PictureBox(); - Create = new Button(); - buttonLeft = new Button(); - buttonUp = new Button(); - buttonRight = new Button(); - buttonDown = new Button(); - ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit(); - SuspendLayout(); - // - // pictureBoxDumpTruck - // - pictureBoxDumpTruck.Dock = DockStyle.Fill; - pictureBoxDumpTruck.Location = new Point(0, 0); - pictureBoxDumpTruck.Name = "pictureBoxDumpTruck"; - pictureBoxDumpTruck.Size = new Size(800, 450); - pictureBoxDumpTruck.TabIndex = 0; - pictureBoxDumpTruck.TabStop = false; - // - // Create - // - Create.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - Create.Location = new Point(12, 415); - Create.Name = "Create"; - Create.Size = new Size(75, 23); - Create.TabIndex = 1; - Create.Text = "Создать"; - Create.UseVisualStyleBackColor = true; - Create.Click += Create_Click; - // - // buttonLeft - // - buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonLeft.BackgroundImage = Properties.Resources.left; - buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(686, 408); - buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); - buttonLeft.TabIndex = 2; - buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += ButtonMove_Click; - // - // buttonUp - // - buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonUp.BackgroundImage = Properties.Resources.up; - buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(722, 372); - buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); - buttonUp.TabIndex = 3; - buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += ButtonMove_Click; - // - // buttonRight - // - buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonRight.BackgroundImage = Properties.Resources.right; - buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(758, 408); - buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); - buttonRight.TabIndex = 4; - buttonRight.UseVisualStyleBackColor = true; - buttonRight.Click += ButtonMove_Click; - // - // buttonDown - // - buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonDown.BackgroundImage = Properties.Resources.down; - buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(722, 408); - buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); - buttonDown.TabIndex = 5; - buttonDown.UseVisualStyleBackColor = true; - buttonDown.Click += ButtonMove_Click; - // - // FormDumpTruck - // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); - Controls.Add(buttonDown); - Controls.Add(buttonRight); - Controls.Add(buttonUp); - Controls.Add(buttonLeft); - Controls.Add(Create); - Controls.Add(pictureBoxDumpTruck); - Name = "FormDumpTruck"; - Text = "FormDumpTruck"; - ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).EndInit(); - ResumeLayout(false); + this.pictureBoxDumpTruck = new System.Windows.Forms.PictureBox(); + this.ButtonCreateDumpTruck = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.ButtonCreateTruck = new System.Windows.Forms.Button(); + this.ButtonStep = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).BeginInit(); + this.SuspendLayout(); + // + // pictureBoxDumpTruck + // + this.pictureBoxDumpTruck.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxDumpTruck.Location = new System.Drawing.Point(0, 0); + this.pictureBoxDumpTruck.Name = "pictureBoxDumpTruck"; + this.pictureBoxDumpTruck.Size = new System.Drawing.Size(800, 450); + this.pictureBoxDumpTruck.TabIndex = 0; + this.pictureBoxDumpTruck.TabStop = false; + // + // ButtonCreateDumpTruck + // + this.ButtonCreateDumpTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.ButtonCreateDumpTruck.Location = new System.Drawing.Point(12, 408); + this.ButtonCreateDumpTruck.Name = "ButtonCreateDumpTruck"; + this.ButtonCreateDumpTruck.Size = new System.Drawing.Size(130, 30); + this.ButtonCreateDumpTruck.TabIndex = 1; + this.ButtonCreateDumpTruck.Text = "Создать самосвал"; + this.ButtonCreateDumpTruck.UseVisualStyleBackColor = true; + this.ButtonCreateDumpTruck.Click += new System.EventHandler(this.ButtonCreateDumpTruck_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::DumpTruck.Properties.Resources.left; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonLeft.Location = new System.Drawing.Point(686, 408); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 2; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::DumpTruck.Properties.Resources.up; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonUp.Location = new System.Drawing.Point(722, 372); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 3; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::DumpTruck.Properties.Resources.right; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonRight.Location = new System.Drawing.Point(758, 408); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 4; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::DumpTruck.Properties.Resources.down; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonDown.Location = new System.Drawing.Point(722, 408); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 5; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // comboBoxStrategy + // + this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "0", + "1"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(667, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23); + this.comboBoxStrategy.TabIndex = 6; + // + // ButtonCreateTruck + // + this.ButtonCreateTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.ButtonCreateTruck.Location = new System.Drawing.Point(148, 408); + this.ButtonCreateTruck.Name = "ButtonCreateTruck"; + this.ButtonCreateTruck.Size = new System.Drawing.Size(130, 30); + this.ButtonCreateTruck.TabIndex = 7; + this.ButtonCreateTruck.Text = "Создать грузовик"; + this.ButtonCreateTruck.UseVisualStyleBackColor = true; + this.ButtonCreateTruck.Click += new System.EventHandler(this.ButtonCreateTruck_Click); + // + // ButtonStep + // + this.ButtonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonStep.Location = new System.Drawing.Point(713, 41); + this.ButtonStep.Name = "ButtonStep"; + this.ButtonStep.Size = new System.Drawing.Size(75, 23); + this.ButtonStep.TabIndex = 8; + this.ButtonStep.Text = "Шаг"; + this.ButtonStep.UseVisualStyleBackColor = true; + this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click); + // + // FormDumpTruck + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonStep); + this.Controls.Add(this.ButtonCreateTruck); + this.Controls.Add(this.comboBoxStrategy); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.ButtonCreateDumpTruck); + this.Controls.Add(this.pictureBoxDumpTruck); + this.Name = "FormDumpTruck"; + this.Text = "FormDumpTruck"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).EndInit(); + this.ResumeLayout(false); + } #endregion private PictureBox pictureBoxDumpTruck; - private Button Create; + private Button ButtonCreateDumpTruck; private Button buttonLeft; private Button buttonUp; private Button buttonRight; private Button buttonDown; - } + private ComboBox comboBoxStrategy; + private Button ButtonCreateTruck; + private Button ButtonStep; + } } \ No newline at end of file diff --git a/DumpTruck/FormDumpTruck.cs b/DumpTruck/FormDumpTruck.cs index 0ee28f0..b74fd94 100644 --- a/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/FormDumpTruck.cs @@ -1,3 +1,6 @@ +using DumpTruck.DrawingObjects; +using DumpTruck.MovementStrategy; + namespace DumpTruck { public partial class FormDumpTruck : Form @@ -7,39 +10,26 @@ namespace DumpTruck InitializeComponent(); } - private DrawingDumpTruck? _drawingDumpTruck; + private DrawingTruck? _drawingTruck; + + private AbstractStrategy? _abstractStrategy; private void Draw() { - if (_drawingDumpTruck == null) + if (_drawingTruck == null) { return; } Bitmap bmp = new(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingDumpTruck.DrawTransport(gr); + _drawingTruck.DrawTransport(gr); pictureBoxDumpTruck.Image = bmp; } - - private void Create_Click(object sender, EventArgs e) - { - Random random = new(); - _drawingDumpTruck = new DrawingDumpTruck(); - _drawingDumpTruck.Init(random.Next(100, 300), random.Next(1000, 3000), - 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)), - 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)), - pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); - _drawingDumpTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); - } - private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawingDumpTruck == null) + if (_drawingTruck == null) { return; } @@ -47,19 +37,78 @@ namespace DumpTruck switch (name) { case "buttonUp": - _drawingDumpTruck.MoveTransport(Direction.Up); + _drawingTruck.MoveTransport(Direction.Up); break; case "buttonDown": - _drawingDumpTruck.MoveTransport(Direction.Down); + _drawingTruck.MoveTransport(Direction.Down); break; case "buttonLeft": - _drawingDumpTruck.MoveTransport(Direction.Left); + _drawingTruck.MoveTransport(Direction.Left); break; case "buttonRight": - _drawingDumpTruck.MoveTransport(Direction.Right); + _drawingTruck.MoveTransport(Direction.Right); break; } Draw(); } - } + + private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000), + 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)), + 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)), + pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); + _drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void ButtonCreateTruck_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); + _drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void ButtonStep_Click(object sender, EventArgs e) + { + if (_drawingTruck == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectTruck(_drawingTruck), pictureBoxDumpTruck.Width, + pictureBoxDumpTruck.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } + } } \ No newline at end of file diff --git a/DumpTruck/MovementStrategy/AbstractStrategy.cs b/DumpTruck/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..f5e3548 --- /dev/null +++ b/DumpTruck/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private Status _state = Status.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public Status GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(Direction.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(Direction.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(Direction.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(Direction.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/DumpTruck/MovementStrategy/DrawningObjectTruck.cs b/DumpTruck/MovementStrategy/DrawningObjectTruck.cs new file mode 100644 index 0000000..92133d5 --- /dev/null +++ b/DumpTruck/MovementStrategy/DrawningObjectTruck.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawingObjects; + +namespace DumpTruck.MovementStrategy +{ + public class DrawningObjectTruck : IMoveableObject + { + private readonly DrawingTruck? _drawingTruck = null; + public DrawningObjectTruck(DrawingTruck drawningCar) + { + _drawingTruck = drawningCar; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawingTruck == null || _drawingTruck.EntityTruck == + null) + { + return null; + } + return new ObjectParameters(_drawingTruck.GetPosX, + _drawingTruck.GetPosY, _drawingTruck.GetWidth, _drawingTruck.GetHeight); + } + } + public int GetStep => (int)(_drawingTruck?.EntityTruck?.Step ?? 0); + public bool CheckCanMove(Direction direction) => + _drawingTruck?.CanMove(direction) ?? false; + public void MoveObject(Direction direction) => + _drawingTruck?.MoveTransport(direction); + } +} diff --git a/DumpTruck/MovementStrategy/IMoveableObject.cs b/DumpTruck/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..fd41031 --- /dev/null +++ b/DumpTruck/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(Direction direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); + } +} diff --git a/DumpTruck/MovementStrategy/MoveToBorder.cs b/DumpTruck/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..c9f3f13 --- /dev/null +++ b/DumpTruck/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX < 0) + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY < 0) + { + MoveDown(); + } + } + } + } +} diff --git a/DumpTruck/MovementStrategy/MoveToCenter.cs b/DumpTruck/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..ce928a2 --- /dev/null +++ b/DumpTruck/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/DumpTruck/MovementStrategy/ObjectParameters.cs b/DumpTruck/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..ac15df7 --- /dev/null +++ b/DumpTruck/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + public class ObjectParameters + { + 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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/DumpTruck/MovementStrategy/Status.cs b/DumpTruck/MovementStrategy/Status.cs new file mode 100644 index 0000000..ed02976 --- /dev/null +++ b/DumpTruck/MovementStrategy/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}