59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
|
||
using ProjectDumpTruck.Drawnings;
|
||
|
||
namespace ProjectDumpTruck.MovementStrategy;
|
||
|
||
/// <summary>
|
||
/// Класс-реализация IMoveableObject с использованием DrawningTruck
|
||
/// </summary>
|
||
public class MoveableTruck : IMoveableObject
|
||
{
|
||
private DrawningTruck? _truck=null;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="truck">Объект класса DrawningTruck</param>
|
||
public MoveableTruck(DrawningTruck? truck)
|
||
{
|
||
_truck = truck;
|
||
}
|
||
|
||
public ObjectParameters? GetObjectPosition
|
||
{
|
||
get
|
||
{
|
||
if (_truck == null||_truck.EntityTruck == null|| !_truck.GetPosX.HasValue|| !_truck.GetPosY.HasValue)
|
||
return null;
|
||
|
||
return new ObjectParameters(_truck.GetPosX.Value, _truck.GetPosY.Value, _truck.GetWidth, _truck.GetHeight);
|
||
}
|
||
}
|
||
|
||
public int GetStep => (int)(_truck?.EntityTruck?.Step ?? 0);
|
||
|
||
/// <summary>
|
||
/// Конвертация из MovementDirection в DirectionType
|
||
/// </summary>
|
||
/// <param name="direction">MovementDirection</param>
|
||
/// <returns>DirectionType</returns>
|
||
private static DirectionType GetDirectionType(MovementDirection direction)
|
||
{
|
||
return direction switch
|
||
{
|
||
MovementDirection.Up => DirectionType.Up,
|
||
MovementDirection.Down => DirectionType.Down,
|
||
MovementDirection.Left => DirectionType.Left,
|
||
MovementDirection.Right => DirectionType.Right,
|
||
_ => DirectionType.Unknow
|
||
};
|
||
}
|
||
|
||
public bool TryMoveableObject(MovementDirection direction)
|
||
{
|
||
if (_truck == null || _truck.EntityTruck == null) return false;
|
||
|
||
return _truck.MoveTransport(GetDirectionType(direction));
|
||
}
|
||
}
|