diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/DirectionType.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DirectionType.cs index 503408f..806db97 100644 --- a/ProjectSeaplane/ProjectSeaplane/Drawings/DirectionType.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DirectionType.cs @@ -15,5 +15,7 @@ public enum DirectionType Left = 3, Right = 4, + + Unknow = -1, } diff --git a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs index 6fc5af4..19b754d 100644 --- a/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs +++ b/ProjectSeaplane/ProjectSeaplane/Drawings/DrawingPlane.cs @@ -18,10 +18,18 @@ public class DrawingPlane protected int? _startPosY; - private readonly int _drawingSeaplaneWidth = 190; + private readonly int _drawingPlaneWidth = 190; - private readonly int _drawingSeaplaneHeight = 70; + private readonly int _drawingPlaneHeight = 70; + public int? GetPosX => _startPosX; + + public int? GetPosY => _startPosY; + + public int GetWidth => _drawingPlaneWidth; + + public int GetHeight => _drawingPlaneHeight; + private DrawingPlane() { _pictureHeight = null; @@ -37,13 +45,13 @@ public class DrawingPlane protected DrawingPlane(int drawingSeaplaneWidth, int drawingSeaplaneHeight) : this() { - _drawingSeaplaneWidth = drawingSeaplaneWidth; - _drawingSeaplaneHeight = drawingSeaplaneHeight; + _drawingPlaneWidth = drawingSeaplaneWidth; + _drawingPlaneHeight = drawingSeaplaneHeight; } public bool SetPictureSize(int width, int height) { - if (_drawingSeaplaneWidth > width || _drawingSeaplaneHeight > height) + if (_drawingPlaneWidth > width || _drawingPlaneHeight > height) { return false; } @@ -53,14 +61,14 @@ public class DrawingPlane if (_startPosX.HasValue || _startPosY.HasValue) { - if (_startPosX + _drawingSeaplaneWidth > _pictureWidth) + if (_startPosX + _drawingPlaneWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawingSeaplaneWidth; + _startPosX = _pictureWidth - _drawingPlaneWidth; } else if (_startPosX < 0) _startPosX = 0; - if (_startPosY + _drawingSeaplaneHeight > _pictureHeight) + if (_startPosY + _drawingPlaneHeight > _pictureHeight) { - _startPosY = _pictureHeight - _drawingSeaplaneHeight; + _startPosY = _pictureHeight - _drawingPlaneHeight; } else if (_startPosY < 0) _startPosY = 0; } @@ -78,18 +86,18 @@ public class DrawingPlane { x = 0; } - else if (x - _drawingSeaplaneWidth >= _pictureWidth.Value) + else if (x - _drawingPlaneWidth >= _pictureWidth.Value) { - x = _pictureWidth.Value - _drawingSeaplaneWidth - _drawingSeaplaneWidth; + x = _pictureWidth.Value - _drawingPlaneWidth - _drawingPlaneWidth; } if (y < 0) { y = 0; } - else if (y - _drawingSeaplaneHeight >= _pictureHeight) + else if (y - _drawingPlaneHeight >= _pictureHeight) { - y = _pictureHeight.Value - _drawingSeaplaneHeight - _drawingSeaplaneHeight; + y = _pictureHeight.Value - _drawingPlaneHeight - _drawingPlaneHeight; } _startPosY = y; @@ -121,14 +129,14 @@ public class DrawingPlane return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityPlane.Step <= _pictureWidth.Value - _drawingSeaplaneWidth) + if (_startPosX.Value + EntityPlane.Step <= _pictureWidth.Value - _drawingPlaneWidth) { _startPosX += (int)EntityPlane.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityPlane.Step <= _pictureHeight.Value - _drawingSeaplaneHeight) + if (_startPosY.Value + EntityPlane.Step <= _pictureHeight.Value - _drawingPlaneHeight) { _startPosY += (int)EntityPlane.Step; } diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs index f9b1add..bcace8f 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonUp = new Button(); buttonCreatePlane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit(); SuspendLayout(); // @@ -47,11 +49,11 @@ pictureBoxSeaplane.TabIndex = 0; pictureBoxSeaplane.TabStop = false; // - // buttonCreate + // buttonCreateSeaplane // buttonCreateSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateSeaplane.Location = new Point(12, 511); - buttonCreateSeaplane.Name = "buttonCreate"; + buttonCreateSeaplane.Name = "buttonCreateSeaplane"; buttonCreateSeaplane.Size = new Size(205, 23); buttonCreateSeaplane.TabIndex = 1; buttonCreateSeaplane.Text = "Создать Seaplane"; @@ -117,11 +119,33 @@ buttonCreatePlane.UseVisualStyleBackColor = true; buttonCreatePlane.Click += buttonCreatePlane_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(684, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(684, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(121, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormSeaplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(817, 546); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreatePlane); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -144,5 +168,7 @@ private Button buttonDown; private Button buttonUp; private Button buttonCreatePlane; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs index 7b83156..9946e60 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectSeaplane.Drawings; +using ProjectSeaplane.MovementStrategy; namespace ProjectSeaplane { @@ -15,9 +16,15 @@ namespace ProjectSeaplane { private DrawingPlane? _drawingPlane; + private AbstractStrategy? _strategy; + + /// + /// Конструктор формы + /// public FormSeaplane() { InitializeComponent(); + _strategy = null; } private void Draw() @@ -51,9 +58,11 @@ namespace ProjectSeaplane default: return; } - _drawingPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); } @@ -91,5 +100,43 @@ namespace ProjectSeaplane Draw(); } } + + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingPlane == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveablePlane(_drawingPlane), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/AbstractStrategy.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..06660d1 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public abstract class AbstractStrategy +{ + private IMoveableObject? _moveableObject; + + private StrategyStatus _state = StrategyStatus.NotInit; + + protected int FieldWidth { get; private set; } + + protected int FieldHeight { get; private set; } + + public StrategyStatus GetStatus() { return _state; } + + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + + if (IsTargetDestination()) + { + _state = StrategyStatus.Finish; + 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 IsTargetDestination(); + + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/IMoveableObject.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..68e6ac9 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public interface IMoveableObject +{ + ObjectParameters? GetObjectPosition { get; } + + int GetStep { get; } + + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToBorder.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..7a88dd5 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder - GetStep() <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToCenter.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..9b92946 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + 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/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveablePlane.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveablePlane.cs new file mode 100644 index 0000000..3d24aad --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MoveablePlane.cs @@ -0,0 +1,59 @@ +using ProjectSeaplane.Drawings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public class MoveablePlane : IMoveableObject +{ + private readonly DrawingPlane? _plane = null; + + public MoveablePlane(DrawingPlane plane) + { + _plane = plane; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_plane == null || _plane.EntityPlane == null || !_plane.GetPosX.HasValue || !_plane.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_plane.GetPosX.Value, _plane.GetPosY.Value, _plane.GetWidth, _plane.GetHeight); + } + } + + public int GetStep => (int)(_plane?.EntityPlane?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_plane == null || _plane.EntityPlane == null) + { + return false; + } + + return _plane.MoveTransport(GetDirectionType(direction)); + } + + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MovementDirection.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..abb11ad --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/MovementDirection.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public enum MovementDirection +{ + Up = 1, + + Down = 2, + + Left = 3, + + Right = 4, +} + diff --git a/ProjectSeaplane/ProjectSeaplane/MovementStrategy/ObjectParameters.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..ab9b01e --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.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/ProjectSeaplane/ProjectSeaplane/MovementStrategy/StrategyStatus.cs b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..5f8e9a4 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy; + +public enum StrategyStatus +{ + NotInit, + + InProgress, + + Finish +}