61 lines
1.9 KiB
Java
61 lines
1.9 KiB
Java
package ProjectContainerShip.src.MovementStrategy;
|
|
|
|
import ProjectContainerShip.src.Drawings.DirectionType;
|
|
public abstract class AbstractStrategy {
|
|
private IMoveableObject _movableObject;
|
|
private StrategyStatus _state = StrategyStatus.NotInit;
|
|
protected int FieldWidth;
|
|
protected int FieldHeight;
|
|
public StrategyStatus GetStatus() { return _state; }
|
|
|
|
public void SetData(IMoveableObject movableObject, int width, int height) {
|
|
if (movableObject == null)
|
|
{
|
|
_state = StrategyStatus.NotInit;
|
|
return;
|
|
}
|
|
_state = StrategyStatus.InProgress;
|
|
_movableObject = movableObject;
|
|
FieldHeight = height;
|
|
FieldWidth = width;
|
|
}
|
|
|
|
public void MakeStep() {
|
|
if (_state != StrategyStatus.InProgress) {
|
|
return;
|
|
}
|
|
if (IsTargetDestination()) {
|
|
_state = StrategyStatus.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() { return _movableObject.GetObjectPosition(); }
|
|
|
|
protected int GetStep() {
|
|
if(_state != StrategyStatus.InProgress) {
|
|
return 0;
|
|
}
|
|
return _movableObject.GetStep();
|
|
}
|
|
|
|
protected abstract void MoveToTarget();
|
|
protected abstract boolean IsTargetDestination();
|
|
private boolean MoveTo(DirectionType directionType) {
|
|
if (_state != StrategyStatus.InProgress) {
|
|
return false;
|
|
}
|
|
if (_movableObject.TryMoveObject(directionType)) {
|
|
_movableObject.MoveObject(directionType);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|