diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index 7497fd7..32687ef 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -413,7 +413,7 @@ public class Facade implements IFacade { public Projectile fire(Worm worm) throws ModelException { try { return worm.fire(); - } catch (IllegalStateException e) { + } catch (IllegalStateException | IllegalArgumentException e) { throw new ModelException(e); } } diff --git a/OGP1718-Worms/src/worms/model/Food.java b/OGP1718-Worms/src/worms/model/Food.java index 52f8774..e8d4ff8 100644 --- a/OGP1718-Worms/src/worms/model/Food.java +++ b/OGP1718-Worms/src/worms/model/Food.java @@ -46,9 +46,4 @@ public class Food extends GameObject { if (getWorld() != null) return super.isValidLocation(location) && getWorld().isAdjacent(location, getRadius()); return super.isValidLocation(location); } - - protected boolean isValidLocation(Coordinate location, World world) { - if (world != null) return super.isValidLocation(location) && world.isAdjacent(location, getRadius()); - return super.isValidLocation(location); - } } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 63e005b..cf3d170 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -42,11 +42,7 @@ public abstract class GameObject { * |setRadius(radius) */ protected GameObject(World world, double[] location, double radius) { - if (!isValidLocation(Coordinate.create(location))) throw new IllegalArgumentException("Illegal location"); - setLocation(location); - setRadius(radius); - if (isValidWorld(world)) world.add(this); - setWorld(world); + this(world, Coordinate.create(location), radius); } @@ -64,10 +60,11 @@ public abstract class GameObject { * |setRadius(radius) */ protected GameObject(World world, Coordinate location, double radius) { - world.add(this); - setWorld(world); + if (!isValidLocation(location)) throw new IllegalArgumentException("Illegal location"); setLocation(location); setRadius(radius); + if (isValidWorld(world)) world.add(this); + setWorld(world); } /** diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index f841053..574a857 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -62,8 +62,7 @@ public abstract class Projectile extends GameObject { !(location.getX() - radius < 0) && !(location.getX() + radius > getWorld().getWidth()) && !(location.getY() + radius > getWorld().getHeight()) && - !(location.getY() - radius < 0 && - !getWorld().isAdjacent(location, radius)); + !(location.getY() - radius < 0); } public static final double G = 5.0; diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index f80ef4e..4f1f8ab 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -447,11 +447,11 @@ public class World { */ public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException { if (hasActiveGame()) throw new IllegalStateException(); - if (obj == null || !getGameObjects().add(obj) || obj.isTerminated() || obj.getWorld() != null) throw new IllegalArgumentException(); + if (obj == null || getGameObjects().contains(obj) || obj.isTerminated() || obj.getWorld() != null) throw new IllegalArgumentException(); obj.setWorld(this); - if (obj.getClass().equals(Food.class) && !((Food) obj).isValidLocation(obj.getLocation(), this)) { + if (!obj.isValidLocation(obj.getLocation())) { obj.setWorld(null); throw new IllegalArgumentException(); } @@ -460,6 +460,7 @@ public class World { throw new IllegalArgumentException(); } obj.setWorld(this); + getGameObjects().add(obj); } /** diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index e6c82b8..f44dbf8 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -522,8 +522,8 @@ public class Worm extends GameObject { if (step > radius) step = radius; Coordinate nextLoc = currentLocation; - while (getDistance(currentLocation, Coordinate.create(nextLoc.getX() + step * cos(direction), - nextLoc.getY() + step * sin(direction))) <= maxDistance) { + while (Math.round(getDistance(currentLocation, Coordinate.create(nextLoc.getX() + step * cos(direction), + nextLoc.getY() + step * sin(direction))) * 100.0) / 100.0 <= maxDistance) { nextLoc = Coordinate.create(nextLoc.getX() + step * cos(direction), nextLoc.getY() + step * sin(direction)); @@ -531,8 +531,8 @@ public class Worm extends GameObject { double max = radius; while (!world.isAdjacent(nextLoc.toArray(), radius)) { max -= 0.01; - nextLoc = Coordinate.create(nextLoc.getX() - 0.01 * cos(direction), - nextLoc.getY() - 0.01 * sin(direction)); + nextLoc = Coordinate.create(nextLoc.getX() - step / 100.0 * cos(direction), + nextLoc.getY() - step / 100.0 * sin(direction)); if (max < 0) { return currentLocation; } @@ -720,8 +720,13 @@ public class Worm extends GameObject { terminate(); return; } - setLocation(newLocation); + if (!newLocation.equals(getLocation())) { + setLocation(newLocation); + fight(); + } setActionPoints(0); + + } /** @@ -798,14 +803,12 @@ public class Worm extends GameObject { if (!world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) { while (true) { - t -= 0.01; newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0); if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() || newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) { if (getDistance(getLocation(), newLoc) < getRadius()) throw new IllegalStateException(); - break; } } @@ -903,25 +906,49 @@ public class Worm extends GameObject { * 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) { + public void fight() { + + World world = getWorld(); + + for (Worm w : world.getGameObjectsByClass(Worm.class)) { + if (w.equals(this)) continue; + if (w.getDistance(this) < 0) { Worm attackedWorm; Worm attacker; - Random rand = new Random(); - int coin = rand.nextInt(2); + int coin = ThreadLocalRandom.current().nextInt(2); if (coin == 1) { - attackedWorm = worm1; - attacker = wormB; + attackedWorm = w; + attacker = this; } else { - attackedWorm = wormB; - attacker = worm1; + attackedWorm = this; + attacker = w; } - double N = 10 * ceil((attacker.getRadius()) / (attackedWorm.getRadius())); - long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; + long N = (long) ceil(10.0 * (attacker.getRadius() / attackedWorm.getRadius())); + long loseAttackedWorm = ThreadLocalRandom.current().nextLong(1, 10 * N); + System.out.println(loseAttackedWorm); + System.out.println(attackedWorm.getName()); 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); +// } +// } }