PIBD14-BOYKO-M.S.SuperEasyS.../ProjectElectroTrans/MovementStrategy/MoveableTrans.cs

61 lines
1.8 KiB
C#
Raw Normal View History

2024-02-28 23:06:50 +04:00
using ProjectElectroTrans.Drawnings;
namespace ProjectElectroTrans.MovementStrategy;
public class MoveableTrans : IMoveableObject
{
/// <summary>
/// Поле-объект класса DrawningTrans или его наследника
/// </summary>
private readonly DrawingTrans? _trans = null;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="trans">Объект класса DrawningTrans</param>
public MoveableTrans(DrawingTrans trans)
{
_trans = trans;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_trans == null || _trans.EntityTrans == null ||
!_trans.GetPosX.HasValue || !_trans.GetPosY.HasValue)
{
return null;
}
return new ObjectParameters(_trans.GetPosX.Value,
_trans.GetPosY.Value, _trans.GetWidth, _trans.GetHeight);
}
}
public int GetStep => (int)(_trans?.EntityTrans?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_trans == null || _trans.EntityTrans == null)
{
return false;
}
return _trans.MoveTransport(GetDirectionType(direction));
}
/// <summary>
/// Конвертация из MovementDirection в DirectionType
/// </summary>
/// <param name="direction">MovementDirection</param>
/// <returns>DirectionType</returns>
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,
};
}
}