diff --git a/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DirectionType.cs b/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DirectionType.cs index 45f811f..ed8f73d 100644 --- a/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DirectionType.cs +++ b/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DirectionType.cs @@ -8,6 +8,7 @@ namespace ProjectRoadTrain.Drawnings; public enum DirectionType { + Unknow = -1, Up = 1, Down = 2, Left = 3, diff --git a/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningRoadTrain.cs b/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningRoadTrain.cs index f648595..93ba94b 100644 --- a/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningRoadTrain.cs +++ b/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningRoadTrain.cs @@ -30,23 +30,17 @@ public class DrawningRoadTrain : DrawningTrain Brush blackcolor = new SolidBrush(Color.Black); Brush bodycolor = new SolidBrush(roadTrain.BodyColor); - if (roadTrain.WaterTank || roadTrain.CleanBrush) + if (roadTrain.WaterTank) + { + g.FillEllipse(bodytankcolor, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50); + g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50); + } + if (roadTrain.CleanBrush) { g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 70, 100, 2); g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 75, 100, 2); g.FillRectangle(bodytankcolor, _startPosX.Value + 130, _startPosY.Value + 65, 100, 2); - g.FillEllipse(bodytankcolor, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50); } - g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 60, 170, 20); - g.DrawRectangle(pen, _startPosX.Value + 120, _startPosY.Value, 50, 60); - g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50); - // 120 высота - // 270 ширина - g.FillRectangle(blackcolor, _startPosX.Value, _startPosY.Value + 60, 170, 20); - g.FillRectangle(bodycolor, _startPosX.Value + 120, _startPosY.Value, 50, 60); - g.FillEllipse(blackcolor, _startPosX.Value + 120, _startPosY.Value + 77, 48, 40); - g.FillEllipse(blackcolor, _startPosX.Value + 49, _startPosY.Value + 77, 48, 40); - g.FillEllipse(blackcolor, _startPosX.Value, _startPosY.Value + 77, 48, 40); base.DrawTransport(g); } diff --git a/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningTrain.cs b/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningTrain.cs index 1f93078..5030265 100644 --- a/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningTrain.cs +++ b/ProjectRoadTrain/ProjectRoadTrain/Drawnings/DrawningTrain.cs @@ -19,9 +19,23 @@ public class DrawningTrain protected int? _startPosY; - private readonly int _drawningRoadWidth = 270; + private readonly int _drawningTrainWidth = 170; - private readonly int _drawningRoadHeight = 117; + private readonly int _drawningTrainHeight = 117; + + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _drawningTrainWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningTrainHeight; private DrawningTrain() @@ -37,12 +51,12 @@ public class DrawningTrain } protected DrawningTrain(int drawningRoadWidth, int drawningRoadHeight) : this() { - _drawningRoadWidth = drawningRoadWidth; - _drawningRoadHeight = drawningRoadHeight; + _drawningTrainWidth = drawningRoadWidth; + _drawningTrainHeight = drawningRoadHeight; } public bool SetPictureSize(int width, int hight) { - if (width >= _drawningRoadWidth && hight >= _drawningRoadHeight) + if (width >= _drawningTrainWidth && hight >= _drawningTrainHeight) { _pictureWidth = width; _pictureHight = hight; @@ -67,18 +81,18 @@ public class DrawningTrain { x = 0; } - else if (x > _pictureWidth.Value - _drawningRoadWidth) + else if (x > _pictureWidth.Value - _drawningTrainWidth) { - x = _pictureWidth.Value - _drawningRoadWidth; + x = _pictureWidth.Value - _drawningTrainWidth; } if (y < 0) { y = 0; } - else if (y > _pictureHight.Value - _drawningRoadHeight) + else if (y > _pictureHight.Value - _drawningTrainHeight) { - y = _pictureHight.Value - _drawningRoadHeight; + y = _pictureHight.Value - _drawningTrainHeight; } _startPosX = x; @@ -92,7 +106,7 @@ public class DrawningTrain { return false; } - if (_startPosX.Value < 0 || _startPosY.Value < 0 || _startPosX > _pictureWidth - _drawningRoadWidth || _startPosY > _pictureHight - _drawningRoadHeight) + if (_startPosX.Value < 0 || _startPosY.Value < 0 || _startPosX > _pictureWidth - _drawningTrainWidth || _startPosY > _pictureHight - _drawningTrainHeight) { return false; } @@ -116,14 +130,14 @@ public class DrawningTrain return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityTrain.Step < _pictureWidth - _drawningRoadWidth) + if (_startPosX.Value + EntityTrain.Step < _pictureWidth - _drawningTrainWidth) { _startPosX += (int)EntityTrain.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityTrain.Step < _pictureHight - _drawningRoadHeight) + if (_startPosY.Value + EntityTrain.Step < _pictureHight - _drawningTrainHeight) { _startPosY += (int)EntityTrain.Step; } @@ -149,7 +163,6 @@ public class DrawningTrain g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 60, 170, 20); g.DrawRectangle(pen, _startPosX.Value + 120, _startPosY.Value, 50, 60); - g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 10, 100, 50); // 120 высота // 270 ширина g.FillRectangle(blackcolor, _startPosX.Value, _startPosY.Value + 60, 170, 20); diff --git a/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.Designer.cs b/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.Designer.cs index 9549cdc..552d7a9 100644 --- a/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.Designer.cs +++ b/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.Designer.cs @@ -35,6 +35,8 @@ buttonRight = new Button(); buttonUp = new Button(); buttonCreateTrain = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).BeginInit(); SuspendLayout(); // @@ -117,11 +119,33 @@ buttonCreateTrain.UseVisualStyleBackColor = true; buttonCreateTrain.Click += buttonCreateTrain_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(755, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(812, 46); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(94, 29); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormRoadTrain // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(923, 536); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateTrain); Controls.Add(buttonUp); Controls.Add(buttonRight); @@ -144,5 +168,7 @@ private Button buttonRight; private Button buttonUp; private Button buttonCreateTrain; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.cs b/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.cs index 9bc36bb..1c9152c 100644 --- a/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.cs +++ b/ProjectRoadTrain/ProjectRoadTrain/FormRoadTrain.cs @@ -1,16 +1,22 @@ -using ProjectRoadTrain.Drawnings; +using ProjectElectroTrans.MovementStrategy; +using ProjectRoadTrain.Drawnings; using ProjectRoadTrain.Entities; +using ProjectRoadTrain.MovementStrategy; +using ProjectSportCar.MovementStrategy; namespace ProjectRoadTrain; public partial class FormRoadTrain : Form { private DrawningTrain? _drawningTrain; + private AbstractStrategy? _strategy; public FormRoadTrain() { InitializeComponent(); + _strategy = null; } + private void Draw() { if (_drawningTrain == null) @@ -44,6 +50,8 @@ public partial class FormRoadTrain : Form _drawningTrain.SetPictureSize(pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height); _drawningTrain.SetPosition(random.Next(10, 100), random.Next(10, 100)); + comboBoxStrategy.Enabled = true; + _strategy = null; Draw(); } @@ -87,5 +95,42 @@ public partial class FormRoadTrain : Form } } - + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningTrain == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTrans(_drawningTrain), pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } + diff --git a/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/AbstractStrategy.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..487c6d7 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,127 @@ + +using ProjectRoadTrain.MovementStrategy; + +namespace ProjectElectroTrans.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 (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/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/IMoveableObjects.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/IMoveableObjects.cs new file mode 100644 index 0000000..c845bd4 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/IMoveableObjects.cs @@ -0,0 +1,21 @@ +using ProjectRoadTrain.MovementStrategy; +namespace ProjectElectroTrans.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} + diff --git a/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveToBorder.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..4243f75 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,54 @@ +using ProjectElectroTrans.MovementStrategy; + +namespace ProjectSportCar.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + + + protected override bool IsTargetDestinaion() + { + 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.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} \ No newline at end of file diff --git a/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveToCenter.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..a243411 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,52 @@ + +namespace ProjectElectroTrans.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/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveableTrain.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveableTrain.cs new file mode 100644 index 0000000..22fc7f1 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MoveableTrain.cs @@ -0,0 +1,55 @@ +using ProjectRoadTrain.Drawnings; +using ProjectRoadTrain.Drawnings; +using ProjectRoadTrain.MovementStrategy; + + +namespace ProjectElectroTrans.MovementStrategy; +public class MoveableTrans : IMoveableObject +{ + private readonly DrawningTrain? _train = null; + public MoveableTrans(DrawningTrain train) + { + _train = train; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_train == null || _train.EntityTrain == null || + !_train.GetPosX.HasValue || !_train.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_train.GetPosX.Value, + _train.GetPosY.Value, _train.GetWidth, _train.GetHeight); + } + } + public int GetStep => (int)(_train?.EntityTrain?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_train == null || _train.EntityTrain == null) + { + return false; + } + return _train.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/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MovementDirection.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..248e8c1 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/MovementDirection.cs @@ -0,0 +1,6 @@ +namespace ProjectRoadTrain.MovementStrategy; + +public enum MovementDirection +{ + Up = 1, Down = 2, Left = 3, Right = 4 +} diff --git a/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/ObjectParameters.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..92df948 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,60 @@ +namespace ProjectElectroTrans.MovementStrategy; + +public class ObjectParameters +{ + /// + /// Координата X + /// + private readonly int _x; + /// + /// Координата Y + /// + 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/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/StrategyStatus.cs b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..301e78c --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectRoadTrain.MovementStrategy; + +public enum StrategyStatus +{ + NotInit, + InProgress, + Finish +}