diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java index f85a0e9..74f55c0 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java @@ -13,12 +13,30 @@ import java.util.Scanner; * The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path. */ public class PathFollowerMoveStrategy implements MoveStrategy { + + /** + * The current Position of the car. + */ private PositionVector currentPosition; + /** + * The current Velocity of the car. + */ private PositionVector currentVelocity; + /** + * List of all points on the path. + */ private ArrayList pointList; + /** + * The index of the next point on the path. + */ private int pointer; - + /** + * Constructer to create a new PathFollowerMoveStrategy for a car. + * @param path The location where the file is saved + * @param startPosition The start position of the car + * @throws FileNotFoundException If the file with the given path does not exist. + */ public PathFollowerMoveStrategy(String path, PositionVector startPosition) throws FileNotFoundException { pointList = new ArrayList<>(); pointer = 0; @@ -27,6 +45,11 @@ public class PathFollowerMoveStrategy implements MoveStrategy { currentVelocity = new PositionVector(0, 0); } + /** + * Method to read the given File and add the points to the pointList + * @param trackFile the File Object which should be read + * @throws FileNotFoundException If the file with the given path does not exist. + */ public void readFile(File trackFile) throws FileNotFoundException { Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8"); while (scanner.hasNextLine()) { @@ -36,61 +59,60 @@ public class PathFollowerMoveStrategy implements MoveStrategy { } } + /** + * Method to select the direction for the next move. + * @return The direction for the next move. null if there are no points left in the list. + */ @Override public Direction nextMove() { + // if no more points in the list --> return null if (pointer >= pointList.size()) { return null; } - int accelerationX = 0; - int accelerationY = 0; + + // increase pointer variable if the next point is reached. if (pointList.get(pointer).equals(currentPosition)) { pointer ++; } - - if(currentVelocity.getX() > 0){ - accelerationX = -1; - } else if(currentVelocity.getX() < 0) { - accelerationX = 1; - } - if(currentVelocity.getY() > 0){ - accelerationY = -1; - } else if(currentVelocity.getY() < 0) { - accelerationY = 1; - } - - + // calculate Vector from current Position to next Point PositionVector movementVector = new PositionVector(pointList.get(pointer).getX() - currentPosition.getX(), pointList.get(pointer).getY() - currentPosition.getY()); - if(movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) { - accelerationX = 1; + + // select acceleration for X + int accelerationX; + if((movementVector.getX() == 0 && currentVelocity.getX() > 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) || //increase velocity + (movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX())){ //reduce velocity + accelerationX = -1; + } else if((movementVector.getX() == 0 && currentVelocity.getX() < 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) || //increase velocity + (movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX())) { //reduce velocity + accelerationX = 1; } - else if(movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) { - accelerationX = -1; - } - else if(movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX()) { - accelerationX = -1; - } - else if(movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX()) { - accelerationX = 1; + else { //no acceleration + accelerationX = 0; } - if(movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) { - accelerationY = 1; + // select acceleration for Y + int accelerationY; + if((movementVector.getY() == 0 && currentVelocity.getY() > 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) || //increase velocity + (movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY())){ //reduce velocity + accelerationY = -1; + } else if((movementVector.getY() == 0 && currentVelocity.getY() < 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) || //increase velocity + (movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY())) { //reduce velocity + accelerationY = 1; } - else if(movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) { - accelerationY = -1; - } - else if(movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY()) { - accelerationY = -1; - } - else if(movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY()) { - accelerationY = 1; + else { //no acceleration + accelerationY = 0; } - + //update current Velocity and current Position with the selected acceleration currentVelocity = new PositionVector(currentVelocity.getX() + accelerationX, currentVelocity.getY() + accelerationY); currentPosition = new PositionVector(currentPosition.getX() + currentVelocity.getX(), currentPosition.getY() + currentVelocity.getY()); + //Find Direction for acceleration PositionVector acceleration = new PositionVector(accelerationX, accelerationY); Direction[] directions = Direction.values(); for (Direction direction : directions) {