From f3d6d971b9d0f49f9a86b69513145b0f0e9d1c99 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 23 May 2018 21:03:10 +0200 Subject: [PATCH] meer refactoring --- OGP1718-Worms/src/worms/model/Bazooka.java | 7 +- OGP1718-Worms/src/worms/model/GameObject.java | 247 +++++++++++------- OGP1718-Worms/src/worms/model/Projectile.java | 236 ++++++++++------- OGP1718-Worms/src/worms/model/Rifle.java | 4 +- OGP1718-Worms/src/worms/model/Worm.java | 5 +- 5 files changed, 295 insertions(+), 204 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Bazooka.java b/OGP1718-Worms/src/worms/model/Bazooka.java index c236014..c88c6f3 100644 --- a/OGP1718-Worms/src/worms/model/Bazooka.java +++ b/OGP1718-Worms/src/worms/model/Bazooka.java @@ -8,8 +8,8 @@ import static java.lang.Math.cos; import static java.lang.Math.sin; public class Bazooka extends Projectile { - protected Bazooka(World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double force) { - super(world, wormLocation, wormOrientation, wormRadius, 300, force); + protected Bazooka(Worm worm) { + super(worm, 300, calcForce(worm.getActionPoints())); } @Override @@ -35,7 +35,8 @@ public class Bazooka extends Projectile { } public static double calcForce(double actionPoints) { - double hp = 2.5 + (actionPoints - 25) % 8.0; + double hp = 2.5 + + actionPoints % 8.0; if (hp > 9.5) return 9.5; return hp; } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 46da5ed..7995e46 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -6,23 +6,9 @@ import worms.util.Coordinate; import static java.lang.Math.*; public abstract class GameObject { - - /** - * - */ - protected double mass; - /** - * - */ - protected World world; - /** - * - */ - protected boolean terminated = false; - /** - * this variable contains the location of the worm (a Coordinate) - */ - protected Coordinate location; + + // region Constructor + //=================================================================================== /** * @param world @@ -60,16 +46,15 @@ public abstract class GameObject { setLocation(location); setRadius(radius); if (isValidWorld(world)) world.add(this); - setWorld(world); + this.world = world; } - /** - * @param world - * @return ... |result == !world.hasActiveGame() && !world.isTerminated() - */ - public static boolean isValidWorld(World world) { - return world != null && !world.hasActiveGame() && !world.isTerminated(); - } + // =================================================================================== + // endregion + + + // region world + //=================================================================================== /** * @return ... @@ -79,6 +64,16 @@ public abstract class GameObject { return this.world; } + + /** + * @param world + * @return ... |result == !world.hasActiveGame() && !world.isTerminated() + */ + public static boolean isValidWorld(World world) { + return world != null && !world.hasActiveGame() && !world.isTerminated(); + } + + /** * @param world * @post ... @@ -87,7 +82,7 @@ public abstract class GameObject { * |else: * | this.world = world */ - public void setWorld(World world) { + public void setWorld(World world) throws IllegalArgumentException { if (world == null) { this.world = null; return; @@ -97,24 +92,16 @@ public abstract class GameObject { } /** - * @return ... - * |result == this.terminated + * */ - public boolean isTerminated() { - return this.terminated; - } + protected World world; - /** - * @post ... - * |this.terminated = true - * |getWorld().remove(this) - */ - public void terminate() { - this.terminated = true; - if (this.world != null) { - this.world.remove(this); - } - } + // =================================================================================== + // endregion + + + // region location + //=================================================================================== /** * Return the location of the game object @@ -125,6 +112,13 @@ public abstract class GameObject { return this.location; } + /** + * @return ... |result == this.location.toArray() + */ + public double[] getLocationArray() { + return this.location.toArray(); + } + /** * @param location * @throws IllegalArgumentException ... @@ -145,13 +139,6 @@ public abstract class GameObject { this.location = location; } - /** - * @return ... |result == this.location.toArray() - */ - public double[] getLocationArray() { - return this.location.toArray(); - } - /** * set the location of the worm to the given location * @@ -163,12 +150,49 @@ public abstract class GameObject { */ protected void setLocation(double[] location) throws IllegalArgumentException { - Coordinate locationCoordinate = Coordinate.create(location); - - if (!isValidLocation(locationCoordinate)) throw new IllegalArgumentException(); - this.location = locationCoordinate; + setLocation(Coordinate.create(location)); } + + /** + * @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)) + */ + protected boolean isValidLocation(Coordinate location) { + double radius = getRadius(); + + if (world == 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().isPassable(location); + } + + /** + * this variable contains the location of the GameObject (a Coordinate) + */ + protected Coordinate location; + + // =================================================================================== + // endregion + + + // region radius + //=================================================================================== + /** * Return the radius of the game object * the radius of the game object expresses half of the @@ -206,32 +230,12 @@ public abstract class GameObject { return !Double.isNaN(radius) && radius > 0; } - /** - * @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)) - */ - protected boolean isValidLocation(Coordinate location) { - double radius = getRadius(); + // =================================================================================== + // endregion - if (world == 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().isPassable(location); - } + + // region mass + //=================================================================================== /** * @return ... |result == this.mass @@ -241,7 +245,7 @@ public abstract class GameObject { } /** - * set the mass of the worm to the given mass (dependent on the radius) + * set the mass of the GameObject 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 @@ -253,28 +257,28 @@ public abstract class GameObject { this.mass = rho * (4.0 / 3.0 * PI * pow(radius, 3.0)); } + /** + * + */ + protected double mass; + + // =================================================================================== + // endregion + + + + // region distance calc + //=================================================================================== public double getDistance(GameObject o) { - Coordinate otherLocation = o.getLocation(); - - return Math.round((Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2)) - o.getRadius() - getRadius()) * 100000000.0) / 100000000.0; + return getDistance(o.getLocation(), this.radius); } + public double getDistance(Coordinate otherLocation, double radius) { - return Math.round((Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2)) - radius - getRadius()) * 100000000.0) / 100000000.0; - } - - public double getAngle(GameObject o) { - - if (o.equals(this)) return Double.NaN; - - double x1 = getLocation().getX(); - double y1 = getLocation().getY(); - double x2 = o.getLocation().getX(); - double y2 = o.getLocation().getY(); - - return Math.abs(Math.atan(Math.abs(x1 - x2) / Math.abs(y1 - y2)) - Math.PI / 2.0); + return Math.round((Math.sqrt(Math.pow((otherLocation.getX() - this.location.getX()), 2) + + Math.pow((otherLocation.getY() - this.location.getY()), 2)) - radius - this.radius) * 10000.0) / 10000.0; } /** @@ -291,4 +295,57 @@ public abstract class GameObject { return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) + Math.pow(Math.abs(start.getY() - end.getY()), 2)); } + + /** + * Returns the angle between (dit?) this and the specified GameObject + * + * @param o + * @return + */ + public double getAngle(GameObject o) { + + if (o.equals(this)) return Double.NaN; + + Coordinate otherLoc = o.getLocation(); + + return Math.abs(Math.atan(Math.abs(this.location.getX() - otherLoc.getX()) / + Math.abs(this.location.getY() - otherLoc.getY())) - Math.PI / 2.0); + } + + // =================================================================================== + // endregion + + + // region destructor + //=================================================================================== + + /** + * @return ... + * |result == this.terminated + */ + public boolean isTerminated() { + return this.terminated; + } + + /** + * @post ... + * |this.terminated = true + * |getWorld().remove(this) + */ + public void terminate() { + this.terminated = true; + if (this.world != null) { + this.world.remove(this); + } + } + + /** + * + */ + private boolean terminated = false; + + + // =================================================================================== + // endregion + } \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index 6b96561..6a12637 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -1,165 +1,199 @@ package worms.model; import static java.lang.Math.*; - import worms.util.Coordinate; - import java.util.List; public abstract class Projectile extends GameObject implements IJumpable { private static final int rho = 7800; - protected Projectile (World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double mass, double force) { - super(world, calcLocation(wormLocation, wormOrientation, wormRadius, mass), calcRadius(mass)); + // region constructor + //=================================================================================== + + public static final double G = 5.0; + public static final double FORCE_TIME = 0.5; + + protected Projectile (Worm worm, double mass, double force) { + super(worm.getWorld(), calcLocation(worm.getLocation(), worm.getOrientation(), worm.getRadius(), mass), calcRadius(mass)); super.mass = mass; this.force = force; - this.orientation = wormOrientation; + this.orientation = worm.getOrientation(); this.hitPoints = getRandomHitPoints(); } - - public static double calcRadius(double mass) { - return pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * PI))), 1.0 / 3.0); - } - - public double getForce() { - return this.force; - } - protected final double force; + // =================================================================================== + // endregion - protected int hitPoints; + // region radius / orientation / force + //=================================================================================== - protected abstract int getRandomHitPoints(); + public double getOrientation() { + return orientation; + } - public int getHitPoints() { - return this.hitPoints; - } + public static double calcRadius(double mass) { + return pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * PI))), 1.0 / 3.0); + } - protected abstract int getImpactHitPoints(); + public double getForce() { + return this.force; + } - protected abstract void setHitPoints(int value) throws IllegalArgumentException; + protected final double force; - public void incrementHitPoints(int value) { - setHitPoints(this.hitPoints + value); - } + private final double orientation; - private final double orientation; + // =================================================================================== + // endregion - public double getOrientation() { - return orientation; - } - @Override - protected boolean isValidLocation(Coordinate location) { - double radius = getRadius(); + // region hitpoints + //=================================================================================== - 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); - } + public int getHitPoints() { + return this.hitPoints; + } - public static final double G = 5.0; - - public static final double FORCE_TIME = 0.5; - - public Coordinate jumpStep(double elapsedTime) { - if (Double.isNaN(elapsedTime) || elapsedTime < 0) - throw new IllegalArgumentException(); - - double velocity = this.jumpVelocity(); + public void incrementHitPoints(int value) { + setHitPoints(this.hitPoints + value); + } - return Coordinate.create(getLocation().getX() + velocity * cos(getOrientation()) * elapsedTime, - getLocation().getY() + velocity * sin(getOrientation()) * elapsedTime - 0.5 * G * pow(elapsedTime, 2)); - } + public void hit(Worm worm){ - private double jumpVelocity() { - return (getForce() / (getMass() / 1000)) * FORCE_TIME; - } + worm.decreaseHitPoints(getImpactHitPoints()); + terminate(); + } - public double jumpTime(double timeStep) { + protected abstract void setHitPoints(int value) throws IllegalArgumentException; + + protected abstract int getImpactHitPoints(); + + protected abstract int getRandomHitPoints(); + + protected int hitPoints; + + // =================================================================================== + // endregion + + // region jump + //=================================================================================== + + public void jump(double jumpTimeStep) throws IllegalStateException { + if (!canJump()) + throw new IllegalStateException(); + + double v = jumpVelocity(); + double t = jumpTime(jumpTimeStep); + double a = getOrientation(); + + Coordinate newLocation = this.location; + + List worms = getWorld().getGameObjectsByClass(Worm.class); + + if (!getWorld().isAdjacent(getLocation(),getRadius())) { + newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), + getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0); + } + if (!isValidLocation(newLocation)) { + terminate(); + return; + } + setLocation(newLocation); + + for (Worm worm: worms) { + if (this.getDistance(worm) < 0) { + setLocation(newLocation); + hit(worm); + } + } + } + + + public double jumpTime(double timeStep) { World world = getWorld(); if (world == null) { throw new IllegalStateException("World cannot be null"); } - double v = jumpVelocity(); - double time = 0; - double a = getOrientation(); - double radius = getRadius(); - List worms = world.getGameObjectsByClass(Worm.class); + double v = jumpVelocity(); + double time = 0; + double a = getOrientation(); + double radius = getRadius(); + List worms = world.getGameObjectsByClass(Worm.class); - if (!world.isPassable(this.location) || worms.stream().anyMatch(w -> w.getDistance(this) < 0)) { - return 0.0; + if (!world.isPassable(this.location) || worms.stream().anyMatch(w -> w.getDistance(this) < 0)) { + return 0.0; } - while(true) { - time += timeStep; - Coordinate newLoc = Coordinate.create(this.location.getX() + v * time * cos(a), + while(true) { + time += timeStep; + Coordinate newLoc = Coordinate.create(this.location.getX() + v * time * cos(a), this.location.getY() + v * time * sin(a) - (G * time * time) / 2.0); - if (newLoc.getX() - radius < 0 || newLoc.getY() - radius < 0 || newLoc.getX() + radius > world.getWidth() || + if (newLoc.getX() - radius < 0 || newLoc.getY() - radius < 0 || newLoc.getX() + radius > world.getWidth() || newLoc.getY() + radius > world.getHeight() || !world.isPassable(newLoc, radius) || worms.stream().anyMatch(w -> w.getDistance(newLoc, this.radius) < 0)) { return time; } } - } + } + + + public Coordinate jumpStep(double elapsedTime) { + if (Double.isNaN(elapsedTime) || elapsedTime < 0) + throw new IllegalArgumentException(); + + double velocity = this.jumpVelocity(); + + return Coordinate.create(getLocation().getX() + velocity * cos(getOrientation()) * elapsedTime, + getLocation().getY() + velocity * sin(getOrientation()) * elapsedTime - 0.5 * G * pow(elapsedTime, 2)); + } private boolean canJump() { - return getOrientation() < PI; - } + return getOrientation() < PI; + } - public void jump(double jumpTimeStep) throws IllegalStateException { - if (!canJump()) - throw new IllegalStateException(); - double v = jumpVelocity(); - double t = jumpTime(jumpTimeStep); - double a = getOrientation(); + private double jumpVelocity() { + return (getForce() / (getMass() / 1000)) * FORCE_TIME; + } - Coordinate newLocation = this.location; + // =================================================================================== + // endregion - List worms = getWorld().getGameObjectsByClass(Worm.class); - if (!getWorld().isAdjacent(getLocation(),getRadius())) { - newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), - getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0); - } - if (!isValidLocation(newLocation)) { - terminate(); - return; - } - setLocation(newLocation); + // region location + //=================================================================================== - for (Worm worm: worms) { - if (this.getDistance(worm) < 0) { - setLocation(newLocation); - hit(worm); - } - } - } + @Override + protected boolean isValidLocation(Coordinate location) { + double radius = getRadius(); - private static Coordinate calcLocation(Coordinate wormLocation, double wormOrientation, double wormRadius, double mass) { + 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); + } + + public static Coordinate calcLocation(Coordinate wormLocation, double wormOrientation, double wormRadius, double mass) { double radius = calcRadius(mass); return Coordinate.create(wormLocation.getX() + cos(wormOrientation) * (wormRadius + radius), wormLocation.getY() + sin(wormOrientation) * (wormRadius + radius)); } - public void hit(Worm worm){ - worm.decreaseHitPoints(getImpactHitPoints()); - terminate(); - } + // =================================================================================== + // endregion + } \ 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 53b291a..c144564 100644 --- a/OGP1718-Worms/src/worms/model/Rifle.java +++ b/OGP1718-Worms/src/worms/model/Rifle.java @@ -10,8 +10,8 @@ import static java.lang.Math.sin; public class Rifle extends Projectile { - public Rifle(World world, Coordinate wormLocation, double wormOrientation, double wormRadius) { - super(world, wormLocation, wormOrientation, wormRadius, 10, 1.5); + public Rifle(Worm worm) { + super(worm, 10, 1.5); } @Override diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index b5e5d31..c1b7a53 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -1114,12 +1114,11 @@ public class Worm extends GameObject implements IJumpable{ if (random == 0) { decrementActionPoints(10); - return new Rifle(this.world, this.location, this.orientation, this.radius); + return new Rifle(this); } else { double force = Bazooka.calcForce(this.actionPoints); decrementActionPoints(25); - - return new Bazooka(this.world, this.location, this.orientation, this.radius, force); + return new Bazooka(this); } }