implemented PathFollowerMoveStrategy.java #28
|
@ -13,12 +13,30 @@ import java.util.Scanner;
|
||||||
* The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path.
|
* The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path.
|
||||||
*/
|
*/
|
||||||
public class PathFollowerMoveStrategy implements MoveStrategy {
|
public class PathFollowerMoveStrategy implements MoveStrategy {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current Position of the car.
|
||||||
|
*/
|
||||||
private PositionVector currentPosition;
|
private PositionVector currentPosition;
|
||||||
|
/**
|
||||||
|
* The current Velocity of the car.
|
||||||
|
*/
|
||||||
private PositionVector currentVelocity;
|
private PositionVector currentVelocity;
|
||||||
|
/**
|
||||||
|
* List of all points on the path.
|
||||||
|
*/
|
||||||
private ArrayList<PositionVector> pointList;
|
private ArrayList<PositionVector> pointList;
|
||||||
|
/**
|
||||||
|
* The index of the next point on the path.
|
||||||
|
*/
|
||||||
private int pointer;
|
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 {
|
public PathFollowerMoveStrategy(String path, PositionVector startPosition) throws FileNotFoundException {
|
||||||
pointList = new ArrayList<>();
|
pointList = new ArrayList<>();
|
||||||
pointer = 0;
|
pointer = 0;
|
||||||
|
@ -27,6 +45,11 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
|
||||||
currentVelocity = new PositionVector(0, 0);
|
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 {
|
public void readFile(File trackFile) throws FileNotFoundException {
|
||||||
Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
|
Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
|
||||||
while (scanner.hasNextLine()) {
|
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
|
@Override
|
||||||
public Direction nextMove() {
|
public Direction nextMove() {
|
||||||
|
// if no more points in the list --> return null
|
||||||
if (pointer >= pointList.size()) {
|
if (pointer >= pointList.size()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
int accelerationX = 0;
|
|
||||||
int accelerationY = 0;
|
// increase pointer variable if the next point is reached.
|
||||||
if (pointList.get(pointer).equals(currentPosition)) {
|
if (pointList.get(pointer).equals(currentPosition)) {
|
||||||
pointer ++;
|
pointer ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calculate Vector from current Position to next Point
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PositionVector movementVector = new PositionVector(pointList.get(pointer).getX() - currentPosition.getX(), pointList.get(pointer).getY() - currentPosition.getY());
|
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()) {
|
else { //no acceleration
|
||||||
accelerationX = -1;
|
accelerationX = 0;
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) {
|
// select acceleration for Y
|
||||||
accelerationY = 1;
|
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()) {
|
else { //no acceleration
|
||||||
accelerationY = -1;
|
accelerationY = 0;
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//update current Velocity and current Position with the selected acceleration
|
||||||
currentVelocity = new PositionVector(currentVelocity.getX() + accelerationX, currentVelocity.getY() + accelerationY);
|
currentVelocity = new PositionVector(currentVelocity.getX() + accelerationX, currentVelocity.getY() + accelerationY);
|
||||||
currentPosition = new PositionVector(currentPosition.getX() + currentVelocity.getX(), currentPosition.getY() + currentVelocity.getY());
|
currentPosition = new PositionVector(currentPosition.getX() + currentVelocity.getX(), currentPosition.getY() + currentVelocity.getY());
|
||||||
|
|
||||||
|
//Find Direction for acceleration
|
||||||
PositionVector acceleration = new PositionVector(accelerationX, accelerationY);
|
PositionVector acceleration = new PositionVector(accelerationX, accelerationY);
|
||||||
Direction[] directions = Direction.values();
|
Direction[] directions = Direction.values();
|
||||||
for (Direction direction : directions) {
|
for (Direction direction : directions) {
|
||||||
|
|
Loading…
Reference in New Issue