PIbd-14_Antonova_A.A.__Hard/MovementStrategy/MoveToCenter.java

39 lines
1.3 KiB
Java
Raw Permalink Normal View History

2024-05-09 20:09:37 +04:00
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 - GetStep() &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 - GetStep() &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 - GetStep() &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2 - GetStep() ;
}
@Override
protected void MoveToTarget() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null) {
return;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else {
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}