diff --git a/DumpTruck/DumpTruck/AbstractStrategy.cs b/DumpTruck/DumpTruck/AbstractStrategy.cs new file mode 100644 index 0000000..384cdd5 --- /dev/null +++ b/DumpTruck/DumpTruck/AbstractStrategy.cs @@ -0,0 +1,130 @@ +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(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.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(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/DumpTruck/DumpTruck/DirectionType.cs b/DumpTruck/DumpTruck/DirectionType.cs new file mode 100644 index 0000000..19e672e --- /dev/null +++ b/DumpTruck/DumpTruck/DirectionType.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck +{ + public enum DirectionType + { + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 + } +} diff --git a/DumpTruck/DumpTruck/DrawningDumpTruck.cs b/DumpTruck/DumpTruck/DrawningDumpTruck.cs new file mode 100644 index 0000000..6ecb5ee --- /dev/null +++ b/DumpTruck/DumpTruck/DrawningDumpTruck.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + +namespace DumpTruck.DrawningObjects +{ + internal class DrawningDumpTruck : DrawningTruck + { + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Цвет кузова + /// Дополнительный цвет + /// Ширина картинки + /// Высота картинки + /// true - объект создан, false - проверка не пройдена, + public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color + additionalColor, bool trailer, int width, int height) : + base (speed, weight, bodyColor, additionalColor, width, height,110, 85) + { + if (EntityTruck != null) + { + EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, trailer); + } + } + + public override void DrawTransport(Graphics g) + { + if (EntityTruck is not EntityDumpTruck dumpTruck + ) + { + return; + } + base.DrawTransport(g); + if (dumpTruck.Trailer) + { + //прицеп + Brush trailer = new SolidBrush(Color.Black); + g.FillRectangle(trailer, _startPosX, _startPosY, 5, 38); + g.FillRectangle(trailer, _startPosX + 5, _startPosY + 33, 70, 5); + g.FillRectangle(trailer, _startPosX + 70, _startPosY, 5, 38); + } + } + } +} diff --git a/DumpTruck/DumpTruck/DrawningObjectTruck.cs b/DumpTruck/DumpTruck/DrawningObjectTruck.cs new file mode 100644 index 0000000..2a1ae9a --- /dev/null +++ b/DumpTruck/DumpTruck/DrawningObjectTruck.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; + + +namespace DumpTruck.MovementStrategy +{ + internal class DrawningObjectTruck : IMoveableObject + + { + private readonly DrawningTruck? _drawningTruck = null; + public DrawningObjectTruck(DrawningTruck drawningTruck) + { + _drawningTruck = drawningTruck; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningTruck == null || _drawningTruck.EntityTruck == null) + { + return null; + } + return new ObjectParameters(_drawningTruck.GetPosX, _drawningTruck.GetPosY, _drawningTruck.GetWidth, _drawningTruck.GetHeight); + } + } + public int GetStep => (int)(_drawningTruck?.EntityTruck?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) => _drawningTruck?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawningTruck?.MoveTransport(direction); + } +} diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs new file mode 100644 index 0000000..df0a319 --- /dev/null +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + +namespace DumpTruck.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningTruck + { + /// + /// Класс-сущность + /// + public EntityTruck? EntityTruck { get; protected set; } + /// + /// Ширина окна + /// + public int _pictureWidth; + /// + /// Высота окна + /// + public int _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + protected int _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + protected readonly int _truckWidth = 110; + /// + /// Высота прорисовки автомобиля + /// + protected readonly int _truckHeight = 85; + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _truckWidth; + /// + /// Высота объекта + /// + public int GetHeight => _truckHeight; + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningTruck(int speed, double weight, Color bodyColor,Color additionalColor, int width, int height) + { + // TODO: Продумать проверки + if (width < _truckWidth || height < _truckHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityTruck = new EntityTruck(speed, weight, bodyColor, additionalColor); + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningTruck(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height, int truckWidth, int truckHeight) + { + // TODO: Продумать проверки + _pictureWidth = width; + _pictureHeight = height; + _truckWidth = truckWidth; + _truckHeight = truckHeight; + EntityTruck = new EntityTruck(speed, weight, bodyColor, additionalColor); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x + _truckWidth > _pictureWidth) + { + x = Math.Max(0, _pictureWidth - _truckWidth); + } + if (y < 0 || y + _truckHeight > _pictureHeight) + { + y = Math.Max(0, _pictureHeight - _truckHeight); + } + _startPosX = x; + _startPosY = y; + } + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityTruck == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityTruck.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityTruck.Step > 0, + // вправо + DirectionType.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight, + _ => false, + }; + } + + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityTruck == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX - EntityTruck.Step > 0) + { + _startPosX -= (int)EntityTruck.Step; + } + break; + //вверх + case DirectionType.Up: + if (_startPosY - EntityTruck.Step > 0) + { + _startPosY -= (int)EntityTruck.Step; + } + break; + // вправо + case DirectionType.Right: + if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth) + { + _startPosX += (int)EntityTruck.Step; + } + break; + //вниз + case DirectionType.Down: + if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight) + { + _startPosY += (int)EntityTruck.Step; + } + break; + } + } + public virtual void DrawTransport(Graphics g) + { + if (EntityTruck == null) + { + return; + } + Pen pen = new(Color.Black); + //грани + g.DrawRectangle(pen, _startPosX + 80, _startPosY, 30, 40); + g.DrawRectangle(pen, _startPosX, _startPosY + 40, 110, 20); + //кузов + Brush br = new SolidBrush(EntityTruck.BodyColor); + g.FillRectangle(br, _startPosX + 81, _startPosY + 1, 29, 40); + Brush br1 = new SolidBrush(EntityTruck.AdditionalColor); + g.FillRectangle(br1, _startPosX + 1, _startPosY + 41, 109, 19); + //колеса + Brush wheels = new SolidBrush(Color.Black); + g.FillEllipse(wheels, _startPosX, _startPosY + 63, 25, 25); + g.FillEllipse(wheels, _startPosX + 25, _startPosY + 63, 25, 25); + g.FillEllipse(wheels, _startPosX + 85, _startPosY + 63, 25, 25); + } + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/EntityDumpTruck.cs b/DumpTruck/DumpTruck/EntityDumpTruck.cs new file mode 100644 index 0000000..5536a41 --- /dev/null +++ b/DumpTruck/DumpTruck/EntityDumpTruck.cs @@ -0,0 +1,28 @@ +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 Trailer { get; private set; } + + /// Инициализация полей объекта-класса + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + /// Дополнительный цвет + public EntityDumpTruck(int speed, double weight, Color bodyColor, Color + additionalColor, bool trailer) : base (speed,weight, bodyColor,additionalColor) + { + Trailer = trailer; + } + } +} diff --git a/DumpTruck/DumpTruck/EntityTruck.cs b/DumpTruck/DumpTruck/EntityTruck.cs new file mode 100644 index 0000000..bef2ece --- /dev/null +++ b/DumpTruck/DumpTruck/EntityTruck.cs @@ -0,0 +1,48 @@ +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 Color AdditionalColor { get; private set; } + /// + /// Шаг перемещения автомобиля + /// + public double Step => (double)Speed * 100 / Weight; + /// + /// Конструктор с параметрами + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + public EntityTruck(int speed, double weight, Color bodyColor,Color additionalColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + } + } +} + diff --git a/DumpTruck/DumpTruck/Form1.Designer.cs b/DumpTruck/DumpTruck/Form1.Designer.cs deleted file mode 100644 index 073cfce..0000000 --- a/DumpTruck/DumpTruck/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace DumpTruck -{ - partial class Form1 - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; - } - - #endregion - } -} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/Form1.cs b/DumpTruck/DumpTruck/Form1.cs deleted file mode 100644 index 2326120..0000000 --- a/DumpTruck/DumpTruck/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace DumpTruck -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/Form1.resx b/DumpTruck/DumpTruck/Form1.resx deleted file mode 100644 index 1af7de1..0000000 --- a/DumpTruck/DumpTruck/Form1.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs new file mode 100644 index 0000000..108c2ad --- /dev/null +++ b/DumpTruck/DumpTruck/FormDumpTruck.Designer.cs @@ -0,0 +1,172 @@ +namespace DumpTruck +{ + partial class FormDumpTruck + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.pictureBoxDumpTruck = new System.Windows.Forms.PictureBox(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.Create_Truck_Button = new System.Windows.Forms.Button(); + this.CreateDumpTruckButton = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.Step_Button = 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(884, 461); + this.pictureBoxDumpTruck.TabIndex = 0; + this.pictureBoxDumpTruck.TabStop = false; + // + // buttonUp + // + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonUp.Location = new System.Drawing.Point(795, 374); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(24, 26); + this.buttonUp.TabIndex = 2; + this.buttonUp.Text = "1"; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.BackColor = System.Drawing.SystemColors.ControlDark; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonLeft.Location = new System.Drawing.Point(765, 406); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(24, 26); + this.buttonLeft.TabIndex = 3; + this.buttonLeft.Text = "3"; + this.buttonLeft.UseVisualStyleBackColor = false; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonRight.Location = new System.Drawing.Point(825, 407); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(24, 26); + this.buttonRight.TabIndex = 4; + this.buttonRight.Text = "4"; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonDown.Location = new System.Drawing.Point(795, 406); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(24, 26); + this.buttonDown.TabIndex = 5; + this.buttonDown.Text = "2"; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // Create_Truck_Button + // + this.Create_Truck_Button.Location = new System.Drawing.Point(12, 392); + this.Create_Truck_Button.Name = "Create_Truck_Button"; + this.Create_Truck_Button.Size = new System.Drawing.Size(123, 23); + this.Create_Truck_Button.TabIndex = 6; + this.Create_Truck_Button.Text = "Create Truck"; + this.Create_Truck_Button.UseVisualStyleBackColor = true; + this.Create_Truck_Button.Click += new System.EventHandler(this.Create_Truck_Button_Click); + // + // CreateDumpTruckButton + // + this.CreateDumpTruckButton.Location = new System.Drawing.Point(166, 392); + this.CreateDumpTruckButton.Name = "CreateDumpTruckButton"; + this.CreateDumpTruckButton.Size = new System.Drawing.Size(168, 23); + this.CreateDumpTruckButton.TabIndex = 7; + this.CreateDumpTruckButton.Text = "Create DumpTruck"; + this.CreateDumpTruckButton.UseVisualStyleBackColor = true; + this.CreateDumpTruckButton.Click += new System.EventHandler(this.CreateDumpTruckButton_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[] { + "MoveToCenter", + "MoveToBorder"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(751, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23); + this.comboBoxStrategy.TabIndex = 8; + // + // Step_Button + // + this.Step_Button.Location = new System.Drawing.Point(797, 41); + this.Step_Button.Name = "Step_Button"; + this.Step_Button.Size = new System.Drawing.Size(75, 23); + this.Step_Button.TabIndex = 9; + this.Step_Button.Text = "Step"; + this.Step_Button.UseVisualStyleBackColor = true; + this.Step_Button.Click += new System.EventHandler(this.Step_Button_Click); + // + // FormDumpTruck + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(884, 461); + this.Controls.Add(this.Step_Button); + this.Controls.Add(this.comboBoxStrategy); + this.Controls.Add(this.CreateDumpTruckButton); + this.Controls.Add(this.Create_Truck_Button); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonUp); + 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 buttonUp; + private Button buttonLeft; + private Button buttonRight; + private Button buttonDown; + private Button Create_Truck_Button; + private Button CreateDumpTruckButton; + private ComboBox comboBoxStrategy; + private Button Step_Button; + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormDumpTruck.cs b/DumpTruck/DumpTruck/FormDumpTruck.cs new file mode 100644 index 0000000..d7c5ead --- /dev/null +++ b/DumpTruck/DumpTruck/FormDumpTruck.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using DumpTruck.DrawningObjects; +using DumpTruck.MovementStrategy; + + +namespace DumpTruck +{ + public partial class FormDumpTruck : Form + { + private DrawningTruck? _drawningTruck; + + private AbstractStrategy? _abstractStrategy; + + public FormDumpTruck() + { + InitializeComponent(); + } + private void Draw() + { + if (_drawningTruck == null) + { + return; + } + Bitmap bmp = new(pictureBoxDumpTruck.Width, + pictureBoxDumpTruck.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningTruck.DrawTransport(gr); + pictureBoxDumpTruck.Image = bmp; + } + private void Create_Truck_Button_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningTruck = new DrawningTruck(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)), + pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); + _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void CreateDumpTruckButton_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningTruck = new DrawningDumpTruck(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(1), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); + _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + /// + /// Обработка нажатия кнопки "Создать" + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningTruck == null) + { + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + switch (name) + { + case "buttonUp": + _drawningTruck.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + _drawningTruck.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + _drawningTruck.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + _drawningTruck.MoveTransport(DirectionType.Right); + break; + } + Draw(); + } + + private void Step_Button_Click(object sender, EventArgs e) + { + if (_drawningTruck == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectTruck(_drawningTruck), pictureBoxDumpTruck.Width, + pictureBoxDumpTruck.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } + } +} diff --git a/DumpTruck/DumpTruck/FormDumpTruck.resx b/DumpTruck/DumpTruck/FormDumpTruck.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/DumpTruck/DumpTruck/FormDumpTruck.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/DumpTruck/DumpTruck/IMoveableObject.cs b/DumpTruck/DumpTruck/IMoveableObject.cs new file mode 100644 index 0000000..0e59b6e --- /dev/null +++ b/DumpTruck/DumpTruck/IMoveableObject.cs @@ -0,0 +1,36 @@ +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(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} + diff --git a/DumpTruck/DumpTruck/MoveToBorder.cs b/DumpTruck/DumpTruck/MoveToBorder.cs new file mode 100644 index 0000000..b1d25e4 --- /dev/null +++ b/DumpTruck/DumpTruck/MoveToBorder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; + +namespace DumpTruck.MovementStrategy +{ + internal 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.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/DumpTruck/DumpTruck/MoveToCenter.cs b/DumpTruck/DumpTruck/MoveToCenter.cs new file mode 100644 index 0000000..db1cc3b --- /dev/null +++ b/DumpTruck/DumpTruck/MoveToCenter.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawningObjects; + +namespace DumpTruck.MovementStrategy +{ + internal 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/DumpTruck/ObjectParameters.cs b/DumpTruck/DumpTruck/ObjectParameters.cs new file mode 100644 index 0000000..d084727 --- /dev/null +++ b/DumpTruck/DumpTruck/ObjectParameters.cs @@ -0,0 +1,58 @@ +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/DumpTruck/Program.cs b/DumpTruck/DumpTruck/Program.cs index a716913..b619d5a 100644 --- a/DumpTruck/DumpTruck/Program.cs +++ b/DumpTruck/DumpTruck/Program.cs @@ -11,7 +11,7 @@ namespace DumpTruck // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + Application.Run(new FormDumpTruck()); } } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/Status.cs b/DumpTruck/DumpTruck/Status.cs new file mode 100644 index 0000000..99b69a0 --- /dev/null +++ b/DumpTruck/DumpTruck/Status.cs @@ -0,0 +1,19 @@ +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 + } +} +