diff --git a/ProjectCar/ProjectCar/Drawnings/DirectionType.cs b/ProjectCar/ProjectCar/Drawnings/DirectionType.cs index 4570e9d..8e94ed4 100644 --- a/ProjectCar/ProjectCar/Drawnings/DirectionType.cs +++ b/ProjectCar/ProjectCar/Drawnings/DirectionType.cs @@ -1,9 +1,13 @@ -namespace ProjectGasMachine.Drawnings; + namespace ProjectGasMachine.Drawnings; /// /// Направление перемещения /// public enum DirectionType { + /// + /// неизвестное направление + /// + Unknow = -1, /// /// Вверх /// diff --git a/ProjectCar/ProjectCar/Drawnings/DrawningMachine.cs b/ProjectCar/ProjectCar/Drawnings/DrawningMachine.cs index b2ad11b..58d9812 100644 --- a/ProjectCar/ProjectCar/Drawnings/DrawningMachine.cs +++ b/ProjectCar/ProjectCar/Drawnings/DrawningMachine.cs @@ -39,6 +39,29 @@ public class DrawningMachine /// private readonly int _drawningMachineHeight = 70; + /// + /// координата X объекта + /// + + public int? GetPosX => _startPosX; + + /// + /// координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// ширина объекта + /// + public int GetWidth => _drawningMachineWidth; + /// + /// высота объекта + /// + public int GetHeight => _drawningMachineHeight; + + + + /// /// пустой конструктор /// diff --git a/ProjectCar/ProjectCar/MovementStrategy/AbstractStrategy.cs b/ProjectCar/ProjectCar/MovementStrategy/AbstractStrategy.cs index 073eae3..2a3240b 100644 --- a/ProjectCar/ProjectCar/MovementStrategy/AbstractStrategy.cs +++ b/ProjectCar/ProjectCar/MovementStrategy/AbstractStrategy.cs @@ -65,5 +65,72 @@ public abstract class AbstractStrategy 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 IsTargetDestination(); + + + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } } diff --git a/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs b/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs new file mode 100644 index 0000000..50e6c0e --- /dev/null +++ b/ProjectCar/ProjectCar/MovementStrategy/MoveableMachine.cs @@ -0,0 +1,70 @@ +using ProjectGasMachine.Drawnings; +using System.Reflection.PortableExecutable; + +namespace ProjectGasMachine.MovementStrategy; + +/// +/// класс-реализация IMoveableObjects с использованием DrawningMachine +/// +public class MoveableMachine : IMoveableObjects +{ + /// + /// поле-объект класса DrawningMachine или его наследника + /// + private readonly DrawningMachine? _machine = null; + + + /// + /// конструктор + /// + /// объект класса DrawningMachine + public MoveableMachine(DrawningMachine machine) + { + _machine = machine; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_machine == null || _machine.EntityGas == null || !_machine.GetPosX.HasValue || !_machine.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_machine.GetPosX.Value, _machine.GetPosY.Value, _machine.GetWidth, _machine.GetHeight); + } + } + + + + + + public int GetStep => (int)(_machine?.EntityGas?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_machine == null || _machine.EntityGas == null) + { + return false; + } + return _machine.MoveTransport(GetDirectionType(direction)); + } + + /// + /// конвертация из 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/ProjectCar/ProjectCar/Program.cs b/ProjectCar/ProjectCar/Program.cs index 87de37b..407e6fa 100644 --- a/ProjectCar/ProjectCar/Program.cs +++ b/ProjectCar/ProjectCar/Program.cs @@ -1,4 +1,4 @@ -namespace ProjectCar +namespace ProjectGasMachine { internal static class Program {