From b6f69d3c1c83bc065711b8fd513ab7d199192723 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 10 Mar 2022 13:31:51 +0100 Subject: [PATCH] created Branch car finished all TODOS in car and created needed method getID and getVelocity. --- src/main/java/ch/zhaw/pm2/racetrack/Car.java | 53 +++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Car.java b/src/main/java/ch/zhaw/pm2/racetrack/Car.java index 9b97c3d..c8739f1 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Car.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Car.java @@ -3,6 +3,8 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.CarSpecification; import ch.zhaw.pm2.racetrack.strategy.MoveStrategy; +import java.util.Vector; + /** * Class representing a car on the racetrack. * Uses {@link PositionVector} to store current position on the track grid and current velocity vector. @@ -48,17 +50,39 @@ public class Car implements CarSpecification { setPosition(position); } + /** + * Returns the id of the car. + * + * @return id of the car. + */ + public char getID(){ + return id; + } + + /** + * Returns the current velocity of the car as a PositionVector. + * + * @return velocity current velocity of the car. + */ + public PositionVector getVelocity(){ + return velocity; + } /** * Set this Car position directly, regardless of current position and velocity. * This should only be used by the game controller in rare cases to set the crash or winning position. * The next position is normaly automatically calculated and set in the {@link #move()} method. * * @param position The new position to set the car directly to. + * @throws IllegalArgumentException if invalid PositionVector is given. */ @Override public void setPosition(final PositionVector position) { - // TODO: implementation - throw new UnsupportedOperationException(); + if (position.getX() < 0 || position.getY() < 0) { + throw new IllegalArgumentException(); + } + else { + this.position = position; + } } /** @@ -69,8 +93,7 @@ public class Car implements CarSpecification { */ @Override public PositionVector nextPosition() { - // TODO: implementation - throw new UnsupportedOperationException(); + return new PositionVector(position.getX() + velocity.getX(),position.getY() + velocity.getY()); } /** @@ -80,11 +103,18 @@ public class Car implements CarSpecification { * Changes only velocity, not position. * * @param acceleration A Direction vector containing the amounts to add to the velocity in x and y dimension + * @throws IllegalArgumentException if PositionVector is not in allowed area. */ @Override public void accelerate(PositionVector.Direction acceleration) { - // TODO: implementation - throw new UnsupportedOperationException(); + if(acceleration.vector.getX() < -1 || acceleration.vector.getX() > 1|| + acceleration.vector.getY() < -1 || acceleration.vector.getY() > 1) { + throw new IllegalArgumentException(); + } + else { + velocity = new PositionVector(velocity.getX() + acceleration.vector.getX(), + velocity.getY() + acceleration.vector.getY()); + } } /** @@ -92,8 +122,7 @@ public class Car implements CarSpecification { */ @Override public void move() { - // TODO: implementation - throw new UnsupportedOperationException(); + position = new PositionVector(position.getX() + velocity.getX(), position.getY() + velocity.getY()); } /** @@ -101,8 +130,7 @@ public class Car implements CarSpecification { */ @Override public void crash() { - // TODO: implementation - throw new UnsupportedOperationException(); + crashed = true; } /** @@ -112,13 +140,12 @@ public class Car implements CarSpecification { */ @Override public boolean isCrashed() { - // TODO: implementation - throw new UnsupportedOperationException(); + return crashed; } /** * Set move strategy - * @param moveStrategy + * @param moveStrategy Strategy to be implemented */ public void setMoveStrategy(MoveStrategy moveStrategy) { this.moveStrategy = moveStrategy;