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