using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMonorail.MovementStrategy;
public abstract class AbstractStrategy
{
///
/// Перемещаемый объект
///
private IMovableObjects? _moveableObject;
///
/// Статус перемещения
///
private StrategyStatus _state = StrategyStatus.NotInit;
///
/// Ширина поля
///
protected int FieldWidth { get; private set; }
///
/// Высота поля
///
protected int FieldHeight { get; private set; }
///
/// Статус перемещения
///
///
public StrategyStatus GetStatus() { return _state; }
///
/// Установка данных
///
/// Перемещаемый объект
/// Ширина поля
/// Высота поля
public void SetData(IMovableObjects movableObject, int width, int height)
{
if (movableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = movableObject;
FieldHeight = height;
FieldWidth = width;
}
///
/// Шаг перемещения
///
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
///
/// Шаг влево
///
///
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
///
/// Шаг вверх
///
///
protected bool MoveUp() => MoveTo(MovementDirection.Up);
///
/// Шаг вправо
///
///
protected bool MoveRight() => MoveTo(MovementDirection.Right);
///
/// Шаг вниз
///
///
protected bool MoveDown() => MoveTo(MovementDirection.Down);
///
/// Параметры объекта
///
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
///
/// Шаг объекта
///
///
protected int? GetStep()
{
if(_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
///
/// Перемещение к цели
///
protected abstract void MoveToTarget();
///
/// Достигнута ли цель
///
///
protected abstract bool IsTargetDestination();
private bool MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
}
}