From 467726e7d3a0e388f10a1466be5c0a653081aea7 Mon Sep 17 00:00:00 2001 From: sardq Date: Sat, 21 Oct 2023 21:47:58 +0400 Subject: [PATCH] =?UTF-8?q?2=20=D0=BB=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HoistingCrane/AbstractStrategy.cs | 88 +++++++++++++++ .../HoistingCrane/AdditionEntityCrane.cs | 18 +++ .../HoistingCrane/AdditionalDrawingCrane.cs | 41 +++++++ .../HoistingCrane/CraneVisual.Designer.cs | 46 +++++++- HoistingCrane/HoistingCrane/CraneVisual.cs | 50 ++++++++- HoistingCrane/HoistingCrane/Direction.cs | 8 +- HoistingCrane/HoistingCrane/DrawingCrane.cs | 103 ++++++++++++------ .../HoistingCrane/DrawningObjectsCrane.cs | 27 +++++ HoistingCrane/HoistingCrane/EntityCrane.cs | 21 +--- .../HoistingCrane/IMoveableObject.cs | 13 +++ HoistingCrane/HoistingCrane/MoveToBorder.cs | 47 ++++++++ HoistingCrane/HoistingCrane/MoveToCenter.cs | 47 ++++++++ .../HoistingCrane/ObjectParameters.cs | 42 +++++++ HoistingCrane/HoistingCrane/Status.cs | 10 ++ 14 files changed, 500 insertions(+), 61 deletions(-) create mode 100644 HoistingCrane/HoistingCrane/AbstractStrategy.cs create mode 100644 HoistingCrane/HoistingCrane/AdditionEntityCrane.cs create mode 100644 HoistingCrane/HoistingCrane/AdditionalDrawingCrane.cs create mode 100644 HoistingCrane/HoistingCrane/DrawningObjectsCrane.cs create mode 100644 HoistingCrane/HoistingCrane/IMoveableObject.cs create mode 100644 HoistingCrane/HoistingCrane/MoveToBorder.cs create mode 100644 HoistingCrane/HoistingCrane/MoveToCenter.cs create mode 100644 HoistingCrane/HoistingCrane/ObjectParameters.cs create mode 100644 HoistingCrane/HoistingCrane/Status.cs diff --git a/HoistingCrane/HoistingCrane/AbstractStrategy.cs b/HoistingCrane/HoistingCrane/AbstractStrategy.cs new file mode 100644 index 0000000..1e3b5b0 --- /dev/null +++ b/HoistingCrane/HoistingCrane/AbstractStrategy.cs @@ -0,0 +1,88 @@ +namespace HoistingCrane.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(); + } + + // Перемещение влевo + // Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + // Перемещение вправо 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; + } + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/AdditionEntityCrane.cs b/HoistingCrane/HoistingCrane/AdditionEntityCrane.cs new file mode 100644 index 0000000..286d24d --- /dev/null +++ b/HoistingCrane/HoistingCrane/AdditionEntityCrane.cs @@ -0,0 +1,18 @@ +namespace HoistingCrane.Entities +{ + public class AdditionEntityCrane : EntityCrane + { + //дополнительный + public Color AdditionalColor { get; private set; } + //противовес + public bool CounterWeight { get; private set; } + //кран + public bool Crane { get; private set; } + public AdditionEntityCrane(int speed, double weight, Color bodyColor, Color additionalColor, bool counterWeight, bool crane) : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + CounterWeight = counterWeight; + Crane = crane; + } + } +} diff --git a/HoistingCrane/HoistingCrane/AdditionalDrawingCrane.cs b/HoistingCrane/HoistingCrane/AdditionalDrawingCrane.cs new file mode 100644 index 0000000..206472a --- /dev/null +++ b/HoistingCrane/HoistingCrane/AdditionalDrawingCrane.cs @@ -0,0 +1,41 @@ +using HoistingCrane.Entities; + +namespace HoistingCrane.DrawningObjects +{ + public class AdditionalDrawingCrane : DrawingCrane + { + // Конструктор + public AdditionalDrawingCrane(int speed, double weight, Color bodyColor, Color additionalColor, bool counterWeight, bool crane, int width, int height) : base(speed, weight, bodyColor, width, height) + { + if (EntityCrane != null) + { + EntityCrane = new AdditionEntityCrane(speed, weight, bodyColor, additionalColor, counterWeight, crane); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityCrane is not AdditionEntityCrane transportCrane) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(transportCrane.AdditionalColor); + //противовес + if (transportCrane.CounterWeight) + { + g.DrawRectangle(pen, _startPositionX + 185, _startPositionY + 20, 15, 45); + + } + //кран + if (transportCrane.Crane) + { + g.DrawRectangle(pen, _startPositionX + 20, _startPositionY, 30, 65); + g.DrawRectangle(pen, _startPositionX, _startPositionY, 20, 30); + g.FillRectangle(additionalBrush, _startPositionX + 20, _startPositionY, 30, 65); + g.FillRectangle(additionalBrush, _startPositionX, _startPositionY, 20, 30); + } + base.DrawTransport(g); + + } + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs b/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs index 123a655..554ada7 100644 --- a/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs +++ b/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs @@ -35,6 +35,9 @@ this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); + this.buttonStep = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.buttonCreateAdditional = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCrane)).BeginInit(); this.SuspendLayout(); // @@ -50,7 +53,7 @@ // buttonCreate // this.buttonCreate.ImeMode = System.Windows.Forms.ImeMode.NoControl; - this.buttonCreate.Location = new System.Drawing.Point(0, 385); + this.buttonCreate.Location = new System.Drawing.Point(254, 378); this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Size = new System.Drawing.Size(170, 59); this.buttonCreate.TabIndex = 24; @@ -106,11 +109,49 @@ this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); // + // buttonStep + // + this.buttonStep.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.buttonStep.Location = new System.Drawing.Point(787, 45); + this.buttonStep.Name = "buttonStep"; + this.buttonStep.Size = new System.Drawing.Size(80, 35); + this.buttonStep.TabIndex = 27; + this.buttonStep.Text = "шаг"; + this.buttonStep.UseVisualStyleBackColor = true; + this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); + // + // comboBoxStrategy + // + 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(695, 6); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(182, 33); + this.comboBoxStrategy.TabIndex = 26; + // + // buttonCreateAdditional + // + this.buttonCreateAdditional.Font = new System.Drawing.Font("Segoe UI", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.buttonCreateAdditional.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.buttonCreateAdditional.Location = new System.Drawing.Point(0, 378); + this.buttonCreateAdditional.Name = "buttonCreateAdditional"; + this.buttonCreateAdditional.Size = new System.Drawing.Size(248, 59); + this.buttonCreateAdditional.TabIndex = 25; + this.buttonCreateAdditional.Text = "создать кран с утяжелителем и краном"; + this.buttonCreateAdditional.UseVisualStyleBackColor = true; + this.buttonCreateAdditional.Click += new System.EventHandler(this.ButtonCreateAdditional_Click); + // // CraneVisual // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(878, 444); + this.Controls.Add(this.buttonStep); + this.Controls.Add(this.comboBoxStrategy); + this.Controls.Add(this.buttonCreateAdditional); this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonRight); @@ -132,5 +173,8 @@ private Button buttonRight; private Button buttonLeft; private Button buttonUp; + private Button buttonStep; + private ComboBox comboBoxStrategy; + private Button buttonCreateAdditional; } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/CraneVisual.cs b/HoistingCrane/HoistingCrane/CraneVisual.cs index 4a50934..092449c 100644 --- a/HoistingCrane/HoistingCrane/CraneVisual.cs +++ b/HoistingCrane/HoistingCrane/CraneVisual.cs @@ -1,8 +1,12 @@ +using HoistingCrane.DrawningObjects; +using HoistingCrane.MovementStrategy; + namespace HoistingCrane { public partial class CraneVisual : Form { private DrawingCrane? _drawingCrane; + private AbstractStrategy? _abstractStrategy; public CraneVisual() { InitializeComponent(); @@ -16,11 +20,15 @@ namespace HoistingCrane pictureBoxCrane.Image = btm; } private void ButtonCreate_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingCrane = new DrawingCrane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxCrane.Width, pictureBoxCrane.Height); _drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void ButtonCreateAdditional_Click(object sender, EventArgs e) { Random random = new Random(); - _drawingCrane = new DrawingCrane(); - _drawingCrane.Init(random.Next(100, 300), random.Next(1000, 3000), 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)), pictureBoxCrane.Width, pictureBoxCrane.Height); - _drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawingCrane = new AdditionalDrawingCrane(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)), pictureBoxCrane.Width, pictureBoxCrane.Height); Draw(); } private void ButtonMove_Click(object sender, EventArgs e) @@ -44,5 +52,41 @@ namespace HoistingCrane } Draw(); } + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawingCrane == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectsCrane(_drawingCrane), pictureBoxCrane.Width, + pictureBoxCrane.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/HoistingCrane/HoistingCrane/Direction.cs b/HoistingCrane/HoistingCrane/Direction.cs index cded86b..0f97ab1 100644 --- a/HoistingCrane/HoistingCrane/Direction.cs +++ b/HoistingCrane/HoistingCrane/Direction.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HoistingCrane +namespace HoistingCrane { public enum DirectionType { diff --git a/HoistingCrane/HoistingCrane/DrawingCrane.cs b/HoistingCrane/HoistingCrane/DrawingCrane.cs index d9cc83c..2c95f9b 100644 --- a/HoistingCrane/HoistingCrane/DrawingCrane.cs +++ b/HoistingCrane/HoistingCrane/DrawingCrane.cs @@ -1,34 +1,36 @@ -using HoistingCrane; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using HoistingCrane.Entities; -namespace HoistingCrane +namespace HoistingCrane.DrawningObjects { - internal class DrawingCrane + public class DrawingCrane { - public EntityCrane? EntityCrane { get; private set; } + public EntityCrane? EntityCrane { get; protected set; } private int _pictureWidth; private int _pictureHeight; - private int _startPositionX; - private int _startPositionY; + protected int _startPositionX; + protected int _startPositionY; - private readonly int _craneWidth = 200; - private readonly int _craneHeight = 150; + protected readonly int _craneWidth = 200; + protected readonly int _craneHeight = 150; - public bool Init(int speed, double weight, bool counterWeight, bool crane, Color bodyColor, Color additionalColor, int width, int height) + public DrawingCrane(int speed, double weight, Color bodyColor, int width, int height) { - if ((_craneWidth > width) || (_craneHeight > height)) return false; + if ((_craneWidth > width) || (_craneHeight > height)) return; _pictureHeight = height; _pictureWidth = width; - EntityCrane = new EntityCrane(); - EntityCrane.Init(speed, weight, counterWeight, crane, bodyColor, additionalColor); + EntityCrane = new EntityCrane(speed, weight, bodyColor); + } + protected DrawingCrane(int speed, double weight, Color bodyColor, int width, int height, int craneWidth, int craneHeight) + { + if ((craneWidth > width) || (craneHeight > height)) return; + _pictureHeight = height; + _pictureWidth = width; + _craneHeight = craneHeight; + _craneWidth = craneWidth; + EntityCrane = new EntityCrane(speed, weight, bodyColor); - return true; } public void SetPosition(int x, int y) { @@ -75,11 +77,60 @@ namespace HoistingCrane } } - public void DrawTransport(Graphics g) + public int GetPosX => _startPositionX; + public int GetPosY => _startPositionY; + + public int GetWidth => _craneWidth; + public int GetHeight => _craneHeight; + public bool CanMove(DirectionType direction) + { + if (EntityCrane == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPositionX - EntityCrane.Step > 0, + //вверх + DirectionType.Up => _startPositionY - EntityCrane.Step > 0, + // вправо + DirectionType.Right => _startPositionX + EntityCrane.Step - _craneWidth < _pictureWidth, + //вниз + DirectionType.Down => _startPositionY + EntityCrane.Step - _craneHeight < _pictureHeight + + }; + } + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityCrane == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPositionX -= (int)EntityCrane.Step; + break; + //вверх + case DirectionType.Up: + _startPositionY -= (int)EntityCrane.Step; + break; + // вправо + case DirectionType.Right: + _startPositionX += (int)EntityCrane.Step; + break; + //вниз + case DirectionType.Down: + _startPositionY += (int)EntityCrane.Step; + break; + } + } + public virtual void DrawTransport(Graphics g) { if (EntityCrane == null) return; Pen pen = new Pen(Color.Black); - Brush additionalBrush = new SolidBrush(EntityCrane.AdditionalColor); Brush bodybrush = new SolidBrush(EntityCrane.BodyColor); // Гусеницы g.DrawLine(pen, _startPositionX + 5, _startPositionY + 110, _startPositionX + 195, _startPositionY + 110); @@ -104,18 +155,6 @@ namespace HoistingCrane //кабинка и выхлоп g.DrawRectangle(pen, _startPositionX + 60, _startPositionY + 10, 20, 55); g.DrawRectangle(pen, _startPositionX + 110, _startPositionY, 75, 65); - if (EntityCrane.CounterWeight) - { - g.DrawRectangle(pen, _startPositionX + 185, _startPositionY + 20, 15, 45); - - } - if (EntityCrane.Crane) - { - g.DrawRectangle(pen, _startPositionX + 20, _startPositionY, 30, 65); - g.DrawRectangle(pen, _startPositionX, _startPositionY, 20, 30); - g.FillRectangle(additionalBrush, _startPositionX + 20, _startPositionY, 30, 65); - g.FillRectangle(additionalBrush, _startPositionX, _startPositionY, 20, 30); - } } } } diff --git a/HoistingCrane/HoistingCrane/DrawningObjectsCrane.cs b/HoistingCrane/HoistingCrane/DrawningObjectsCrane.cs new file mode 100644 index 0000000..ac707c1 --- /dev/null +++ b/HoistingCrane/HoistingCrane/DrawningObjectsCrane.cs @@ -0,0 +1,27 @@ +using HoistingCrane.DrawningObjects; + +namespace HoistingCrane.MovementStrategy +{ + public class DrawningObjectsCrane : IMoveableObject + { + private readonly DrawingCrane? _drawingCrane = null; + public DrawningObjectsCrane(DrawingCrane drawingCrane) + { + _drawingCrane = drawingCrane; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawingCrane == null || _drawingCrane.EntityCrane == null) + { + return null; + } + return new ObjectParameters(_drawingCrane.GetPosX, _drawingCrane.GetPosY, _drawingCrane.GetWidth, _drawingCrane.GetHeight); + } + } + public int GetStep => (int)(_drawingCrane?.EntityCrane?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => _drawingCrane?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawingCrane?.MoveTransport(direction); + } +} diff --git a/HoistingCrane/HoistingCrane/EntityCrane.cs b/HoistingCrane/HoistingCrane/EntityCrane.cs index 92cc82c..e3a4ee9 100644 --- a/HoistingCrane/HoistingCrane/EntityCrane.cs +++ b/HoistingCrane/HoistingCrane/EntityCrane.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HoistingCrane +namespace HoistingCrane.Entities { - internal class EntityCrane + public class EntityCrane { //скорость public int Speed { get; private set; } @@ -14,22 +8,13 @@ namespace HoistingCrane public double Weight { get; private set; } //основной цввет public Color BodyColor { get; private set; } - //дополнительный - public Color AdditionalColor { get; private set; } - //противовес - public bool CounterWeight { get; private set; } - //Кран - public bool Crane { get; private set; } //шаг перемещения public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, bool counterWeight, bool crane, Color bodyColor, Color additionalColor) + public EntityCrane(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; - CounterWeight = counterWeight; - Crane = crane; BodyColor = bodyColor; - AdditionalColor = additionalColor; } } } diff --git a/HoistingCrane/HoistingCrane/IMoveableObject.cs b/HoistingCrane/HoistingCrane/IMoveableObject.cs new file mode 100644 index 0000000..8ab0383 --- /dev/null +++ b/HoistingCrane/HoistingCrane/IMoveableObject.cs @@ -0,0 +1,13 @@ +namespace HoistingCrane.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + /// Шаг объекта + int GetStep { get; } + /// Проверка, можно ли переместиться по нужному направлению + bool CheckCanMove(DirectionType direction); + /// Изменение направления перeмещения объекта + void MoveObject(DirectionType direction); + } +} diff --git a/HoistingCrane/HoistingCrane/MoveToBorder.cs b/HoistingCrane/HoistingCrane/MoveToBorder.cs new file mode 100644 index 0000000..f6d0851 --- /dev/null +++ b/HoistingCrane/HoistingCrane/MoveToBorder.cs @@ -0,0 +1,47 @@ +namespace HoistingCrane.MovementStrategy +{ + internal class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth && objParams.ObjectMiddleHorizontal <= FieldHeight && objParams.ObjectMiddleHorizontal + 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/HoistingCrane/HoistingCrane/MoveToCenter.cs b/HoistingCrane/HoistingCrane/MoveToCenter.cs new file mode 100644 index 0000000..26f8b32 --- /dev/null +++ b/HoistingCrane/HoistingCrane/MoveToCenter.cs @@ -0,0 +1,47 @@ +namespace HoistingCrane.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/HoistingCrane/HoistingCrane/ObjectParameters.cs b/HoistingCrane/HoistingCrane/ObjectParameters.cs new file mode 100644 index 0000000..4a34e4e --- /dev/null +++ b/HoistingCrane/HoistingCrane/ObjectParameters.cs @@ -0,0 +1,42 @@ +namespace HoistingCrane.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; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/HoistingCrane/HoistingCrane/Status.cs b/HoistingCrane/HoistingCrane/Status.cs new file mode 100644 index 0000000..8c32c5f --- /dev/null +++ b/HoistingCrane/HoistingCrane/Status.cs @@ -0,0 +1,10 @@ +namespace HoistingCrane.MovementStrategy +{ + // Статус выполнения операции перемещения + public enum Status + { + NotInit, + InProgress, + Finish + } +} \ No newline at end of file -- 2.25.1