77 lines
2.0 KiB
C#
77 lines
2.0 KiB
C#
|
namespace AirBomber.MovementStrategy
|
|||
|
{
|
|||
|
public abstract class AbstractStrategy
|
|||
|
{
|
|||
|
private IMovableObject? _movableObject;
|
|||
|
|
|||
|
private Status _state = Status.NotInit;
|
|||
|
|
|||
|
protected int FieldWidth { get; private set; }
|
|||
|
protected int FieldHeight { get; private set; }
|
|||
|
|
|||
|
public Status GetStatus() { return _state; }
|
|||
|
|
|||
|
public void SetData(IMovableObject MovableObject, int Width, int Height)
|
|||
|
{
|
|||
|
if (MovableObject is null)
|
|||
|
{
|
|||
|
_state = Status.NotInit;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
_state = Status.InProgress;
|
|||
|
_movableObject = MovableObject;
|
|||
|
|
|||
|
FieldWidth = Width;
|
|||
|
FieldHeight = Height;
|
|||
|
}
|
|||
|
|
|||
|
public void MakeStep()
|
|||
|
{
|
|||
|
if (_state != Status.InProgress)
|
|||
|
return;
|
|||
|
|
|||
|
if (IsTargetDestination())
|
|||
|
{
|
|||
|
_state = Status.Finish;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
MoveToTarget();
|
|||
|
}
|
|||
|
|
|||
|
protected bool MoveLeft() => MoveTo(DirectionType.Left);
|
|||
|
protected bool MoveRight() => MoveTo(DirectionType.Right);
|
|||
|
protected bool MoveUp() => MoveTo(DirectionType.Up);
|
|||
|
protected bool MoveDown() => MoveTo(DirectionType.Down);
|
|||
|
|
|||
|
protected ObjectParameters? ObjectParameters => _movableObject?.ObjectPosition;
|
|||
|
|
|||
|
protected int? GetStep()
|
|||
|
{
|
|||
|
if (_state != Status.InProgress)
|
|||
|
return null;
|
|||
|
|
|||
|
return _movableObject?.Step;
|
|||
|
}
|
|||
|
|
|||
|
protected abstract void MoveToTarget();
|
|||
|
|
|||
|
protected abstract bool IsTargetDestination();
|
|||
|
|
|||
|
private bool MoveTo(DirectionType DirectionType)
|
|||
|
{
|
|||
|
if (_state != Status.InProgress)
|
|||
|
return false;
|
|||
|
|
|||
|
if (_movableObject?.CanMove(DirectionType) ?? false)
|
|||
|
{
|
|||
|
_movableObject.MoveObject(DirectionType);
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|