From 48c05010f67b270873978cc38c9244589e701762 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Fri, 13 Apr 2018 18:29:39 +0200 Subject: [PATCH] fixes --- OGP1718-Worms/src/worms/facade/Facade.java | 6 ++- OGP1718-Worms/src/worms/model/Food.java | 1 + OGP1718-Worms/src/worms/model/World.java | 58 +++------------------- OGP1718-Worms/src/worms/model/Worm.java | 1 + 4 files changed, 15 insertions(+), 51 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index f438c49..bf76a2a 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -147,7 +147,11 @@ public class Facade implements IFacade { */ @Override public double getJumpTime(Worm worm, double deltaT) throws ModelException { - return worm.jumpTime(); + try { + return worm.jumpTime(); + } catch(IllegalStateException e){ + throw new ModelException(e); + } } // /** diff --git a/OGP1718-Worms/src/worms/model/Food.java b/OGP1718-Worms/src/worms/model/Food.java index 6d588cc..d1b5981 100644 --- a/OGP1718-Worms/src/worms/model/Food.java +++ b/OGP1718-Worms/src/worms/model/Food.java @@ -9,5 +9,6 @@ public class Food extends GameObject { setRadius(0.2); final double rho = 150; setMass(getRadius(), rho); + world.addFood(this); } } diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index c2d611a..5294e62 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -142,14 +142,14 @@ public class World { public boolean isPassable(double[] center, double radius) { for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) { - double lenX = Math.round((center[0] + radius * Math.cos(i)) * 10000) / 10000; - double lenY = Math.round((center[1] + radius * Math.sin(i)) * 10000) / 10000; + double lenX = center[0] + radius * Math.cos(i); + double lenY = center[1] + radius * Math.sin(i); if (i < 1.58 && i > 1.57) { - lenY -= 0.00001; + lenY -= 0.0000000001; } else if (i < 0.79 && i > 0.78 ) { - lenX -= 0.00001; + lenX -= 0.0000000001; } if (!isPassable(lenX, lenY)) { @@ -158,46 +158,6 @@ public class World { } return true; } - - private boolean isUnderCircle(Coordinate square, Coordinate center, double radius) { - - double angle = Math.atan2(center.getY() - square.getY(), square.getX() - center.getX()); - if (angle < Math.PI && angle > Math.PI / 2) { - square = Coordinate.create(square.getX() + 1, square.getY() + 1); - } - - - - else if (angle < Math.PI / 2 && angle > 0) angle = Math.PI / 4; - else if (angle < 0 && angle > -Math.PI / 2) angle = -Math.PI / 4; - else if (angle < 0 && angle > -Math.PI) angle = -3* Math.PI / 4; - - if (square.getX() == center.getX() && square.getY() == center.getY()) return true; - - double x = Math.abs(radius * Math.cos(angle) + center.getX()); - double y = Math.abs(radius * Math.sin(angle) + center.getY()); - - return Math.floor(square.getX() / getLengthX()) - Math.floor(x / getLengthX()) == 0 && - Math.floor(square.getY() / getLengthY()) - Math.floor(y / getLengthY()) == 0; - } - - public ArrayList getCircleList(Coordinate center, double radius) { - - double[] pos = {Math.floor(center.getX() - radius), Math.floor(center.getY() - radius)}; - ArrayList circle = new ArrayList(); - int partsX = (int) Math.ceil(center.getX() + radius / getLengthX()) - (int) Math.floor((center.getX() - radius) / getLengthX()); - int partsY = (int) Math.ceil(center.getY() + radius / getLengthY()) - (int) Math.floor((center.getY() - radius) / getLengthY()); - - for (int x = 0; x < partsX; x++) { - for (int y = 0; y < partsY; y++) { - Coordinate temp = Coordinate.create(pos[0] + x * getLengthX(), pos[1] + y * getLengthY()); - if (isUnderCircle(temp, center, radius)) { - circle.add(temp); - } - } - } - return circle; - } /** * * @param center ... @@ -236,10 +196,10 @@ public class World { int yCoord = map.length - 1 - (int) Math.floor(y / lengthY); if (yCoord < 0 || yCoord >= map.length) { - return false; + return true; } if (xCoord < 0 || xCoord >= map[0].length) { - return false; + return true; } return getMap()[yCoord][xCoord]; @@ -252,8 +212,6 @@ public class World { // region objects //=================================================================================== - - public Collection getAllItems() { Set all = new HashSet(); all.add(getWormList()); @@ -278,9 +236,9 @@ public class World { public void addWorm(Worm worm) throws IllegalArgumentException, NullPointerException, IllegalStateException { - if (hasActiveGame()) throw new IllegalStateException(); - if (hasAsWorm(worm)) throw new IllegalArgumentException(); + if (hasActiveGame() || hasAsWorm(worm)) throw new IllegalStateException(); getWormList().add(worm); + worm.setWorld(this); } public void removeWorm(Worm worm) throws IllegalArgumentException, NullPointerException { diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index ec3ec2c..704ab6d 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -134,6 +134,7 @@ public class Worm extends GameObject { this(Coordinate.create(location), direction, name, radius, 0.25); setWorld(world); setTeam(team); + world.addWorm(this); } //===================================================================================