From 8aa2699d6972c4a54f0a76554bf4534b809b4e6a Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 23 May 2018 15:59:29 +0200 Subject: [PATCH] fix projectile, refactoring --- OGP1718-Worms/src/worms/model/Bazooka.java | 7 +- OGP1718-Worms/src/worms/model/GameObject.java | 6 +- OGP1718-Worms/src/worms/model/Projectile.java | 53 +++++++-------- OGP1718-Worms/src/worms/model/Rifle.java | 5 ++ OGP1718-Worms/src/worms/model/World.java | 5 +- OGP1718-Worms/src/worms/model/Worm.java | 67 +++++-------------- 6 files changed, 58 insertions(+), 85 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Bazooka.java b/OGP1718-Worms/src/worms/model/Bazooka.java index c9eba80..c236014 100644 --- a/OGP1718-Worms/src/worms/model/Bazooka.java +++ b/OGP1718-Worms/src/worms/model/Bazooka.java @@ -19,6 +19,11 @@ public class Bazooka extends Projectile { return nb; } + @Override + protected int getImpactHitPoints() { + return this.hitPoints * (int) this.force; + } + @Override protected void setHitPoints(int value) { if (value > 7) value = 7; @@ -26,8 +31,6 @@ public class Bazooka extends Projectile { if (value == 0) value++; else value--; } - double force = getForce(); - value = value* (int) force; super.hitPoints = value; } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 35e38ac..e694ef9 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -258,7 +258,11 @@ public abstract class GameObject { Coordinate otherLocation = o.getLocation(); - return Math.sqrt(Math.pow((otherLocation.getX() - location.getX()), 2) + Math.pow((otherLocation.getY() - location.getY()), 2)) - o.getRadius() - getRadius(); + 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; + } + 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) { diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index 7a5736b..ab02747 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -26,7 +26,7 @@ public abstract class Projectile extends GameObject { public double getForce() { return this.force; } - private final double force; + protected final double force; protected int hitPoints; @@ -37,6 +37,8 @@ public abstract class Projectile extends GameObject { return this.hitPoints; } + protected abstract int getImpactHitPoints(); + protected abstract void setHitPoints(int value) throws IllegalArgumentException; public void incrementHitPoints(int value) { @@ -83,7 +85,7 @@ public abstract class Projectile extends GameObject { return (getForce() / (getMass() / 1000)) * FORCE_TIME; } - public double getJumpTime(double jumpTimeStep) { + public double getJumpTime(double timeStep) { World world = getWorld(); if (world == null) { @@ -93,32 +95,25 @@ public abstract class Projectile extends GameObject { double v = jumpVelocity(); double time = 0; double a = getOrientation(); - Coordinate loc = getLocation(); double radius = getRadius(); - Coordinate newLoc; + List worms = world.getGameObjectsByClass(Worm.class); - while (true) { - time += jumpTimeStep; - double x = loc.getX() + v * time * cos(a); - double y = loc.getY() + v * time * sin(a) - (G * time * time) / 2.0; - newLoc = Coordinate.create(x, y); - if (! world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) { - while (true) { - time -= jumpTimeStep; - newLoc = Coordinate.create(loc.getX() + v * time * cos(a), loc.getY() + v * time * sin(a) - (G * time * time) / 2.0); - if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() || - newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) { - if (getDistance(loc, newLoc) < getRadius()) throw new IllegalStateException(); - break; - } - } - break; - } - if ((int) round(time) == 10) { - throw new RuntimeException(); - } - } - return time; + 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), + 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() || + newLoc.getY() + radius > world.getHeight() || !world.isPassable(newLoc, radius) || + worms.stream().anyMatch(w -> w.getDistance(newLoc, this.radius) < 0)) { + return time; + } + } } public double getDistance(Coordinate start, Coordinate end) { @@ -138,11 +133,11 @@ public abstract class Projectile extends GameObject { double v = jumpVelocity(); double t = getJumpTime(jumpTimeStep); double a = getOrientation(); - Coordinate newLocation = Coordinate.create(getLocation().toArray()); + + 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); } @@ -169,7 +164,7 @@ public abstract class Projectile extends GameObject { public void hit(Worm worm){ - worm.decreaseHitPoints(getHitPoints()); + worm.decreaseHitPoints(getImpactHitPoints()); terminate(); } } \ 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 7bfd6f7..afdaf66 100644 --- a/OGP1718-Worms/src/worms/model/Rifle.java +++ b/OGP1718-Worms/src/worms/model/Rifle.java @@ -19,6 +19,11 @@ public class Rifle extends Projectile { return ThreadLocalRandom.current().nextInt(1,6) * 2; } + @Override + protected int getImpactHitPoints() { + return this.hitPoints; + } + @Override protected void setHitPoints(int value) { if (value > 10) value = 10; diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 5ec719f..42c4e79 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -560,7 +560,6 @@ public class World { lgWorm = (Worm) item2; smWorm = (Worm) item1; } - long lgHitPoints = lgWorm.getActionPoints(); if (lgHitPoints < 5) { lgWorm.setActionPoints(0); @@ -580,8 +579,8 @@ public class World { else if (item1 instanceof Worm && item2 instanceof Projectile || item1 instanceof Projectile && item2 instanceof Worm) { - Worm worm = null; - Projectile proj = null; + Worm worm; + Projectile proj; if (item1 instanceof Worm) { worm = (Worm) item1; proj = (Projectile) item2; diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index cf1d273..81a3b2b 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -110,7 +110,7 @@ public class Worm extends GameObject { throws IllegalArgumentException { super(world, location, radius); - this.orientation = orientation; + setOrientation(orientation); if (!isValidMinRadius(minRadius)) throw new IllegalArgumentException("Invalid min radius"); @@ -1088,57 +1088,24 @@ public class Worm extends GameObject { World world = this.world; Coordinate location = this.location; if (!world.isAdjacent(location, radius)) { - Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY()); - if (world.isAdjacent(newLoc, radius)) { - this.radius = radius; - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX() - changeRadius, location.getY()); - if (world.isAdjacent(newLoc, radius)) { - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX(), location.getY() - changeRadius); - if (world.isAdjacent(newLoc, radius)) { - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX(), location.getY() + changeRadius); - if (world.isAdjacent(newLoc, radius)) { - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() - changeRadius); - if (world.isAdjacent(newLoc, radius)) { - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() + changeRadius); - if (world.isAdjacent(newLoc, radius)) { - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() - changeRadius); - if (world.isAdjacent(newLoc, radius)) { - setLocation(newLoc); - setRadius(radius); - return; - } - newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() + changeRadius); - if (!world.isAdjacent(newLoc, radius)) { - terminate(); - return; + + for (double x = -1.0; x < 2; x++) { + for (double y = -1.0; y < 2; y++) { + Coordinate newLoc = Coordinate.create(location.getX() + changeRadius * x, location.getY() + changeRadius * y); + if (world.isAdjacent(newLoc, radius)) { + this.radius = radius; + setLocation(newLoc); + setRadius(radius); + return; + } + } } + terminate(); + } else { + setLocation(location); + setRadius(radius); } - setLocation(location); - setRadius(radius); + } /**