From fb690e38f941df8feb7ee85b8ce1b451bff1d6e8 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Sun, 6 May 2018 13:30:11 +0200 Subject: [PATCH] update --- OGP1718-Worms/src/worms/facade/Facade.java | 4 +- OGP1718-Worms/src/worms/model/Bazooka.java | 12 +- OGP1718-Worms/src/worms/model/GameObject.java | 247 +++-- OGP1718-Worms/src/worms/model/Program.java | 4 + OGP1718-Worms/src/worms/model/Projectile.java | 37 +- OGP1718-Worms/src/worms/model/Rifle.java | 9 +- OGP1718-Worms/src/worms/model/World.java | 4 +- OGP1718-Worms/src/worms/model/Worm.java | 963 ++++++++---------- 8 files changed, 619 insertions(+), 661 deletions(-) create mode 100644 OGP1718-Worms/src/worms/model/Program.java diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index ba3e4a3..4765f77 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -172,7 +172,7 @@ public class Facade implements IFacade { try { Coordinate jumpStep = worm.jumpStep(t); - return new double[]{jumpStep.item1, jumpStep.item2}; + return jumpStep.toArray(); } catch (IllegalArgumentException e) { throw new ModelException(e); @@ -1036,6 +1036,6 @@ public class Facade implements IFacade { */ @Override public void castSpell(World world) throws ModelException { - + world.castSpell(); } } diff --git a/OGP1718-Worms/src/worms/model/Bazooka.java b/OGP1718-Worms/src/worms/model/Bazooka.java index bd97be3..bbede59 100644 --- a/OGP1718-Worms/src/worms/model/Bazooka.java +++ b/OGP1718-Worms/src/worms/model/Bazooka.java @@ -1,9 +1,11 @@ package worms.model; +import worms.util.Coordinate; + import java.util.concurrent.ThreadLocalRandom; public class Bazooka extends Projectile { - protected Bazooka(World world, double[] location, double force, double orientation) { + protected Bazooka(World world, Coordinate location, double force, double orientation) { super(world, location, 300, force, orientation); } @@ -13,8 +15,12 @@ public class Bazooka extends Projectile { } @Override - protected void setHitPoints(int value) throws IllegalArgumentException { - if (value > 7 || value % 2 != 1) throw new IllegalArgumentException(); + protected void setHitPoints(int value) { + if (value > 7) value = 7; + else if (value % 2 != 1) { + if (value == 0) value++; + else value--; + } super.hitPoints = value; } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index a1a1cb3..81eae80 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -3,27 +3,44 @@ package worms.model; import be.kuleuven.cs.som.annotate.Raw; import worms.util.Coordinate; -import static java.lang.Math.PI; -import static java.lang.Math.pow; -import static java.lang.Math.round; +import static java.lang.Math.*; public abstract class GameObject { - /** - * - * @param world - * @param location - * @param radius - * - * @post ... - * |setWorld(world) - * @post ... - * |world.add(this) - * @post ... - * |setLocation(location) - * @post ... - * |setRadius(radius) - */ + /** + * This variable contains the radius of the game object + */ + protected double radius; + /** + * + */ + protected double mass; + /** + * + */ + private World world; + /** + * + */ + private boolean terminated = false; + /** + * this variable contains the location of the worm (a Coordinate) + */ + private Coordinate location; + + /** + * @param world + * @param location + * @param radius + * @post ... + * |setWorld(world) + * @post ... + * |world.add(this) + * @post ... + * |setLocation(location) + * @post ... + * |setRadius(radius) + */ protected GameObject(World world, double[] location, double radius) { setWorld(world); world.add(this); @@ -32,33 +49,48 @@ public abstract class GameObject { } /** - * - * @param world - * - * @return ... - * |result == !world.hasActiveGame() && !world.isTerminated() + * @param world + * @param location + * @param radius + * @post ... + * |setWorld(world) + * @post ... + * |world.add(this) + * @post ... + * |setLocation(location) + * @post ... + * |setRadius(radius) + */ + protected GameObject(World world, Coordinate location, double radius) { + setWorld(world); + world.add(this); + setLocation(location); + setRadius(radius); + } + + /** + * @param world + * @return ... |result == !world.hasActiveGame() && !world.isTerminated() */ public static boolean isValidWorld(World world) { return !world.hasActiveGame() && !world.isTerminated(); } + /** - * - * @return ... - * |result == this.world + * @return ... + * |result == this.world */ public World getWorld() { return this.world; } - + /** - * * @param world - * * @post ... - * |if world == null - * | this.world = null - * |else: - * | this.world = world + * |if world == null + * | this.world = null + * |else: + * | this.world = world */ public void setWorld(World world) { if (world == null) { @@ -68,16 +100,10 @@ public abstract class GameObject { if (!isValidWorld(world)) throw new IllegalArgumentException(); this.world = world; } - - /** - * - */ - private World world; /** - * - * @return ... - * |result == this.terminated + * @return ... + * |result == this.terminated */ public boolean isTerminated() { return this.terminated; @@ -85,39 +111,38 @@ public abstract class GameObject { /** * @post ... - * |this.terminated = true - * |getWorld().remove(this) + * |this.terminated = true + * |getWorld().remove(this) */ public void terminate() { this.terminated = true; getWorld().remove(this); } - - /** - * - */ - private boolean terminated = false; - - - - /** - * this variable contains the location of the worm (a Coordinate) - */ - private Coordinate location; /** * Return the location of the game object - * the location of the worm expresses the place of the game object - * in the play area + * the location of the worm expresses the place of the game object + * in the play area */ Coordinate getLocation() { return this.location; } - + /** - * - * @return ... - * |result == this.location.toArray() + * @param location + * @throws IllegalArgumentException ... + * |!isValidLocation(location) + * @post ... + * this.location = location + */ + protected void setLocation(Coordinate location) throws IllegalArgumentException { + + if (!isValidLocation(location)) throw new IllegalArgumentException(); + this.location = location; + } + + /** + * @return ... |result == this.location.toArray() */ public double[] getLocationArray() { return this.location.toArray(); @@ -126,15 +151,13 @@ public abstract class GameObject { /** * set the location of the worm to the given location * - * @param location - * the new location for the game object - * @post the new location of the game object is equal to the given location - * |new.getLocation() == location - * @throws IllegalArgumentException - * the given location is not a valid location for a game object - * |! isValidLocation(location) + * @param location the new location for the game object + * @throws IllegalArgumentException the given location is not a valid location for a game object + * |! isValidLocation(location) + * @post the new location of the game object is equal to the given location + * |new.getLocation() == location */ - void setLocation(double[] location) throws IllegalArgumentException { + protected void setLocation(double[] location) throws IllegalArgumentException { Coordinate locationCoordinate = Coordinate.create(location); @@ -142,44 +165,19 @@ public abstract class GameObject { this.location = locationCoordinate; } - /** - * - * @param location - * - * @post ... - * this.location = location - * - * @throws IllegalArgumentException - * ... - * |!isValidLocation(location) - */ - void setLocation(Coordinate location) throws IllegalArgumentException { - - if (!isValidLocation(location)) throw new IllegalArgumentException(); - this.location = location; - } - - - /** - * This variable contains the radius of the game object - */ - protected double radius; - /** * Return the radius of the game object - * the radius of the game object expresses half of the - * width of the game object + * the radius of the game object expresses half of the + * width of the game object */ - public double getRadius(){ + public double getRadius() { return this.radius; } - + /** - * * @param radius - * * @post ... - * |this.radius = radius + * |this.radius = radius */ protected void setRadius(double radius) { if (!canHaveAsRadius(radius)) throw new IllegalArgumentException(); @@ -189,11 +187,10 @@ public abstract class GameObject { /** * Check whether the given radius is a valid radius for the worm * - * @param radius - * the radius to check + * @param radius the radius to check * @return True if and only if the radius is bigger then the minimum radius - * (or equal) and the radius is a number - * |result == (radius >= getMinRadius() && !Double.isNaN(radius)) + * (or equal) and the radius is a number + * |result == (radius >= getMinRadius() && !Double.isNaN(radius)) */ @Raw private boolean canHaveAsRadius(double radius) { @@ -201,19 +198,16 @@ public abstract class GameObject { } /** - * * @param location - * - * @return ... - * |if (world == null) - * | result == !Double.isNaN(location.getX()) && !Double.isNaN(location.getY()) - * |result == !Double.isNaN(location.getX()) && - * | !Double.isNaN(location.getY()) && - * | !(location.getX() - radius < 0) && - * | !(location.getX() + radius > getWorld().getWidth()) && - * | !(location.getY() + radius > getWorld().getHeight()) && - * | !(location.getY() - radius < 0 && - * | !getWorld().isPassable(location)) + * @return ... |if (world == null) + * | result == !Double.isNaN(location.getX()) && !Double.isNaN(location.getY()) + * |result == !Double.isNaN(location.getX()) && + * | !Double.isNaN(location.getY()) && + * | !(location.getX() - radius < 0) && + * | !(location.getX() + radius > getWorld().getWidth()) && + * | !(location.getY() + radius > getWorld().getHeight()) && + * | !(location.getY() - radius < 0 && + * | !getWorld().isPassable(location)) */ protected boolean isValidLocation(Coordinate location) { double radius = getRadius(); @@ -228,19 +222,11 @@ public abstract class GameObject { !(location.getX() + radius > getWorld().getWidth()) && !(location.getY() + radius > getWorld().getHeight()) && !(location.getY() - radius < 0 && - !getWorld().isPassable(location)); + !getWorld().isPassable(location)); } - /** - * - */ - protected double mass; - - /** - * - * @return ... - * |result == this.mass + * @return ... |result == this.mass */ public double getMass() { return this.mass; @@ -249,13 +235,12 @@ public abstract class GameObject { /** * set the mass of the worm to the given mass (dependent on the radius) * - * @param radius - * part of the formula to calculate the mass - * @post the new mass of the worm is equal to - * rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) - * |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) + * @param radius part of the formula to calculate the mass + * @post the new mass of the worm is equal to + * rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) + * |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) */ - void setMass(double radius, double rho) { + protected void setMass(double radius, double rho) { this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); } } \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/Program.java b/OGP1718-Worms/src/worms/model/Program.java new file mode 100644 index 0000000..8e7574d --- /dev/null +++ b/OGP1718-Worms/src/worms/model/Program.java @@ -0,0 +1,4 @@ +package worms.model; + +public class Program { +} diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index ac4101d..9b1dac7 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -1,11 +1,13 @@ package worms.model; +import worms.util.Coordinate; + public abstract class Projectile extends GameObject { private static final int rho = 7800; - protected Projectile (World world, double location[], double mass, double force, double orientation) { + protected Projectile (World world, Coordinate location, double mass, double force, double orientation) { super(world, location, calcRadius(mass)); super.mass = mass; this.force = force; @@ -43,4 +45,37 @@ public abstract class Projectile extends GameObject { public double getOrientation() { return orientation; } + + @Override + protected boolean isValidLocation(Coordinate location) { + double radius = getRadius(); + + if (getWorld() == null) { + return !Double.isNaN(location.getX()) && + !Double.isNaN(location.getY()); + } + return !Double.isNaN(location.getX()) && + !Double.isNaN(location.getY()) && + !(location.getX() - radius < 0) && + !(location.getX() + radius > getWorld().getWidth()) && + !(location.getY() + radius > getWorld().getHeight()) && + !(location.getY() - radius < 0 && + !getWorld().isAdjacent(location, radius)); + } + + + public Coordinate getJumpStep(double elapsedTime) { + // TODO zie naar worm hoe dit moet, implementatie moet wel anders! + return null; + } + + public double getJumpTime(double jumpTimeStep) { + + // TODO zie naar worm hoe dit moet, implementatie moet wel anders! + return 0.0; + } + + public void jump() { + // TODO zie naar worm hoe dit moet, implementatie moet wel anders! + } } \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/Rifle.java b/OGP1718-Worms/src/worms/model/Rifle.java index 337acde..f1fc763 100644 --- a/OGP1718-Worms/src/worms/model/Rifle.java +++ b/OGP1718-Worms/src/worms/model/Rifle.java @@ -1,11 +1,13 @@ package worms.model; +import worms.util.Coordinate; + import java.util.concurrent.ThreadLocalRandom; public class Rifle extends Projectile { - public Rifle(World world, double[] location, double orientation) { + public Rifle(World world, Coordinate location, double orientation) { super(world, location, 10, 1.5, orientation); } @@ -15,8 +17,9 @@ public class Rifle extends Projectile { } @Override - protected void setHitPoints(int value) throws IllegalArgumentException { - if (value > 10 || value % 2 != 0) throw new IllegalArgumentException(); + protected void setHitPoints(int value) { + if (value > 10) value = 10; + else if (value % 2 != 0) value--; super.hitPoints = value; } diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index d1c1c78..7b23c36 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -571,8 +571,8 @@ public class World { item2.terminate(); } else if (item1 instanceof Projectile && item2 instanceof Projectile) { -// ((Projectile) item1).increaseHitPoints(2); -// ((Projectile) item2).increaseHitPoints(2); + ((Projectile) item1).incrementHitPoints(2); + ((Projectile) item2).incrementHitPoints(2); } else { throw new IllegalArgumentException(); diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index e29507b..4d89c30 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -13,28 +13,27 @@ import java.util.concurrent.ThreadLocalRandom; /** * A class with the specifications of the worm * - * @invar The location of the worm must be a valid location. - * |isValidLocation(getLocation()) - * @invar The orientation of the worm must be a valid location. - * |isValidOrientation(getOrientation()) - * @invar The radius of the worm mus be a valid radius. - * |isValidMinRadius(getRadius()) - * @invar The mass of the worm must bef a valid mass. - * |isValidMass(getMass()) - * @invar The value of the action points of the worm must be a valid value for action points. - * |isValidValueActionPoints(getActionPoints()) - * @invar The name of the worm must be a valid name. - * |isValidName(getName()) - * @invar The value of the hit points of the worm must be a valid value for hit points. - * isValidValueHitPoints(getHitPoints()) - * - * @version 3.0 * @author Arthur Bols and Leen Dereu - * + *

* Arthur Bols 1e bachelor Informatica * Leen Dereu 1e bachelor Informatica - * + *

* URL: https://github.com/KUL-ogp/ogp1718-project-bols-dereu + * @invar The location of the worm must be a valid location. + * |isValidLocation(getLocation()) + * @invar The orientation of the worm must be a valid location. + * |isValidOrientation(getOrientation()) + * @invar The radius of the worm mus be a valid radius. + * |isValidMinRadius(getRadius()) + * @invar The mass of the worm must bef a valid mass. + * |isValidMass(getMass()) + * @invar The value of the action points of the worm must be a valid value for action points. + * |isValidValueActionPoints(getActionPoints()) + * @invar The name of the worm must be a valid name. + * |isValidName(getName()) + * @invar The value of the hit points of the worm must be a valid value for hit points. + * isValidValueHitPoints(getHitPoints()) + * @version 3.0 */ public class Worm extends GameObject { @@ -42,35 +41,28 @@ public class Worm extends GameObject { //=================================================================================== /** - *Initialize the new worm with given location, orientation, name and radius. + * Initialize the new worm with given location, orientation, name and radius. * - * @param world - * The world this worm belongs to. - * @param location - * The location for the new worm. - * @param direction - * The direction for the new worm. - * @param name - * The name for the new worm. - * @param radius - * The radius for the new worm. - * @param team - * The team of the new worm. - * - * @post The new world of the worm is equal to the given world. - * |new.getWorld() == world - * @post The new location of the worm is equal to the given location. - * |new.getLocation() == location - * @post The new direction of the worm is equal to the given direction. - * |new.getDirection() == direction - * @post The new name of the worm is equal to the given name. - * |new.getName() == name - * @post The new radius of the worm is equal to the given radius. - * |new.getRadius() == radius - * @post The new team of the worm is equal to the given team. - * |new.getTeam() == team + * @param world The world this worm belongs to. + * @param location The location for the new worm. + * @param direction The direction for the new worm. + * @param name The name for the new worm. + * @param radius The radius for the new worm. + * @param team The team of the new worm. + * @post The new world of the worm is equal to the given world. + * |new.getWorld() == world + * @post The new location of the worm is equal to the given location. + * |new.getLocation() == location + * @post The new direction of the worm is equal to the given direction. + * |new.getDirection() == direction + * @post The new name of the worm is equal to the given name. + * |new.getName() == name + * @post The new radius of the worm is equal to the given radius. + * |new.getRadius() == radius + * @post The new team of the worm is equal to the given team. + * |new.getTeam() == team */ - @Raw + @Raw public Worm(World world, double[] location, double direction, double radius, String name, Team team) { this(world, location, direction, name, radius, 0.25, team); @@ -78,54 +70,41 @@ public class Worm extends GameObject { } /** - *initialize the new worm with given location, orientation, name, radius and minimum radius + * initialize the new worm with given location, orientation, name, radius and minimum radius * - * @param world - * The world for the new worm. - * @param location - * The location for the new worm. - * @param orientation - * The orientation for the new worm. - * @param name - * The name for the new worm. - * @param radius - * The radius for the new worm. - * @param minRadius - * The minimum radius the new worm can have. - * @param team - * The team for the new worm. - * - * @post The new world of the worm is equal to the given world. - * |new.getWorld() == world - * @post The new location of the worm is equal to the given location. - * |new.getLocation() == location - * @post The new orientation of the worm is equal to the given orientation. - * |new.getOrientation() == orientation - * @post The new name of the worm is equal to the given name. - * |new.getName() == name - * @post The new radius of the worm is equal to the given radius. - * |new.getRadius() == radius - * @post The new minimum radius of the worm is equal to the given minimum radius. - * |new.getMinRadius() == minRadius - * @post The new value of action points of the worm is equal to the maximum possible value of action points. - * |new.getActionPoints() == getMaxActionPoints() - * @post The new value of hit points of the worm is equal to the start value for hit points. - * |new.getHitPoints() == startHitPoints - * - * @throws IllegalArgumentException - * The given location is not a valid location for a worm. - * |!isValidLocation(location) - * @throws IllegalArgumentException - * The given minimum radius is not a valid minimum radius for a worm. - * |!isValidMinRadius(minRadius) - * @throws IllegalArgumentException - * The given radius can not be a minimum radius for a worm. - * |!canHaveAsMinRadius(radius) - * @throws IllegalNameException - * The given name is not a valid name for a worm. - * |isValidName(name) + * @param world The world for the new worm. + * @param location The location for the new worm. + * @param orientation The orientation for the new worm. + * @param name The name for the new worm. + * @param radius The radius for the new worm. + * @param minRadius The minimum radius the new worm can have. + * @param team The team for the new worm. + * @throws IllegalArgumentException The given location is not a valid location for a worm. + * |!isValidLocation(location) + * @throws IllegalArgumentException The given minimum radius is not a valid minimum radius for a worm. + * |!isValidMinRadius(minRadius) + * @throws IllegalArgumentException The given radius can not be a minimum radius for a worm. + * |!canHaveAsMinRadius(radius) + * @throws IllegalNameException The given name is not a valid name for a worm. + * |isValidName(name) + * @post The new world of the worm is equal to the given world. + * |new.getWorld() == world + * @post The new location of the worm is equal to the given location. + * |new.getLocation() == location + * @post The new orientation of the worm is equal to the given orientation. + * |new.getOrientation() == orientation + * @post The new name of the worm is equal to the given name. + * |new.getName() == name + * @post The new radius of the worm is equal to the given radius. + * |new.getRadius() == radius + * @post The new minimum radius of the worm is equal to the given minimum radius. + * |new.getMinRadius() == minRadius + * @post The new value of action points of the worm is equal to the maximum possible value of action points. + * |new.getActionPoints() == getMaxActionPoints() + * @post The new value of hit points of the worm is equal to the start value for hit points. + * |new.getHitPoints() == startHitPoints */ - @Raw + @Raw public Worm(World world, double[] location, double orientation, String name, double radius, double minRadius, Team team) throws IllegalArgumentException { super(world, location, radius); @@ -159,10 +138,11 @@ public class Worm extends GameObject { /** * Return the orientation of the worm. - * The orientation of a worm expresses the direction in which - * the worm is looking. + * The orientation of a worm expresses the direction in which + * the worm is looking. */ - @Basic @Raw + @Basic + @Raw public double getOrientation() { return orientation; } @@ -174,13 +154,12 @@ public class Worm extends GameObject { /** * Set the orientation of the worm to the given orientation. - * - * @param orientation - * The new orientation of the worm. - * @pre The given orientation must be a valid orientation for any worm. - * |isValidOrientation(orientation) - * @post The new orientation of the worm must be equal to the given orientation. - * |new.getOrientation() == orientation + * + * @param orientation The new orientation of the worm. + * @pre The given orientation must be a valid orientation for any worm. + * |isValidOrientation(orientation) + * @post The new orientation of the worm must be equal to the given orientation. + * |new.getOrientation() == orientation */ @Raw private void setOrientation(double orientation) { @@ -191,10 +170,9 @@ public class Worm extends GameObject { /** * Check whether the given orientation is a valid orientation for the worm. * - * @param newOrientation - * The orientation to check. + * @param newOrientation The orientation to check. * @return True if and only if the orientation is bigger then and smaller then 2pi. - * |result == ( newOrientation >= 0 && newOrientation < 2 * PI ) + * |result == ( newOrientation >= 0 && newOrientation < 2 * PI ) */ public static boolean isValidOrientation(double newOrientation) { return newOrientation >= 0 && newOrientation < 2 * PI; @@ -206,14 +184,13 @@ public class Worm extends GameObject { // region Shape mass/radius //=================================================================================== - + /** * Check whether the given radius is a valid minimum radius for the worm. * - * @param minRadius - * The radius to check. + * @param minRadius The radius to check. * @return True if and only if the radius is a number and the radius is bigger then 0. - * |result == ((!Double.isNaN(radius)) && (radius > 0)) + * |result == ((!Double.isNaN(radius)) && (radius > 0)) */ public static boolean isValidMinRadius(double minRadius) { return !Double.isNaN(minRadius) && minRadius > 0; @@ -221,16 +198,15 @@ public class Worm extends GameObject { /** * Set the radius of the worm to the given radius. - * - * @param radius - * The new radius for the worm. - * @post The new radius of the worm is equal to the given radius. - * |new.getRadius() == radius - * @throws IllegalArgumentException - * The given radius is not a valid radius for any worm. - * |! canHaveAsMinRadius(radius) + * + * @param radius The new radius for the worm. + * @throws IllegalArgumentException The given radius is not a valid radius for any worm. + * |! canHaveAsMinRadius(radius) + * @post The new radius of the worm is equal to the given radius. + * |new.getRadius() == radius */ - @Raw @Override + @Raw + @Override public void setRadius(double radius) throws IllegalArgumentException { if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius"); @@ -243,10 +219,11 @@ public class Worm extends GameObject { /** * Return the minimum radius the worm can have. - * The minimum radius of the worm expresses the minimum length - * of half of the width of the worm. + * The minimum radius of the worm expresses the minimum length + * of half of the width of the worm. */ - @Basic @Raw + @Basic + @Raw public double getMinRadius() { return this.minRadius; } @@ -258,12 +235,11 @@ public class Worm extends GameObject { /** * Check whether the given radius is a valid radius for the worm. - * - * @param radius - * The radius to check. + * + * @param radius The radius to check. * @return True if and only if the radius is bigger then the minimum radius - * (or equal) and the radius is a number. - * |result == (radius >= getMinRadius() && !Double.isNaN(radius)) + * (or equal) and the radius is a number. + * |result == (radius >= getMinRadius() && !Double.isNaN(radius)) */ @Raw private boolean canHaveAsRadius(double radius) { @@ -272,9 +248,10 @@ public class Worm extends GameObject { /** * Return the mass of the worm. - * The mass of the worm expresses the weight of the worm. + * The mass of the worm expresses the weight of the worm. */ - @Basic @Raw + @Basic + @Raw public double getMass() { return this.mass; } @@ -283,24 +260,25 @@ public class Worm extends GameObject { // endregion - // region ActionPoints //=================================================================================== /** * Return the current action points of the worm. - * The action points identifies the energy of the worm. + * The action points identifies the energy of the worm. */ - @Basic @Raw + @Basic + @Raw public long getActionPoints() { return this.actionPoints; } /** * Return the maximum of action points of the worm. - * The maximum action points identifies the maximum energy of the worm. + * The maximum action points identifies the maximum energy of the worm. */ - @Basic @Raw + @Basic + @Raw public long getMaxActionPoints() { return this.maxActionPoints; } @@ -308,17 +286,16 @@ public class Worm extends GameObject { /** * Set the current points of the worm to the given points. * - * @param actionPoints - * The new points for the worm. - * @post If the given points are bigger then the maximum points, the current points - * are equal to the maximum points. If the given points are lower then 0 - * the current points are equal to 0. If the given points is between the - * maximum points and 0, the current points is equal to the given points. - * |if (actionPoints > getMaxActionPoints()) - * | actionPoints = getMaxActionPoints(); - * |else if (actionPoints < 0) - * | actionPoints = 0; - * |this.actionPoints = actionPoints; + * @param actionPoints The new points for the worm. + * @post If the given points are bigger then the maximum points, the current points + * are equal to the maximum points. If the given points are lower then 0 + * the current points are equal to 0. If the given points is between the + * maximum points and 0, the current points is equal to the given points. + * |if (actionPoints > getMaxActionPoints()) + * | actionPoints = getMaxActionPoints(); + * |else if (actionPoints < 0) + * | actionPoints = 0; + * |this.actionPoints = actionPoints; */ @Raw public void setActionPoints(long actionPoints) { @@ -332,13 +309,12 @@ public class Worm extends GameObject { /** * Substract the current points of the worm. - * - * @param delta - * The value which should be decreased. - * @post The current points are set to the old current points minus the given value. - * |setActionPoints(getActionPoints() - delta) + * + * @param delta The value which should be decreased. + * @post The current points are set to the old current points minus the given value. + * |setActionPoints(getActionPoints() - delta) */ - public void decreaseActionPoints (long delta) { + public void decreaseActionPoints(long delta) { setActionPoints(getActionPoints() - delta); } @@ -356,42 +332,39 @@ public class Worm extends GameObject { /** * Set the maximum of points to the given maximum of points. * - * @param maxActionPoints - * The new maximum of points for the worm. - * @post The new maximum points is set to the given maximum points (as an integer). - * |this.maxPoints = round(maxActionPoints) - * @post When the maximum points change, the current points should change too. - * |setActionPoints(getActionPoints) + * @param maxActionPoints The new maximum of points for the worm. + * @post The new maximum points is set to the given maximum points (as an integer). + * |this.maxPoints = round(maxActionPoints) + * @post When the maximum points change, the current points should change too. + * |setActionPoints(getActionPoints) */ @Raw private void setMaxActionPoints(double maxActionPoints) { this.maxActionPoints = round(maxActionPoints); setActionPoints(getActionPoints()); } - + /** * Substract the current points of the worm. - * - * @param value - * The value which should be subtracted. - * @post The current points are set to the old current points minus the given value. - * |setActionPoints(getActionPoints() - value) + * + * @param value The value which should be subtracted. + * @post The current points are set to the old current points minus the given value. + * |setActionPoints(getActionPoints() - value) */ - private void subtractActionPoints (long value) { + private void subtractActionPoints(long value) { setActionPoints(getActionPoints() - value); } - + /** * Substract the current points of the worm. - * - * @param angle - * The angle needed to calculate the new current points. - * @post The current points are set to the old current points minus - * the angle (in degrees) divided by 6. - * |setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6)) + * + * @param angle The angle needed to calculate the new current points. + * @post The current points are set to the old current points minus + * the angle (in degrees) divided by 6. + * |setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6)) */ - private void subtractActionPoints (double angle) { + private void subtractActionPoints(double angle) { setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6)); } @@ -400,15 +373,16 @@ public class Worm extends GameObject { // endregion - // region name //=================================================================================== - + /** * Return the name of the worm. - * The name of the worm expresses the identity of the worm. + * The name of the worm expresses the identity of the worm. */ - @Basic @Immutable @Raw + @Basic + @Immutable + @Raw public String getName() { return this.name; } @@ -416,17 +390,16 @@ public class Worm extends GameObject { /** * Check whether the given name is a valid name for all worms. * - * @param name - * The name to check. + * @param name The name to check. * @return -1 if and only if the given name is longer then 2, - * the first letter is uppercase and the name only exists - * of letters, " ", " ' " and " "" ". - * |for (i = 0; i < name.length(); i++) - * |result == (name.length() > 2 && Character.isUpperCase(name.charAt(0) - * |&& Character.isLetter(name.charAt(i)) && - * |allowedCharacters.indexOf(name.charAt(i))) + * the first letter is uppercase and the name only exists + * of letters, " ", " ' " and " "" ". + * |for (i = 0; i < name.length(); i++) + * |result == (name.length() > 2 && Character.isUpperCase(name.charAt(0) + * |&& Character.isLetter(name.charAt(i)) && + * |allowedCharacters.indexOf(name.charAt(i))) */ - public static int isValidName (String name) { + public static int isValidName(String name) { if (name.length() < 2 || !Character.isUpperCase(name.charAt(0))) { return 0; @@ -445,14 +418,12 @@ public class Worm extends GameObject { /** * Set the name of the worm tot the given name. - * - * @param name - * The new name for the worm. - * @post The new name of the worm is equal to the given name. - * |new.getName() == name - * @throws IllegalNameException - * The given name is not a valid name for any worm. - * |! isValidName(name) + * + * @param name The new name for the worm. + * @throws IllegalNameException The given name is not a valid name for any worm. + * |! isValidName(name) + * @post The new name of the worm is equal to the given name. + * |new.getName() == name */ @Raw public void setName(String name) throws IllegalNameException { @@ -473,26 +444,23 @@ public class Worm extends GameObject { // endregion - // region move //=================================================================================== /** * Move the worm for the given number of steps. - * + * + * @throws IllegalArgumentException The cost of the step is bigger then the worms current action points. + * |cost > getActionPoints() * @post The newlocation of the worm is the furthest possible step the worm can take. Depending on its - * radius and surroundings. - * |new.getLocation() = FurthestLocationInDirection + * radius and surroundings. + * |new.getLocation() = FurthestLocationInDirection * @post The action points of the worm are substrated with the cost of the step. This cost is equal to - * the absolute value of the distance multiplied with the cosinus of the new direction plus the absolute - * value of 4 multiplied with the distance and the sinus of the new direction. - * |new.getActionPoints() == old.getActionPoints() - (abs(distance * cos(new.getOrientation()) + abs(4 * distance * sin(new.getOrientation()))) + * the absolute value of the distance multiplied with the cosinus of the new direction plus the absolute + * value of 4 multiplied with the distance and the sinus of the new direction. + * |new.getActionPoints() == old.getActionPoints() - (abs(distance * cos(new.getOrientation()) + abs(4 * distance * sin(new.getOrientation()))) * @post Let the worm eat if necessary. - * |checkEat() - * - * @throws IllegalArgumentException - * The cost of the step is bigger then the worms current action points. - * |cost > getActionPoints() + * |checkEat() */ public void move() throws IllegalArgumentException { @@ -505,37 +473,34 @@ public class Worm extends GameObject { if (cost > getActionPoints()) throw new IllegalArgumentException(); - setLocation(newLocation); - subtractActionPoints(cost); + setLocation(newLocation); + subtractActionPoints(cost); - checkEat(); + checkEat(); } /** * Gives the biggest step possible for the worm. - * - * @param direction - * The direction of the worm. - * @param maxDistance - * The maximum distance the worm can move. - * - * @return The furthest possible location for the given direction and maximum distance. - * The new location has to be passable. - * |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius)) + * + * @param direction The direction of the worm. + * @param maxDistance The maximum distance the worm can move. + * @return The furthest possible location for the given direction and maximum distance. + * The new location has to be passable. + * |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius)) */ public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) { - Coordinate currentLocation = getLocation(); - double radius = getRadius(); - World world = getWorld(); - double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2)); - if (step > radius) step = radius; - Coordinate nextLoc = currentLocation; + Coordinate currentLocation = getLocation(); + double radius = getRadius(); + World world = getWorld(); + double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2)); + if (step > radius) step = radius; + Coordinate nextLoc = currentLocation; - while(getDistance(currentLocation, nextLoc) < maxDistance) { + while (getDistance(currentLocation, nextLoc) < maxDistance) { - nextLoc = Coordinate.create(round((nextLoc.getX() + 0.1 * cos(direction)) * 100.0) / 100.0, + nextLoc = Coordinate.create(round((nextLoc.getX() + 0.1 * cos(direction)) * 100.0) / 100.0, round((nextLoc.getY() + step * sin(direction)) * 100.0) / 100.0); - if (!world.isPassable(nextLoc.toArray(), radius)) { + if (!world.isPassable(nextLoc.toArray(), radius)) { double max = radius * 0.1 + 0.001; while (!world.isAdjacent(nextLoc.toArray(), radius)) { max -= 0.01; @@ -554,14 +519,14 @@ public class Worm extends GameObject { /** * Gives the direction in which the furthest location is maximal. - * + * * @return The direction in which the furthest location is maximal. Where the distance between - * the center and a point on the circle (shape of worm) maximal is. The furthes location - * has to be passable. - * |for each direction: - * | if new.getDistance > maxDistance: - * | maxDistance == new.getDistance - * |result == maxDistance + * the center and a point on the circle (shape of worm) maximal is. The furthes location + * has to be passable. + * |for each direction: + * | if new.getDistance > maxDistance: + * | maxDistance == new.getDistance + * |result == maxDistance */ public double getFurthestLocationDirection() { Coordinate location = getLocation(); @@ -585,16 +550,13 @@ public class Worm extends GameObject { /** * Gives the distance between two coordinates. - * - * @param start - * The start coordinate of the equation. - * @param end - * The end coordinate of the equation. - * + * + * @param start The start coordinate of the equation. + * @param end The end coordinate of the equation. * @return the distance between the two given coordinates. The distance is equal to - * the square root of the square of the displacement of the x coordinate plus the - * square of the displacement of the y coordinate. - * |result == sqrt(pow(start.getX()-end.getX())+pow(start.getY()-end.getY())) + * the square root of the square of the displacement of the x coordinate plus the + * square of the displacement of the y coordinate. + * |result == sqrt(pow(start.getX()-end.getX())+pow(start.getY()-end.getY())) */ public double getDistance(Coordinate start, Coordinate end) { return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) + @@ -604,36 +566,32 @@ public class Worm extends GameObject { /** * The clashing worms their hit points are reduced. * - * @param basicWorm - * The worm who has moved. - * @param worm - * The worms who has clashed with the basicworm. - * - * @post All the worms that clashed their hitpoints are reduced. There is a random integer between - * 1 and 10 (= N). The lose of the smalles worm is equal to N divided by the orientation of the largest - * worm that is divided by the orientation of the smallest worm plus the orientatie of the largest one. - * |new.getHitPoints() == old.getHitPoints - (N/(largest.getOrientation()/(smallest.getOrientation() + largest.getOrientation())) - * @post The lose of the largest worm is N minus the lose of the smallest one. - * |new.getHitPoints() == old.getHitPoints - loseSmallest + * @param basicWorm The worm who has moved. + * @param worm The worms who has clashed with the basicworm. + * @post All the worms that clashed their hitpoints are reduced. There is a random integer between + * 1 and 10 (= N). The lose of the smalles worm is equal to N divided by the orientation of the largest + * worm that is divided by the orientation of the smallest worm plus the orientatie of the largest one. + * |new.getHitPoints() == old.getHitPoints - (N/(largest.getOrientation()/(smallest.getOrientation() + largest.getOrientation())) + * @post The lose of the largest worm is N minus the lose of the smallest one. + * |new.getHitPoints() == old.getHitPoints - loseSmallest */ public void collision(Worm basicWorm, Worm... worm) { - Worm largestWorm; - Worm smallestWorm; - for (Worm w1: worm) { - if (w1.getMass() > basicWorm.getMass()) { - largestWorm = w1; - smallestWorm = basicWorm; - } - else { - largestWorm = basicWorm; - smallestWorm = w1; - } - long total = 1 + (new Random().nextLong() * (10 - 1)); - long loseSmallest = (long) (total / (largestWorm.getOrientation() / (smallestWorm.getOrientation() + largestWorm.getOrientation()))); - long loseLargest = total - loseSmallest; - smallestWorm.incrementHitPoints(loseSmallest); - largestWorm.incrementHitPoints(loseLargest); - } + Worm largestWorm; + Worm smallestWorm; + for (Worm w1 : worm) { + if (w1.getMass() > basicWorm.getMass()) { + largestWorm = w1; + smallestWorm = basicWorm; + } else { + largestWorm = basicWorm; + smallestWorm = w1; + } + long total = 1 + (new Random().nextLong() * (10 - 1)); + long loseSmallest = (long) (total / (largestWorm.getOrientation() / (smallestWorm.getOrientation() + largestWorm.getOrientation()))); + long loseLargest = total - loseSmallest; + smallestWorm.incrementHitPoints(loseSmallest); + largestWorm.incrementHitPoints(loseLargest); + } } //=================================================================================== @@ -646,34 +604,32 @@ public class Worm extends GameObject { /** * Turns the worm with the given angle. * - * @param angle - * The angle that must be added to the orientation. - * @pre The angle to add must be between 0 and 2pi (including 0). - * |0 <= angle < 2pi + * @param angle The angle that must be added to the orientation. + * @pre The angle to add must be between 0 and 2pi (including 0). + * |0 <= angle < 2pi * @post The new orientation is the old orientation plus the given angle. - * |new.getOrientation() = this.getOrientation() + angle + * |new.getOrientation() = this.getOrientation() + angle * @post The resulting angle (= the new orientation) must be between 0 and 2pi (including 0). - * |0 <= new.getOrientation() < 2pi + * |0 <= new.getOrientation() < 2pi * @post Current actionPoints of action actionPoints should be reduced, for this there is another - * method with as parameter the given angle. - * |substractActionPoints(angle) + * method with as parameter the given angle. + * |substractActionPoints(angle) */ public void turn(double angle) { - assert canTurn(angle); - setOrientation(getOrientation() + angle); - subtractActionPoints(abs(angle)); + assert canTurn(angle); + setOrientation(getOrientation() + angle); + subtractActionPoints(abs(angle)); } /** * Return a boolean reflecting whether the worm can accept the given angle to turn. * - * @param angle - * The angle to be checked. + * @param angle The angle to be checked. * @return True if and only if the given angle is between 0 (including) and 2pi, - * the angle is a number and the current number of action points is high enough to turn. - * |result == ( (0 <= angle) && (angle < (2 * PI)) && - | (!Double.isNaN(angle)) && - | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) ) + * the angle is a number and the current number of action points is high enough to turn. + * |result == ( (0 <= angle) && (angle < (2 * PI)) && + * | (!Double.isNaN(angle)) && + * | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) ) */ private boolean canTurn(double angle) { double currentAngle = getOrientation(); @@ -691,7 +647,7 @@ public class Worm extends GameObject { /** * This constant contains the gravity. - * */ + */ public static final double G = 5.0; /** @@ -702,25 +658,23 @@ public class Worm extends GameObject { /** * Let the worm jump. * + * @throws IllegalStateException if the current action actionPoints is equal to 0 or the orientation is more then + * pi the worm can not jump + * |!canJump() * @post the new x coordinate is equal to the old x coordinate plus the jump velocity multiplied with the - * jump time multiplied with the cosinus of the orientation - * |new.getLocation().getX() = old.getLocation.getX() + jumpVelocity * jumpTime * cos(getOrientation()) + * jump time multiplied with the cosinus of the orientation + * |new.getLocation().getX() = old.getLocation.getX() + jumpVelocity * jumpTime * cos(getOrientation()) * @post the new y coordinate is equal to the old y coordinate plus the jump velocity multiplied with the - * jump time multiplied with the sinus of the orientation minus the constant of gravity multiplied with - * the square of the jump time divided with 2. - * |new.getLocation().getY() = old.getLocation().getY() + jumpVelocity * jumpTime * sin(getOrientation()) - (G * jumpTime^2) / 2.0 + * jump time multiplied with the sinus of the orientation minus the constant of gravity multiplied with + * the square of the jump time divided with 2. + * |new.getLocation().getY() = old.getLocation().getY() + jumpVelocity * jumpTime * sin(getOrientation()) - (G * jumpTime^2) / 2.0 * @post the current action actionPoints should be 0 after a jump - * |setActionPoints(0) + * |setActionPoints(0) * @post If the new location is no valid location, the worm has to be terminated. - * |if (! isValidLocation(new.getLocation()): - * | terminate(); + * |if (! isValidLocation(new.getLocation()): + * | terminate(); * @post Let the worm eat if necessary. - * |checkEat() - * - * @throws IllegalStateException - * if the current action actionPoints is equal to 0 or the orientation is more then - * pi the worm can not jump - * |!canJump() + * |checkEat() */ public void jump() throws IllegalStateException { @@ -748,11 +702,10 @@ public class Worm extends GameObject { * Return the time the worm will jump. * * @return The time the worm will jump. The distance divided by the velocity multiplied - * with the cosinus of the orientation. - * |jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation)) - * @throws IllegalStateException - * Orientation is bigger then pi or action points is equal to 0. - * |getOrientation() >= PI || getActionPoints() == 0 + * with the cosinus of the orientation. + * |jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation)) + * @throws IllegalStateException Orientation is bigger then pi or action points is equal to 0. + * |getOrientation() >= PI || getActionPoints() == 0 */ public double jumpTime() { @@ -762,17 +715,15 @@ public class Worm extends GameObject { /** * Gives the location after a jump. * - * @param deltaTime - * The total time the worm should jump. + * @param deltaTime The total time the worm should jump. * @return Coordinate with the new location of the worm. The new x-coordinate is the old x-coordinate plus the jump velocity - * multiplied with the cosinus of the orientation multiplied with delta time. The new y-coordinate is the old y-coordinate plus - * the jump velocity multiplied with the sinus of the orientation multiplied with delta time minus 0,5 times the gravity multiplied - * with the second power of delta time. - * |Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime, - |getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)) - * @throws IllegalArgumentException() - * If the deltaTime is not a number or bigger then jumpTime or smaller then 0. - * |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) + * multiplied with the cosinus of the orientation multiplied with delta time. The new y-coordinate is the old y-coordinate plus + * the jump velocity multiplied with the sinus of the orientation multiplied with delta time minus 0,5 times the gravity multiplied + * with the second power of delta time. + * |Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime, + * |getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)) + * @throws IllegalArgumentException() If the deltaTime is not a number or bigger then jumpTime or smaller then 0. + * |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) */ public Coordinate jumpStep(double deltaTime) { if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) @@ -786,19 +737,19 @@ public class Worm extends GameObject { /** * Gives the time the worm should jump. - * + * * @return If the furthest location for the jump is founded the time is founded. Every step takes - * 0.05 seconds. The furthest location is where the world is passable. - * |v = jumpVelocity; t = 0; a = getoriantation() - * |while (x = loc.getX() + v * t * cos(a)) && (y = loc.getY() + v * t * sin(a) - (G * t * t) / 2.0) - * | t += 0.05 - * | while (! new.Loc.isPassable()) - * | t -= 0.01 - * | newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0) - * | if (! new.Loc.isValidLocation) - * | break - * | break - * |result == t + * 0.05 seconds. The furthest location is where the world is passable. + * |v = jumpVelocity; t = 0; a = getoriantation() + * |while (x = loc.getX() + v * t * cos(a)) && (y = loc.getY() + v * t * sin(a) - (G * t * t) / 2.0) + * | t += 0.05 + * | while (! new.Loc.isPassable()) + * | t -= 0.01 + * | newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0) + * | if (! new.Loc.isValidLocation) + * | break + * | break + * |result == t */ private double calcJumpTime() { double v = jumpVelocity(); @@ -810,7 +761,7 @@ public class Worm extends GameObject { double radius = getRadius(); Coordinate newLoc; - while(true) { + while (true) { t += 0.05; double x = loc.getX() + v * t * cos(a); @@ -864,8 +815,8 @@ public class Worm extends GameObject { * Return a boolean whether the worm can jump or not. * * @return True if and only if the action actionPoints is bigger then 0 and the orientation - * is lower then pi. - * |result == getActionPoints() > 0 && getOrientation() < PI + * is lower then pi. + * |result == getActionPoints() > 0 && getOrientation() < PI */ private boolean canJump() { return getActionPoints() > 0 && getOrientation() < PI; @@ -874,11 +825,10 @@ public class Worm extends GameObject { /** * Return the distance the worm will jump. * - * @param v - * The velocity of the jump. + * @param v The velocity of the jump. * @return The distance the worm will jump. The distance is equal to the velocity powered by 2 multiplied - * with the sinus of 2 times the orientation en this divided with the gravity. - * |(pow(v, 2) * sin(2 * getOrientation())) / G + * with the sinus of 2 times the orientation en this divided with the gravity. + * |(pow(v, 2) * sin(2 * getOrientation())) / G */ private double jumpDistance(double v) { return (pow(v, 2) * sin(2 * getOrientation())) / G; @@ -886,13 +836,11 @@ public class Worm extends GameObject { /** * Returns of the jump distance valid is. - * - * @param distance - * The distance of the jump. - * - * @return True if and only if the absolute value of the distance bigger or equal is - * to the radius. - * |result == (abs(distance) >= getRadius()) + * + * @param distance The distance of the jump. + * @return True if and only if the absolute value of the distance bigger or equal is + * to the radius. + * |result == (abs(distance) >= getRadius()) */ public boolean canHaveAsJumpDistance(double distance) { @@ -903,53 +851,47 @@ public class Worm extends GameObject { * Return the velocity of the jump. * * @return The velocity of the jump. The force of the jump is equal to 5 multiplied with the - * current action actionPoints plus the mass multiplied with the gravity. Therefrom the velocity - * is equal to the force divided with the mass, multiplied with the time te force takes. - * |force = 5 * getActionPoints() + getMass() * G - * |velocity = (force / getMass()) * FORCE_TIME + * current action actionPoints plus the mass multiplied with the gravity. Therefrom the velocity + * is equal to the force divided with the mass, multiplied with the time te force takes. + * |force = 5 * getActionPoints() + getMass() * G + * |velocity = (force / getMass()) * FORCE_TIME */ private double jumpVelocity() { double force = 5 * getActionPoints() + getMass() * G; return force / getMass() * FORCE_TIME; } - + /** * Decreases the hit points of the attacked worm (chosen by a coin). - * - * @param newLocation - * The new location of the jumped worm. - * @param oldLocation - * The old location of the jumped worm. - * @param worm1 - * The jumped worm. - * @param worm2 - * The worms who stand on the same place (or parial) of the jumped worm. - * - * @post the attacked worms hit points are decreased with random value between 1 and 10 multiplied - * with the rate of the radius of the attacker and the radius of the attacked worm. - * |new.attackedWorm.getHitPoints() == old.attackedWorm.getHitPoints() - 10 * ((attacker.getRadius())/(attackedWorm.getRadius())) + * + * @param newLocation The new location of the jumped worm. + * @param oldLocation The old location of the jumped worm. + * @param worm1 The jumped worm. + * @param worm2 The worms who stand on the same place (or parial) of the jumped worm. + * @post the attacked worms hit points are decreased with random value between 1 and 10 multiplied + * with the rate of the radius of the attacker and the radius of the attacked worm. + * |new.attackedWorm.getHitPoints() == old.attackedWorm.getHitPoints() - 10 * ((attacker.getRadius())/(attackedWorm.getRadius())) */ public void fight(Coordinate newLocation, Coordinate oldLocation, Worm worm1, Worm... worm2) { - if (! worm1.isTerminated() && newLocation != oldLocation) { - for (Worm wormB: worm2) { - Worm attackedWorm; - Worm attacker; - Random rand = new Random(); - int coin = rand.nextInt(2); - if (coin == 1) { - attackedWorm = worm1; - attacker = wormB; - } - else { - attackedWorm = wormB; - attacker = worm1; - } - double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius())); - long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; - attackedWorm.decreaseHitPoints(loseAttackedWorm); - } - } + if (!worm1.isTerminated() && newLocation != oldLocation) { + for (Worm wormB : worm2) { + Worm attackedWorm; + Worm attacker; + Random rand = new Random(); + int coin = rand.nextInt(2); + if (coin == 1) { + attackedWorm = worm1; + attacker = wormB; + } else { + attackedWorm = wormB; + attacker = worm1; + } + double N = 10 * ceil((attacker.getRadius()) / (attackedWorm.getRadius())); + long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; + attackedWorm.decreaseHitPoints(loseAttackedWorm); + } + } } @@ -961,9 +903,10 @@ public class Worm extends GameObject { /** * Return the current hit points of the worm. - * The hit points identifies the strength of the worm. + * The hit points identifies the strength of the worm. */ - @Basic @Raw + @Basic + @Raw public long getHitPoints() { return this.hitPoints; } @@ -971,14 +914,13 @@ public class Worm extends GameObject { /** * Set the current hit points of the worm to the given points. * - * @param hitPoints - * The new points for the worm. - * @post If the given points are equal or lower then zero, the worm should be dead. - * Otherwise the current hit points are set to the given points. - * |if (hitpoints <= 0) - * | terminate(); - * |else - * | new.getHitPoints() == hitpoints + * @param hitPoints The new points for the worm. + * @post If the given points are equal or lower then zero, the worm should be dead. + * Otherwise the current hit points are set to the given points. + * |if (hitpoints <= 0) + * | terminate(); + * |else + * | new.getHitPoints() == hitpoints */ @Raw public void setHitPoints(long hitPoints) { @@ -995,29 +937,26 @@ public class Worm extends GameObject { /** * Decrease the hit points with the given value. * - * @param value - * The value that should be decreased. - * @post The current hit points should be decreased with the given value. - * |new.getHitPoints() == old.getHitPoints() - value + * @param value The value that should be decreased. + * @post The current hit points should be decreased with the given value. + * |new.getHitPoints() == old.getHitPoints() - value */ public void decreaseHitPoints(long value) { - setHitPoints(getHitPoints() - value); + setHitPoints(getHitPoints() - value); } /** * Increment the hit points with the given value. - * - * @param value - * The value that schould be added. - * @post The current hit points should be increaseed with the given value. - * |new.getHitPoints() == old.getHitPoints() + value + * + * @param value The value that schould be added. + * @post The current hit points should be increaseed with the given value. + * |new.getHitPoints() == old.getHitPoints() + value */ public void incrementHitPoints(long value) { - setHitPoints(getHitPoints() + value); + setHitPoints(getHitPoints() + value); } - //=================================================================================== // endregion @@ -1026,19 +965,19 @@ public class Worm extends GameObject { /** * Set the location to the first position just above impassable domain - * + * * @post The location is set to the first location beneath the old location just above impassable domain. - * If the worm is the partial out of the world, the worm should be terminated. - * |while canFall() (y -= 0.01): - * | new.getLocation() == (oldLocation.getX(), (y * 100.0) / 100.0) - * | if (y - radius < 0): - * | terminate() + * If the worm is the partial out of the world, the worm should be terminated. + * |while canFall() (y -= 0.01): + * | new.getLocation() == (oldLocation.getX(), (y * 100.0) / 100.0) + * | if (y - radius < 0): + * | terminate() * @post The hit points of the worm schould be decreased with 3 times the displacement of the worm. - * |new.getHitPoints() == old.getHitPoints() - (3 * (oldLocation.getY() - getLocation().getY())) + * |new.getHitPoints() == old.getHitPoints() - (3 * (oldLocation.getY() - getLocation().getY())) * @post The worm has to eat when that is needed. - * |checkEat(); + * |checkEat(); */ - public void fall() { + public void fall() { double height = getWorld().getHeight() - getRadius(); Coordinate oldLocation = getLocation(); if (canFall()) { @@ -1062,63 +1001,60 @@ public class Worm extends GameObject { decreaseHitPoints(cost); checkEat(); - } - - /** - * Returns of the worm can fall or not. - * - * @return True if and only if the domain just beneath the worm is passable. - * |result == ! getWorld().isAdjacent(center, getRadius()) - */ - public boolean canFall() { - double[] center = {getLocation().getX(), getLocation().getY()}; - return ! getWorld().isAdjacent(center, getRadius()); - } - - /** - * Changes the hit points of the clashing worms. - * - * @param fallingWorm - * The worm who has fallen. - * @param worm - * The worm on which the falling worm has fallen. - * - * @post the falling worm his hit points are incremented with half of the hit points of the - * stationary worm. - * |new.getHitPoints() == old.getHitPoints() + stationaryWorm.getHitPoints()/2 - * @post the stationary worm his hit points are decreased with half of his hit points. - * |new.getHitPoints() == old.getHitPoints() - old.getHitPoints()/2 - */ - public void collisionFall(Worm fallingWorm, Worm... worm) { - for (Worm stationaryWorm: worm) { - long value = stationaryWorm.getHitPoints()/2; - fallingWorm.incrementHitPoints(value); - stationaryWorm.decreaseHitPoints(value); - } } - - //=================================================================================== + + /** + * Returns of the worm can fall or not. + * + * @return True if and only if the domain just beneath the worm is passable. + * |result == ! getWorld().isAdjacent(center, getRadius()) + */ + public boolean canFall() { + double[] center = {getLocation().getX(), getLocation().getY()}; + return !getWorld().isAdjacent(center, getRadius()); + } + + /** + * Changes the hit points of the clashing worms. + * + * @param fallingWorm The worm who has fallen. + * @param worm The worm on which the falling worm has fallen. + * @post the falling worm his hit points are incremented with half of the hit points of the + * stationary worm. + * |new.getHitPoints() == old.getHitPoints() + stationaryWorm.getHitPoints()/2 + * @post the stationary worm his hit points are decreased with half of his hit points. + * |new.getHitPoints() == old.getHitPoints() - old.getHitPoints()/2 + */ + public void collisionFall(Worm fallingWorm, Worm... worm) { + for (Worm stationaryWorm : worm) { + long value = stationaryWorm.getHitPoints() / 2; + fallingWorm.incrementHitPoints(value); + stationaryWorm.decreaseHitPoints(value); + } + } + + //=================================================================================== // endregion // region team //=================================================================================== + /** * Return the current team of the worm. - * The team identifies the partners of the worm. + * The team identifies the partners of the worm. */ - public Team getTeam() { + public Team getTeam() { return this.team; } - /** - * Set the current team of the worm to the given team. + /** + * Set the current team of the worm to the given team. * - * @param team - * The new team for the worm. - * @post The current team of the worm is set to the given team. - * |new.getTeam() == team - */ + * @param team The new team for the worm. + * @post The current team of the worm is set to the given team. + * |new.getTeam() == team + */ public void setTeam(Team team) { this.team = team; } @@ -1137,24 +1073,22 @@ public class Worm extends GameObject { /** * The worm eats food and grows. - * - * @param food - * The food the worm has eaten. - * - * @post The eaten food is terminated. - * |food.terminate(); - * @post The worm's radius is increased with 10%. - * |new.getRadius() = 0,10 * old.getRadius() + old.getRadius() - * @post The worm has moved so that the worm stands full on a valid location (passable). - * |new.getLocation() == isPassable(Location, radius) - * @post If there is no valid new location, the worm has explode. - * |for each new location; (new.getLocation() != isPassable): - * | terminate() - * @post If the circular area of the worm is not long full in the world, the worm leaves the world. - * |if (new.getLocation != isValidLocation()): - * | terminate() - * @post Let the worm eat if necessary. - * |checkEat() + * + * @param food The food the worm has eaten. + * @post The eaten food is terminated. + * |food.terminate(); + * @post The worm's radius is increased with 10%. + * |new.getRadius() = 0,10 * old.getRadius() + old.getRadius() + * @post The worm has moved so that the worm stands full on a valid location (passable). + * |new.getLocation() == isPassable(Location, radius) + * @post If there is no valid new location, the worm has explode. + * |for each new location; (new.getLocation() != isPassable): + * | terminate() + * @post If the circular area of the worm is not long full in the world, the worm leaves the world. + * |if (new.getLocation != isValidLocation()): + * | terminate() + * @post Let the worm eat if necessary. + * |checkEat() */ public void eat(Food food, boolean terminate) { @@ -1223,15 +1157,15 @@ public class Worm extends GameObject { /** * If the worm can eat food (depends on his location), the worms eats the food. - * - * @post The worm has eaten the food. - * |eat(food) + * + * @post The worm has eaten the food. + * |eat(food) */ public void checkEat() { World world = getWorld(); List foodList = world.getFoodList(); - for (Food food: foodList) { + for (Food food : foodList) { if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) { eat(food); } @@ -1239,54 +1173,45 @@ public class Worm extends GameObject { } - // region firing and projectiles //=================================================================================== - private double[] locationProj; - private double massProj; - private long hitpointsProj; - private double forceProj; - - public void fire() { - if (getActionPoints() >= 30 && getWorld() != null) { - Random r = new Random(); - int random = r.nextInt((1) + 1); - locationProj[0] = cos(getOrientation()) * getRadius(); - locationProj[1] = sin(getOrientation()) * getRadius(); - if (random == 0) { - propertiesRifle(); - decreaseActionPoints(10); - } - else { - propertiesBazooka(); - decreaseActionPoints(25); - } - //new Projectile(this.getWorld(), locationProj, massProj, hitpointsProj, forceProj); - } - } - - public void propertiesRifle() { - massProj = 10; - forceProj = 1.5; - Random r = new Random(); - hitpointsProj = 0 + r.nextInt((10-0)/2) *2; - } - - public void propertiesBazooka() { - massProj = 300; - forceProj = 2.5 + getActionPoints()/8; - Random r = new Random(); - hitpointsProj = 0 + r.nextInt((10-0)/(2+1)) *(2+1); - } - - public void hitByRifle() { - decreaseHitPoints(hitpointsProj); - } - - public void hitByBazooka() { - decreaseHitPoints(hitpointsProj * (long) forceProj); + private double[] locationProj; + private double massProj; + private long hitpointsProj; + private double forceProj; + + public Projectile fire() { + if (getActionPoints() >= 30 && getWorld() != null) { + + int random = ThreadLocalRandom.current().nextInt(2); + Coordinate projLocation = Coordinate.create(cos(getOrientation()) * getRadius(), sin(getOrientation()) * getRadius()); + + if (random == 0) { + decreaseActionPoints(10); + return new Rifle(getWorld(), projLocation,getOrientation()); + } else { + double force = Bazooka.calcForce(getActionPoints()); + decreaseActionPoints(25); + return new Bazooka(getWorld(), projLocation, force, getOrientation()); + } + } + + return null; } + +// public void hitByRifle() { +// decreaseHitPoints(hitpointsProj); +// } +// +// public void hitByBazooka() { +// decreaseHitPoints(hitpointsProj * (long) forceProj); +// } + + // TODO We gaan maar 1 hit function gebruiken + + + // =================================================================================== // endregion }