PIbd-21_KozyrevSS_GasolineT.../MoveToCenter.java

36 lines
1.2 KiB
Java

public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 && objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 &&
objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 && objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2;
}
@Override
protected void MoveToTarget() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null) {
return;
}
int diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else {
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}