78 lines
2.2 KiB
Java

public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NotInit;
private int fieldWidth;
protected int getFieldWidth() {
return fieldWidth;
}
private int fieldHeight;
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;
this.moveableObject = moveableObject;
fieldWidth = width;
fieldHeight = height;
}
public void makeStep() {
if (state != Status.InProgress) {
return;
}
if (isTargetDestination()) {
state = Status.Finish;
return;
}
moveToTarget();
}
protected boolean moveLeft() {
return moveTo(DirectionBulldozer.Left);
}
protected boolean moveRight() {
return moveTo(DirectionBulldozer.Right);
}
protected boolean moveUp() {
return moveTo(DirectionBulldozer.Up);
}
protected boolean moveDown() {
return moveTo(DirectionBulldozer.Down);
}
protected ObjectParameters getObjectParameters() {
if (moveableObject == null) {
return null;
}
return moveableObject.getObjectsPosition();
}
protected Integer getStep() {
if (state != Status.InProgress) {
return null;
}
if (moveableObject == null) {
return null;
}
return moveableObject.getStep();
}
protected abstract void moveToTarget();
protected abstract boolean isTargetDestination();
private boolean moveTo(DirectionBulldozer directionBulldozer) {
if (state != Status.InProgress) {
return false;
}
if (moveableObject == null) {
return false;
}
if (moveableObject.checkCanMove(directionBulldozer)) {
moveableObject.moveObject(directionBulldozer);
return true;
}
return false;
}
}