PIbd21.LyovushkinaA.A.Conta.../AbstractStrategy.java
2023-10-09 23:11:13 +04:00

80 lines
2.1 KiB
Java

public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int
height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() { return MoveTo(Direction.Left);}
protected boolean MoveRight() { return MoveTo(Direction.Right);}
protected boolean MoveUp() {return MoveTo(Direction.Up);}
protected boolean MoveDown(){return MoveTo(Direction.Down);}
protected ObjectParameters GetObjectParameters(){
return _moveableObject.GetObjectPosition();
}
protected int GetStep()
{
if (_state != Status.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(Direction direction)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(direction))
{
_moveableObject.MoveObject(direction);
return true;
}
return false;
}
}