diff --git a/ProjectBulldozer/ProjectBulldozer/DirectionType1.cs b/ProjectBulldozer/ProjectBulldozer/DirectionType.cs similarity index 90% rename from ProjectBulldozer/ProjectBulldozer/DirectionType1.cs rename to ProjectBulldozer/ProjectBulldozer/DirectionType.cs index 0afc190..4628515 100644 --- a/ProjectBulldozer/ProjectBulldozer/DirectionType1.cs +++ b/ProjectBulldozer/ProjectBulldozer/DirectionType.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectBulldozer; -public enum DirectionType1 +public enum DirectionType { /// /// Вверх diff --git a/ProjectBulldozer/ProjectBulldozer/DrawingBulldozer.cs b/ProjectBulldozer/ProjectBulldozer/DrawingBulldozer.cs index 0ca5b8b..9f51410 100644 --- a/ProjectBulldozer/ProjectBulldozer/DrawingBulldozer.cs +++ b/ProjectBulldozer/ProjectBulldozer/DrawingBulldozer.cs @@ -105,7 +105,7 @@ public class DrawningBulldozer /// /// Направление /// true - перемещене выполнено, false - перемещение - public bool MoveTransport(DirectionType1 direction) { + public bool MoveTransport(DirectionType direction) { if (EntityBulldozer == null || !_startPosX.HasValue || !_startPosY.HasValue) @@ -115,7 +115,7 @@ public class DrawningBulldozer switch (direction) { //влево - case DirectionType1.Left: + case DirectionType.Left: if (_startPosX.Value - EntityBulldozer.Step > 0) { _startPosX -= (int)EntityBulldozer.Step; @@ -123,7 +123,7 @@ public class DrawningBulldozer return true; //вверх - case DirectionType1.Up: + case DirectionType.Up: if (_startPosY.Value - EntityBulldozer.Step > 0) { _startPosY -= (int)EntityBulldozer.Step; @@ -131,7 +131,7 @@ public class DrawningBulldozer return true; // вправо - case DirectionType1.Right: + case DirectionType.Right: if (_startPosX + _BulldozerWidth + EntityBulldozer.Step < _pictureWidth) { _startPosX += (int)EntityBulldozer.Step; @@ -139,7 +139,7 @@ public class DrawningBulldozer return true; //вниз - case DirectionType1.Down: + case DirectionType.Down: if (_startPosY + _BulldozerHeight + EntityBulldozer.Step < _pictureHeight) { _startPosY += (int)EntityBulldozer.Step; diff --git a/ProjectBulldozer/ProjectBulldozer/MovementStrategy/AbstractStrategy.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..59ff5db --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.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/ProjectBulldozer/ProjectBulldozer/MovementStrategy/IMoveableObject.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..20fdaef --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно +bool TryMoveObject(MovementDirection direction); + +} diff --git a/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveToBorder.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..f80b1cb --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + /// + /// Стратегия перемещения объекта в правый угол + /// + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) return false; + + return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep(); + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) return; + + if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight(); + if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown(); + + } +} diff --git a/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveToCenter.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..9a22b4b --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.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/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveableBulldozer.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveableBulldozer.cs new file mode 100644 index 0000000..9a38733 --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MoveableBulldozer.cs @@ -0,0 +1,66 @@ +using ProjectBulldozer.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.MovementStrategy; + +public class MoveableBulldozer : IMoveableObject +{ + + /// + /// Поле-объект класса DrawningBulldozerProstoy или его наследника + /// + private readonly DrawningBulldozerProstoy? _bulldozer = null; + /// + /// Конструктор + /// + /// Объект класса DrawningBulldozerProstoy + public MoveableBulldozer(DrawningBulldozerProstoy bulldozer) + { + _bulldozer = bulldozer; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_bulldozer == null || _bulldozer.EntityBulldozerProstoy == null || + !_bulldozer.GetPosX.HasValue || !_bulldozer.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_bulldozer.GetPosX.Value, + _bulldozer.GetPosY.Value, _bulldozer.GetWidth, _bulldozer.GetHeight); + } + } + public int GetStep => (int)(_bulldozer?.EntityBulldozerProstoy?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_bulldozer == null || _bulldozer.EntityBulldozerProstoy == null) + { + return false; + } + return _bulldozer.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/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MovementDirection.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..89bd1ba --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/MovementDirection.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 + +} diff --git a/ProjectBulldozer/ProjectBulldozer/MovementStrategy/ObjectParameters.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..5aa8a73 --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.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/ProjectBulldozer/ProjectBulldozer/MovementStrategy/StrategyStatus.cs b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..54d84a2 --- /dev/null +++ b/ProjectBulldozer/ProjectBulldozer/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBulldozer.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish + +}