diff --git a/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs index cacc39a..4862509 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs @@ -2,6 +2,7 @@ public enum DirectionType { + None = -1, Up = 1, Down = 2, Left = 3, diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs index d9d175a..a139c5c 100644 --- a/ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs @@ -18,6 +18,11 @@ public class DrawingBaseLiner private readonly int _drawingLinerHeight = 40; + public int? GetPosX => _startPosX; + public int? GetPosY => _startPosY; + public int GetWidth => _drawingLinerWidth; + public int GetHeight => _drawingLinerHeight; + private DrawingBaseLiner() { _pictureWidth = null; diff --git a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs index 7249588..2a06e5c 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs @@ -36,6 +36,8 @@ buttonMoveDown = new Button(); buttonMoveRight = new Button(); buttonCreateBaseLiner = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit(); SuspendLayout(); // @@ -125,11 +127,33 @@ buttonCreateBaseLiner.UseVisualStyleBackColor = true; buttonCreateBaseLiner.Click += buttonCreateBaseLiner_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "To Center", "To Border" }); + comboBoxStrategy.Location = new Point(862, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(908, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Step"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormLiner // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(995, 620); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateBaseLiner); Controls.Add(buttonMoveRight); Controls.Add(buttonMoveDown); @@ -154,5 +178,7 @@ private Button buttonMoveDown; private Button buttonMoveRight; private Button buttonCreateBaseLiner; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormLiner.cs b/ProjectLiner/ProjectLiner/FormLiner.cs index 2fd2351..d246d75 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.cs @@ -1,17 +1,23 @@ using ProjectLiner.Drawnings; using ProjectLiner.Entities; +using ProjectLiner.MovementStrategy; namespace ProjectLiner { public partial class FormLiner : Form { - public DrawingBaseLiner? _drawingLiner; + private DrawingBaseLiner? _drawingLiner; + + private AbstractStrategy? _strategy; + public FormLiner() { InitializeComponent(); + _strategy = null; + comboBoxStrategy.Enabled = false; } - private void DrawTransoprt() + private void Draw() { if (_drawingLiner == null) { @@ -45,14 +51,16 @@ namespace ProjectLiner _drawingLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height); _drawingLiner.SetPosition(random.Next(0, pictureBoxLiner.Width), random.Next(0, pictureBoxLiner.Height)); - DrawTransoprt(); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); } private void ButtonCreateLiner_Click(object sender, EventArgs e) { CreateObject(nameof(DrawingLiner)); } - + private void buttonCreateBaseLiner_Click(object sender, EventArgs e) { CreateObject(nameof(DrawingBaseLiner)); @@ -85,10 +93,43 @@ namespace ProjectLiner if (result) { - DrawTransoprt(); + Draw(); } } - + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingLiner == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableLiner(_drawingLiner), + pictureBoxLiner.Width, pictureBoxLiner.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + if (_strategy.GetStatus() == StrategyStatus.Finished) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs b/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..64ea1b6 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,60 @@ +namespace ProjectLiner.MovementStrategy; + +public abstract class AbstractStrategy +{ + private IMoveableObject? _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int FieldWidth { get; private set; } + protected int FieldHeight { get; private set; } + public StrategyStatus GetStatus() => _state; + + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finished; + return; + } + MoveToTarget(); + } + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + protected bool MoveRight() => MoveTo(MovementDirection.Right); + protected bool MoveUp() => MoveTo(MovementDirection.Up); + protected bool MoveDown() => MoveTo(MovementDirection.Down); + + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + protected abstract void MoveToTarget(); + protected abstract bool IsTargetDestinaion(); + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObject.cs b/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..062b433 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,8 @@ +namespace ProjectLiner.MovementStrategy; + +public interface IMoveableObject +{ + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..c38ee99 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,39 @@ +namespace ProjectLiner.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + if (objParams.RightBorder + GetStep() < FieldWidth && + objParams.DownBorder + GetStep() < FieldHeight) + { + MoveRight(); + MoveDown(); + } else if (objParams.RightBorder + GetStep() < FieldWidth) + { + MoveRight(); + } + else if (objParams.DownBorder + GetStep() < FieldHeight) + { + MoveDown(); + } + } +} diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..d975d5e --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,50 @@ +namespace ProjectLiner.MovementStrategy; + +internal class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs new file mode 100644 index 0000000..c0d1bd4 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs @@ -0,0 +1,47 @@ +using ProjectLiner.Drawnings; + +namespace ProjectLiner.MovementStrategy; + +public class MoveableLiner : IMoveableObject +{ + private readonly DrawingBaseLiner? _liner = null; + + public MoveableLiner(DrawingBaseLiner liner) + { + _liner = liner; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_liner == null || _liner.BaseLiner == null || + !_liner.GetPosX.HasValue || !_liner.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_liner.GetPosX.Value, _liner.GetPosY.Value, _liner.GetWidth, _liner.GetHeight); + } + } + + public int GetStep => (int)(_liner?.BaseLiner?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_liner == null || _liner.BaseLiner == null) + { + return false; + } + return _liner.MoveTransport(GetDirectionType(direction)); + } + + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.None, + }; + } +} diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..24d0391 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs @@ -0,0 +1,9 @@ +namespace ProjectLiner.MovementStrategy; + +public enum MovementDirection +{ + Up = 1, + Down = 2, + Left = 3, + Right = 4, +} diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs b/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..7b72048 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,26 @@ +namespace ProjectLiner.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/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs b/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..056dec9 --- /dev/null +++ b/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,8 @@ +namespace ProjectLiner.MovementStrategy; + +public enum StrategyStatus +{ + NotInit, + InProgress, + Finished, +}