98 lines
2.1 KiB
Java
98 lines
2.1 KiB
Java
|
package DoubleDeckerBus.MovementStrategy;
|
||
|
|
||
|
import DoubleDeckerBus.DirectionType;
|
||
|
|
||
|
public abstract class AbstractStrategy {
|
||
|
private IMoveableObject _moveableObject;
|
||
|
|
||
|
private Status _state = Status.NotInit;
|
||
|
|
||
|
private int FieldWidth;
|
||
|
|
||
|
protected int FieldWidth() {
|
||
|
return FieldWidth;
|
||
|
}
|
||
|
|
||
|
private int FieldHeight;
|
||
|
|
||
|
protected int FieldHeight() {
|
||
|
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;
|
||
|
FieldWidth = width;
|
||
|
FieldHeight = height;
|
||
|
}
|
||
|
|
||
|
public void MakeStep() {
|
||
|
if (_state != Status.InProgress) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (IsTargetDestination()) {
|
||
|
_state = Status.Finish;
|
||
|
return;
|
||
|
}
|
||
|
MoveToTarget();
|
||
|
}
|
||
|
|
||
|
protected boolean MoveLeft() {
|
||
|
return MoveTo(DirectionType.Left);
|
||
|
}
|
||
|
|
||
|
protected boolean MoveRight() {
|
||
|
return MoveTo(DirectionType.Right);
|
||
|
}
|
||
|
|
||
|
protected boolean MoveUp() {
|
||
|
return MoveTo(DirectionType.Up);
|
||
|
}
|
||
|
|
||
|
protected boolean MoveDown() {
|
||
|
return MoveTo(DirectionType.Down);
|
||
|
}
|
||
|
|
||
|
protected ObjectParameters GetObjectParameters(){
|
||
|
if (_moveableObject != null) {
|
||
|
return _moveableObject.GetObjectParameters();
|
||
|
}
|
||
|
else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected Integer GetStep() {
|
||
|
if (_state != Status.InProgress) {
|
||
|
return null;
|
||
|
}
|
||
|
return _moveableObject.GetStep();
|
||
|
}
|
||
|
|
||
|
protected abstract void MoveToTarget();
|
||
|
|
||
|
protected abstract boolean IsTargetDestination();
|
||
|
|
||
|
private boolean MoveTo(DirectionType directionType) {
|
||
|
if (_state != Status.InProgress) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (_moveableObject.CheckCanMove(directionType)) {
|
||
|
_moveableObject.MoveObject(directionType);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|