70 lines
2.0 KiB
Java
70 lines
2.0 KiB
Java
|
public abstract class AbstractStrategy {
|
||
|
private IMoveableObject _moveableObject;
|
||
|
private Status _state = Status.NotInit;
|
||
|
|
||
|
private int FieldWidth;
|
||
|
private void SetFieldWidth(int width) {FieldWidth = width;}
|
||
|
protected int GetFieldWidth() {return FieldWidth;}
|
||
|
private int FieldHeight;
|
||
|
private void SetFieldHeight(int height) {FieldHeight = height;}
|
||
|
protected int GetFieldHeight() {return 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;
|
||
|
SetFieldWidth(width);
|
||
|
SetFieldHeight(height);
|
||
|
}
|
||
|
|
||
|
public void MakeStep()
|
||
|
{
|
||
|
if(_state != Status.InProgress)
|
||
|
return;
|
||
|
if (IsTargetDestination())
|
||
|
{
|
||
|
_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.GetObjectParameters(); }
|
||
|
|
||
|
protected Integer GetStep()
|
||
|
{
|
||
|
if (_state != Status.InProgress)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
return _moveableObject.GetStep();
|
||
|
}
|
||
|
|
||
|
protected abstract void MoveToTarget();
|
||
|
|
||
|
protected abstract boolean IsTargetDestination();
|
||
|
private boolean MoveTo(Direction direction)
|
||
|
{
|
||
|
if (_state != Status.InProgress)
|
||
|
return false;
|
||
|
if (!_moveableObject.CheckCanMove(direction))
|
||
|
{
|
||
|
_moveableObject.MoveObject(direction);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
}
|