67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WarmlyLocomotive.Drawnings;
|
||
|
||
namespace WarmlyLocomotive.MovementStrategy;
|
||
|
||
public class MoveableLocomotive : IMoveableObject
|
||
{
|
||
/// <summary>
|
||
/// Поле-объект класса DrawningLocomotive или его наследника
|
||
/// </summary>
|
||
private readonly DrawningLocomotive? _locomotive = null;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="locomotive">Объект класса DrawningLocomotive</param>
|
||
public MoveableLocomotive(DrawningLocomotive locomotive)
|
||
{
|
||
_locomotive = locomotive;
|
||
}
|
||
|
||
public ObjectParameters? GetObjectPosition
|
||
{
|
||
get
|
||
{
|
||
if (_locomotive == null || _locomotive.EntityLocomotive == null || !_locomotive.GetPosX.HasValue || !_locomotive.GetPosY.HasValue)
|
||
{
|
||
return null;
|
||
}
|
||
return new ObjectParameters(_locomotive.GetPosX.Value, _locomotive.GetPosY.Value, _locomotive.GetWidth, _locomotive.GetHeight);
|
||
}
|
||
}
|
||
|
||
|
||
public int GetStep => (int)(_locomotive?.EntityLocomotive?.Step ?? 0);
|
||
|
||
public bool TryMoveObject(MovementDirection direction)
|
||
{
|
||
if (_locomotive == null || _locomotive.EntityLocomotive == null)
|
||
{
|
||
return false;
|
||
}
|
||
return _locomotive.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,
|
||
};
|
||
}
|
||
}
|