70 lines
1.9 KiB
Java
70 lines
1.9 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(){
|
|
if (_moveableObject != null) {
|
|
return _moveableObject.GetObjectPosition();
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
protected int GetStep()
|
|
{
|
|
if (_state != Status.InProgress)
|
|
{
|
|
return Integer.parseInt(null);
|
|
}
|
|
return _moveableObject.GetStep();
|
|
}
|
|
protected abstract void MoveToTarget();
|
|
protected abstract boolean IsTargetDestinaion();
|
|
private boolean MoveTo(Direction directionType)
|
|
{
|
|
if (_state != Status.InProgress)
|
|
{
|
|
return false;
|
|
}
|
|
if (_moveableObject.CheckCanMove(directionType))
|
|
{
|
|
_moveableObject.MoveObject(directionType);
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
} |