From c069051001b56d4f388407052c8750046b5f9bc7 Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Fri, 13 Apr 2018 18:33:04 +0200 Subject: [PATCH 1/3] =?UTF-8?q?fall=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OGP1718-Worms/src/worms/model/Worm.java | 26 ++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index ec3ec2c..d65297b 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -832,25 +832,29 @@ public class Worm extends GameObject { //=================================================================================== public void fall() { - double[] center = {getLocation().getX(), getLocation().getY()}; + double heigth = getWorld().getHeight() - getRadius(); Coordinate oldLocation = getLocation(); - double endY = getLocation().getY(); if (canFall()) { - for (double y = oldLocation.getY(); y <= (getWorld().getHeight() + 1); y--) { - if (y >= (getWorld().getHeight() + 1)) { - terminate(); - } + for (double y = oldLocation.getY(); y <= heigth; y = y - 0.1) { double[] newLoc = {oldLocation.getX(), y}; - setLocation(newLoc); - if (! canFall()) { - endY = y; + if (! getWorld().isPassable(newLoc, getRadius())) { + setLocation(newLoc); break; } - center[1] = y; +// for (double i = y; i >= y - 1; i = i - 0.1) { +// double[] newLocation = {oldLocation.getX(), i}; +// if (! getWorld().isPassable(newLocation, getRadius())) { +// setLocation(newLocation); +// endY = i; +// break; +// } +// } } + + terminate(); } - long cost = 3 * (long) Math.floor(oldLocation.getY() - endY); + long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY()); decreaseHitPoints(cost); } From 5171f369161a8f2c753c512486d6dd749fd30d31 Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Fri, 13 Apr 2018 18:35:43 +0200 Subject: [PATCH 2/3] fall --- OGP1718-Worms/src/worms/model/Worm.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index e801c9d..10e6756 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -842,16 +842,7 @@ public class Worm extends GameObject { setLocation(newLoc); break; } -// for (double i = y; i >= y - 1; i = i - 0.1) { -// double[] newLocation = {oldLocation.getX(), i}; -// if (! getWorld().isPassable(newLocation, getRadius())) { -// setLocation(newLocation); -// endY = i; -// break; -// } -// } } - terminate(); } From 1fff8ee8b9d106e401e7874f680d8eb277a4707b Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Fri, 13 Apr 2018 20:22:15 +0200 Subject: [PATCH 3/3] Jump methods --- OGP1718-Worms/src/worms/model/Worm.java | 77 ++++++++++++++++++------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 10e6756..de23d5e 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -665,9 +665,26 @@ public class Worm extends GameObject { if (!canJump()) throw new IllegalStateException(); - - setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY())); - setActionPoints(0); + + Coordinate newLocation = Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY()); + + isNewLocationInWorld(newLocation); + + if (! getWorld().isPassable(newLocation) && validJumpDistance(jumpDistance(jumpVelocity())) && ! isTerminated()) { + setLocation(newLocation); + setActionPoints(0); + } + } + + /** + * + * @param location + */ + public void isNewLocationInWorld (Coordinate location) { + if (location.getX() - radius < 0 || location.getX() + radius > getWorld().getWidth() || + location.getY() - radius < 0 || location.getY() + radius > getWorld().getHeight()) { + terminate(); + } } /** @@ -733,6 +750,18 @@ public class Worm extends GameObject { private double jumpDistance(double v) { return (pow(v, 2) * sin(2 * getOrientation())) / G; } + + /** + * + * @param distance + * @return + */ + public boolean validJumpDistance(double distance) { + if (distance >= getRadius()) { + return true; + } + return false; + } /** * Return the velocity of the jump @@ -749,25 +778,33 @@ public class Worm extends GameObject { return force / getMass() * FORCE_TIME; } - public void fight(Worm worm1, Worm... worm2) { - for (Worm wormB: worm2) { - Worm attackedWorm; - Worm attacker; - Random rand = new Random(); - int coin = rand.nextInt(2); - if (coin == 1) { - attackedWorm = worm1; - attacker = wormB; + /** + * + * @param newLocation + * @param oldLocation + * @param worm1 + * @param worm2 + */ + 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); } - else { - attackedWorm = wormB; - attacker = worm1; - } - double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius())); - long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; - attackedWorm.incrementHitPoints(loseAttackedWorm); } - }