From f9487fc660ade9482722489df67a6d57c991005a Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sat, 4 Nov 2023 01:54:14 +0400 Subject: [PATCH] Lab2+ --- src/MoveToCenter.java | 37 +++++++++++++++++++++++++++++++++++++ src/Status.java | 5 +++++ 2 files changed, 42 insertions(+) create mode 100644 src/MoveToCenter.java create mode 100644 src/Status.java diff --git a/src/MoveToCenter.java b/src/MoveToCenter.java new file mode 100644 index 0000000..0bcde1b --- /dev/null +++ b/src/MoveToCenter.java @@ -0,0 +1,37 @@ +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean isTargetDestination(){ + var objParams = getObjectParameters(); + if(objParams == null) + return false; + return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 && + objParams.getObjectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 && + objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 && + objParams.getObjectMiddleVertical() + getStep() >= getFieldHeight() / 2; + } + @Override + protected void moveToTarget() { + ObjectParameters objParams = getObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2; + if (Math.abs(diffX) > getStep()) { + if (diffX > 0) { + moveLeft(); + } + else { + moveRight(); + } + } + var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2; + if (Math.abs(diffY) > getStep()) { + if (diffY > 0) { + moveUp(); + } + else { + moveDown(); + } + } + } +} diff --git a/src/Status.java b/src/Status.java new file mode 100644 index 0000000..c773b40 --- /dev/null +++ b/src/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NOT_INIT, + IN_PROGRESS, + FINISH +}