2024-05-16 05:12:44 +03:00

58 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
};
}
}