diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DirectionType.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DirectionType.cs index 80c84f9..c645a8d 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DirectionType.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DirectionType.cs @@ -4,6 +4,10 @@ /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, /// /// Вверх /// diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShip.cs index fa543cd..fe33413 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShip.cs @@ -33,11 +33,26 @@ public class DrawningShip /// Ширина прорисовки /// private readonly int _drawningShipWidth = 150; - /// /// Высота прорисовки /// - private int _drawningShipHeight = 80; + protected int _drawningShipHeight = 80; + /// + /// Координаты Х + /// + public int? GetPosX => _startPosX; + /// + /// Координтаы У + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidht => _drawningShipWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningShipHeight; /// /// Пустой конструктор /// diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningWarmlyShip.cs index b076926..134df93 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningWarmlyShip.cs @@ -14,7 +14,7 @@ public class DrawningWarmlyShip : DrawningShip /// /// /// - public DrawningWarmlyShip(int speed, double weight, Color bodycolor, Color additionalcolor, bool shpipipes, bool fueltank) : base(150,140) + public DrawningWarmlyShip(int speed, double weight, Color bodycolor, Color additionalcolor, bool shpipipes, bool fueltank) : base(150,80) { EntityShip = new EntityWarmlyShip(speed, weight, bodycolor, additionalcolor, shpipipes, fueltank); //дописать конструктор } @@ -28,6 +28,7 @@ public class DrawningWarmlyShip : DrawningShip Brush brush = new SolidBrush(warmlyship.AdditionalColor); if (warmlyship.ShipPipes) { + _drawningShipHeight = 140; g.FillRectangle(brush, _startPosX.Value + 70, _startPosY.Value, 12, 60); g.FillRectangle(brush, _startPosX.Value + 90, _startPosY.Value, 12, 60); _startPosY += 60; diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs index 509d5d4..2d76590 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs @@ -35,6 +35,8 @@ buttonLeft = new Button(); buttonDown = new Button(); buttonСreateShip = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); SuspendLayout(); // @@ -124,11 +126,33 @@ buttonСreateShip.UseVisualStyleBackColor = false; buttonСreateShip.Click += buttonСreateShip_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(685, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(133, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(732, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(86, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormWarmlyShip // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(830, 366); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonСreateShip); Controls.Add(buttonDown); Controls.Add(buttonLeft); @@ -152,5 +176,7 @@ private Button buttonLeft; private Button buttonDown; private Button buttonСreateShip; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs index e1479b4..2a47915 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs @@ -1,18 +1,21 @@ using ProjectWarmlyShip.Drawnings; +using ProjectWarmlyShip.MovementStrategy; namespace ProjectWarmlyShip { public partial class FormWarmlyShip : Form { private DrawningShip? _drawningShip; + private AbstractStrategy? _strategy; public FormWarmlyShip() { InitializeComponent(); + _strategy = null; } private void CreateObject(string type) { Random random = new Random(); - switch(type) + switch (type) { case nameof(DrawningShip): _drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), @@ -29,6 +32,8 @@ namespace ProjectWarmlyShip } _drawningShip.SetPictureSize(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); Graphics gr = Graphics.FromImage(bmp); _drawningShip.DrawTransport(gr); @@ -84,6 +89,43 @@ namespace ProjectWarmlyShip pictureBoxWarmlyShip.Image = bmp; } } + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningShip == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableShip(_drawningShip), + pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningShip.DrawTransport(gr); + pictureBoxWarmlyShip.Image = bmp; + if (_strategy.GetStatus == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/AbstractStrategy.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..9647eaf --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,115 @@ +namespace ProjectWarmlyShip.MovementStrategy; + +public abstract class AbstractStrategy +{ + /// + /// Перемещаемый объект + /// + private IMoveableObjects? _moveableObject; + /// + /// Статус перемещения + /// + private StrategyStatus _state = StrategyStatus.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public StrategyStatus GetStatus {get { return _state; }} + /// + /// Установка данных + /// + /// + /// + /// + public void SetData(IMoveableObjects moveableObjects, int width, int height) + { + if (moveableObjects == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObjects; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) return; + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveRight() => MoveTo(MovementDirection.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected bool MoveUp() => MoveTo(MovementDirection.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false -неудача) + 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(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false -неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} + diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/IMoveableObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/IMoveableObjects.cs new file mode 100644 index 0000000..33f8030 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/IMoveableObjects.cs @@ -0,0 +1,17 @@ +namespace ProjectWarmlyShip.MovementStrategy; + +public interface IMoveableObjects +{ + //Получение координаты обхекта + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// + /// + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveToBorder.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..c57aa17 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,31 @@ +namespace ProjectWarmlyShip.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.LeftBorder - GetStep() <= 0 || + objParams.RightBorder + GetStep() >= FieldWidth || + objParams.TopBorder - GetStep() <= 0 + || objParams.ObjectMiddleVertical + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + //реализация в правый нижний угол + int x = objParams.RightBorder; + if (x + GetStep() < FieldWidth) MoveRight(); + int y = objParams.DownBorder; + if (y + GetStep() < FieldHeight) MoveDown(); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveToCenter.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..f72d05c --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,49 @@ +namespace ProjectWarmlyShip.MovementStrategy; + +public 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/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveableShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveableShip.cs new file mode 100644 index 0000000..caaffc7 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MoveableShip.cs @@ -0,0 +1,49 @@ +using ProjectWarmlyShip.Drawnings; + +namespace ProjectWarmlyShip.MovementStrategy; + +public class MoveableShip : IMoveableObjects +{ + private DrawningShip? _drawningship; + public MoveableShip(DrawningShip? drawningship) + { + _drawningship = drawningship; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningship == null || _drawningship.EntityShip == null || !_drawningship.GetPosX.HasValue || !_drawningship.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_drawningship.GetPosX.Value, _drawningship.GetPosY.Value, _drawningship.GetWidht, _drawningship.GetHeight); + } + } + public int GetStep => (int)(_drawningship?.EntityShip?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_drawningship == null || _drawningship.EntityShip == null) + { + return false; + } + return _drawningship.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/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MovementDirection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..23d2856 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/MovementDirection.cs @@ -0,0 +1,21 @@ +namespace ProjectWarmlyShip.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/ObjectParameters.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..1790454 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,59 @@ +namespace ProjectWarmlyShip.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/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/StrategyStatus.cs b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..117b98d --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,15 @@ +namespace ProjectWarmlyShip.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + // Завершена + Finish +}