59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
|
using ProjectTank.Drawnings;
|
|||
|
|
|||
|
namespace ProjectTank.MovementStrategy
|
|||
|
{
|
|||
|
public class MoveableTank : IMoveableObject
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Поле-объект класса или его DrawningTank наследника
|
|||
|
/// </summary>
|
|||
|
private readonly DrawningTank2? _tank = null;
|
|||
|
/// <summary>
|
|||
|
/// Конструктор
|
|||
|
/// </summary>
|
|||
|
/// <param name="car">Объект класса DrawningBoat</param>
|
|||
|
public MoveableTank(DrawningTank2 tank)
|
|||
|
{
|
|||
|
_tank = tank;
|
|||
|
}
|
|||
|
|
|||
|
public ObjectParameters? GetObjectPosition
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_tank == null || _tank.EntityTank2 == null ||
|
|||
|
!_tank.GetPosX.HasValue || !_tank.GetPosY.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new ObjectParameters(_tank.GetPosX.Value,
|
|||
|
_tank.GetPosY.Value, _tank.GetWidth, _tank.GetHeight);
|
|||
|
}
|
|||
|
}
|
|||
|
public int GetStep => (int)(_tank?.EntityTank2?.Step ?? 0);
|
|||
|
public bool TryMoveObject(MovementDirection direction)
|
|||
|
{
|
|||
|
if (_tank == null || _tank.EntityTank2 == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return _tank.MoveTransport(GetDirectionType(direction));
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Конвертация из MovementDirection в DirectionType
|
|||
|
/// </summary>
|
|||
|
/// <param name="direction">MovementDirection</param>
|
|||
|
/// <re turns>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,
|
|||
|
};
|
|||
|
}
|
|||
|
}
|
|||
|
}
|