From 13c7543c5ede191a04f85b53120610872b775850 Mon Sep 17 00:00:00 2001 From: dlopatin Date: Mon, 15 Apr 2024 16:04:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=20IMovableObject=20+=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=20MovableLocomotive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MovementStrategy/IMoveableObject.cs | 12 +++ .../MovementStrategy/MovableLocomotive.cs | 66 ++++++++++++++++ .../MovementStrategy/MovementDirection.cs | 30 ++++++++ .../MovementStrategy/ObjectParameters.cs | 76 +++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 WarmlyLocomotive/MovementStrategy/IMoveableObject.cs create mode 100644 WarmlyLocomotive/MovementStrategy/MovableLocomotive.cs create mode 100644 WarmlyLocomotive/MovementStrategy/MovementDirection.cs create mode 100644 WarmlyLocomotive/MovementStrategy/ObjectParameters.cs diff --git a/WarmlyLocomotive/MovementStrategy/IMoveableObject.cs b/WarmlyLocomotive/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..01cf264 --- /dev/null +++ b/WarmlyLocomotive/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.MovementStrategy +{ + internal interface IMoveableObject + { + } +} diff --git a/WarmlyLocomotive/MovementStrategy/MovableLocomotive.cs b/WarmlyLocomotive/MovementStrategy/MovableLocomotive.cs new file mode 100644 index 0000000..84a9d38 --- /dev/null +++ b/WarmlyLocomotive/MovementStrategy/MovableLocomotive.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WarmlyLocomotive.Drawnings; + +namespace WarmlyLocomotive.MovementStrategy; + +public class MoveableLocomotive : IMoveableObject +{ + /// + /// Поле-объект класса DrawningLocomotive или его наследника + /// + private readonly DrawningLocomotive? _locomotive = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningLocomotive + public MoveableLocomotive(DrawningLocomotive locomotive) + { + _locomotive = locomotive; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_locomotive == null || _locomotive.EntityLocomotive == null || !_locomotive.GetPosX.HasValue || !_locomotive.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_locomotive.GetPosX.Value, _locomotive.GetPosY.Value, _locomotive.GetWidth, _locomotive.GetHeight); + } + } + + + public int GetStep => (int)(_locomotive?.EntityLocomotive?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_locomotive == null || _locomotive.EntityLocomotive == null) + { + return false; + } + return _locomotive.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/WarmlyLocomotive/MovementStrategy/MovementDirection.cs b/WarmlyLocomotive/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..3b4b6ce --- /dev/null +++ b/WarmlyLocomotive/MovementStrategy/MovementDirection.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} diff --git a/WarmlyLocomotive/MovementStrategy/ObjectParameters.cs b/WarmlyLocomotive/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..f7bed13 --- /dev/null +++ b/WarmlyLocomotive/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.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; + } + +}