PIBD-14_Khalikova.A.R_Storm.../ProjectStormtrooper/MovementStrategy/MoveableStormtrooperBase.cs

61 lines
2.2 KiB
C#
Raw Normal View History

using ProjectStormtrooper.Drawnings;
namespace ProjectStormtrooper.MovementStrategy;
/// <summary>
/// Класс-реализации IMoveableObject с использованием DrawingStormtrooperBase
/// </summary>
public class MoveableStormtrooperBase : IMoveableObject
{
/// <summary>
/// Поле-объект класса DrawningStormtrooperBase или его наследников
/// </summary>
private readonly DrawningStormtrooperBase? _stormtrooper = null;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="Stormtrooper">Объект класса DrawningStormtrooperBase</param>
public MoveableStormtrooperBase(DrawningStormtrooperBase Stormtrooper)
{
_stormtrooper = Stormtrooper;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_stormtrooper == null || _stormtrooper.EntityStormtrooperBase == null || !_stormtrooper.GetPosX.HasValue || !_stormtrooper.GetPosY.HasValue)
{
return null;
}
return new ObjectParameters(_stormtrooper.GetPosX.Value,_stormtrooper.GetPosY.Value,_stormtrooper.GetWidth,_stormtrooper.GetHeight);
}
}
public int GetStep => (int)(_stormtrooper?.EntityStormtrooperBase?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_stormtrooper == null || _stormtrooper.EntityStormtrooperBase == null)
{
return false;
}
return _stormtrooper.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,
};
}
}