83 lines
2.0 KiB
Java
83 lines
2.0 KiB
Java
|
package ProjectStormtrooper;
|
||
|
|
||
|
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 (IsTargetDestination()) {
|
||
|
_state = Status.Finish;
|
||
|
return;
|
||
|
}
|
||
|
MoveToTarget();
|
||
|
}
|
||
|
|
||
|
protected boolean MoveLeft() {
|
||
|
return MoveTo(EnumDirectionType.Left);
|
||
|
}
|
||
|
|
||
|
protected boolean MoveRight() {
|
||
|
return MoveTo(EnumDirectionType.Right);
|
||
|
}
|
||
|
|
||
|
protected boolean MoveUp() {
|
||
|
return MoveTo(EnumDirectionType.Up);
|
||
|
}
|
||
|
|
||
|
protected boolean MoveDown() {
|
||
|
return MoveTo(EnumDirectionType.Down);
|
||
|
}
|
||
|
|
||
|
protected ObjectParameters GetObjectParameters() {
|
||
|
if (_moveableObject == null)
|
||
|
return null;
|
||
|
return _moveableObject.GetObjectPosition();
|
||
|
}
|
||
|
|
||
|
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(EnumDirectionType directionType) {
|
||
|
if (_state != Status.InProgress) {
|
||
|
return false;
|
||
|
}
|
||
|
if (_moveableObject == null)
|
||
|
return false;
|
||
|
if (_moveableObject.CheckCanMove(directionType)) {
|
||
|
_moveableObject.MoveObject(directionType);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|