58 lines
2.0 KiB
C#
58 lines
2.0 KiB
C#
using ProjectBattleship.DrawingObject;
|
||
namespace ProjectBattleship.MovementStrategy;
|
||
/// <summary>
|
||
/// Класс-реализация IMoveableObject с использованием DrawingWarship
|
||
/// </summary>
|
||
public class MoveableWarship : IMoveableObject
|
||
{
|
||
/// <summary>
|
||
/// Поле-объект класса DrawingWarship или его наследника
|
||
/// </summary>
|
||
private readonly DrawingWarship? _warship = null;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="warship">Объект класса DrawingWarship</param>
|
||
public MoveableWarship(DrawingWarship warship)
|
||
{
|
||
_warship = warship;
|
||
}
|
||
public ObjectParameters? GetObjectPosition
|
||
{
|
||
get
|
||
{
|
||
if (_warship == null || _warship.EntityWarship == null ||
|
||
!_warship.GetPosX.HasValue || !_warship.GetPosY.HasValue)
|
||
{
|
||
return null;
|
||
}
|
||
return new ObjectParameters(_warship.GetPosX.Value,
|
||
_warship.GetPosY.Value, _warship.GetWidth, _warship.GetHeight);
|
||
}
|
||
}
|
||
public int GetStep => (int)(_warship?.EntityWarship?.Step ?? 0);
|
||
public bool TryMoveObject(MovementDirection direction)
|
||
{
|
||
if (_warship == null || _warship.EntityWarship == null)
|
||
{
|
||
return false;
|
||
}
|
||
return _warship.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,
|
||
};
|
||
}
|
||
} |