From 2cd0d3af7a612a5f05e33865e972ce55365b7fa8 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 16 May 2018 16:25:06 +0200 Subject: [PATCH] fixes --- OGP1718-Worms/src/worms/facade/Facade.java | 34 ++++- OGP1718-Worms/src/worms/model/Bazooka.java | 7 +- OGP1718-Worms/src/worms/model/GameObject.java | 7 +- OGP1718-Worms/src/worms/model/Projectile.java | 127 ++++++++++-------- OGP1718-Worms/src/worms/model/Rifle.java | 11 +- OGP1718-Worms/src/worms/model/Team.java | 9 +- OGP1718-Worms/src/worms/model/World.java | 50 +++++-- OGP1718-Worms/src/worms/model/Worm.java | 14 +- 8 files changed, 170 insertions(+), 89 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index 017d456..c9ffb6e 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -402,7 +402,11 @@ public class Facade implements IFacade { */ @Override public Projectile fire(Worm worm) throws ModelException { - return null; + try { + return worm.fire(); + } catch (IllegalStateException e) { + throw new ModelException(e); + } } /** @@ -427,7 +431,11 @@ public class Facade implements IFacade { */ @Override public World createWorld(double width, double height, boolean[][] passableMap) throws ModelException { - return new World(width, height, passableMap); + try { + return new World(width, height, passableMap); + } catch (IllegalArgumentException e) { + throw new ModelException(e); + } } /** @@ -585,7 +593,11 @@ public class Facade implements IFacade { */ @Override public void removeFood(World world, Food food) throws ModelException { - world.remove(food); + try { + world.remove(food); + } catch (IllegalArgumentException e) { + throw new ModelException(e); + } } /** @@ -695,7 +707,11 @@ public class Facade implements IFacade { */ @Override public Worm createWorm(World world, double[] location, double direction, double radius, String name, Team team) throws ModelException { - return new Worm(world, location, direction, radius, name, team); + try { + return new Worm(world, location, direction, radius, name, team); + } catch(IllegalArgumentException e) { + throw new ModelException(e); + } } /** @@ -897,7 +913,7 @@ public class Facade implements IFacade { @Override public Team createTeam(World world, String name) throws ModelException, MustNotImplementException { try { - return new Team(name); + return new Team(name, world); } catch(IllegalNameException e) { throw new ModelException(e.getMessage()); } @@ -949,7 +965,7 @@ public class Facade implements IFacade { */ @Override public List getAllWormsOfTeam(Team team) throws ModelException, MustNotImplementException { - return (List) team.getAllWormsOfTeam(); + return team.getAllWormsList(); } /** @@ -957,7 +973,11 @@ public class Facade implements IFacade { */ @Override public void addWormsToTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException { - team.addWorm(worms); + try { + team.addWorm(worms); + } catch(IllegalArgumentException e) { + throw new ModelException(e); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Bazooka.java b/OGP1718-Worms/src/worms/model/Bazooka.java index bbede59..2186ab1 100644 --- a/OGP1718-Worms/src/worms/model/Bazooka.java +++ b/OGP1718-Worms/src/worms/model/Bazooka.java @@ -4,9 +4,12 @@ import worms.util.Coordinate; import java.util.concurrent.ThreadLocalRandom; +import static java.lang.Math.cos; +import static java.lang.Math.sin; + public class Bazooka extends Projectile { - protected Bazooka(World world, Coordinate location, double force, double orientation) { - super(world, location, 300, force, orientation); + protected Bazooka(World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double force) { + super(world, wormLocation, wormOrientation, wormRadius, 300, force); } @Override diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index f641e3d..267da9e 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -43,7 +43,7 @@ public abstract class GameObject { */ protected GameObject(World world, double[] location, double radius) { setWorld(world); - world.add(this); + if (isValidWorld(world)) world.add(this); setLocation(location); setRadius(radius); } @@ -73,7 +73,7 @@ public abstract class GameObject { * @return ... |result == !world.hasActiveGame() && !world.isTerminated() */ public static boolean isValidWorld(World world) { - return !world.hasActiveGame() && !world.isTerminated(); + return world != null && !world.hasActiveGame() && !world.isTerminated(); } /** @@ -241,7 +241,8 @@ public abstract class GameObject { * |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3)) */ protected void setMass(double radius, double rho) { - this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); + + this.mass = rho * (4.0 / 3.0 * PI * pow(radius, 3.0)); } diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index d215699..13011d2 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -8,12 +8,11 @@ public abstract class Projectile extends GameObject { private static final int rho = 7800; - - protected Projectile (World world, Coordinate location, double mass, double force, double orientation) { - super(world, location, calcRadius(mass)); + protected Projectile (World world, Coordinate wormLocation, double wormOrientation, double wormRadius, double mass, double force) { + super(world, calcLocation(wormLocation, wormOrientation, wormRadius, mass), calcRadius(mass)); super.mass = mass; this.force = force; - this.orientation = orientation; + this.orientation = wormOrientation; this.hitPoints = getRandomHitPoints(); } @@ -83,70 +82,80 @@ public abstract class Projectile extends GameObject { private double jumpVelocity() { //wat met de action points? - double force = 5 * Worm.getActionPoints() + getMass() * G; - return force/getMass() * FORCE_TIME; +// double force = 5 * Worm.getActionPoints() + getMass() * G; +// return force/getMass() * FORCE_TIME; + return 0.0; } private double getJumpTime(double jumpTimeStep) { - // TODO zie naar worm hoe dit moet, implementatie moet wel anders! - //wat met parameter jumpTimeStep? - - double v = jumpVelocity(); - double time = 0; - double a = getOrientation(); - Coordinate loc = getLocation(); - World world = getWorld(); - double radius = getRadius(); - Coordinate newLoc; - - while (true) { - time += 0.05; - 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 -= 0.01; - 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)) { - break; - } - } - break; - } - if ((int) round(time) == 10) { - throw new RuntimeException(); - } - } - - return time; +// // TODO zie naar worm hoe dit moet, implementatie moet wel anders! +// //wat met parameter jumpTimeStep? +// +// double v = jumpVelocity(); +// double time = 0; +// double a = getOrientation(); +// Coordinate loc = getLocation(); +// World world = getWorld(); +// double radius = getRadius(); +// Coordinate newLoc; +// +// while (true) { +// time += 0.05; +// 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 -= 0.01; +// 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)) { +// break; +// } +// } +// break; +// } +// if ((int) round(time) == 10) { +// throw new RuntimeException(); +// } +// } +// +// return time; + return 0.0; } private boolean canJump() { // wat met action points. - return Worm.getActionPoints() > 0 && getOrientation() < PI; + //return this.getActionPoints() > 0 && getOrientation() < PI; + return false; } public void jump() throws IllegalStateException { - // TODO zie naar worm hoe dit moet, implementatie moet wel anders! - // wat meegeven met getJumpTime() ? - // wat met action points? - - if (!canJump()) - throw new IllegalStateException(); - - double v = jumpVelocity(); - double t = getJumpTime(); - double a = getOrientation(); - - Coordinate 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); - Worm.setActionPoints(0); +// // TODO zie naar worm hoe dit moet, implementatie moet wel anders! +// // wat meegeven met getJumpTime() ? +// // wat met action points? +// +// if (!canJump()) +// throw new IllegalStateException(); +// +// double v = jumpVelocity(); +// double t = getJumpTime(); +// double a = getOrientation(); +// +// Coordinate 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); +// //Worm.setActionPoints(0); } + + private 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)); + } } \ 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 f1fc763..1cd85c9 100644 --- a/OGP1718-Worms/src/worms/model/Rifle.java +++ b/OGP1718-Worms/src/worms/model/Rifle.java @@ -4,11 +4,14 @@ import worms.util.Coordinate; import java.util.concurrent.ThreadLocalRandom; +import static java.lang.Math.cos; +import static java.lang.Math.sin; + public class Rifle extends Projectile { - public Rifle(World world, Coordinate location, double orientation) { - super(world, location, 10, 1.5, orientation); + public Rifle(World world, Coordinate wormLocation, double wormOrientation, double wormRadius) { + super(world, wormLocation, wormOrientation, wormRadius, 10, 1.5); } @Override @@ -23,4 +26,8 @@ public class Rifle extends Projectile { super.hitPoints = value; } + public static double calcRadius() { + return calcRadius(10); + } + } diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 9138f85..ec4e2ed 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -20,9 +20,12 @@ public class Team { * @post ... * |wormCollection = new Treeset<>(new TeamComparator()) */ - public Team (String name) { + public Team (String name, World world) { setName(name); this.wormCollection = new TreeSet<>(new TeamComparator()); + if (world != null) { + world.addTeam(this); + } } //=================================================================================== @@ -140,6 +143,10 @@ public class Team { return wormCollection; } + public List getAllWormsList() { + return new ArrayList<>(this.wormCollection); + } + /** * * @return ... diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 7b23c36..89b71a5 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -30,7 +30,7 @@ public class World { */ public World(double width, double height, boolean[][] map) { - if (!isValidDimension(width) || !isValidDimension(height)) throw new IllegalArgumentException(); + if (!isValidDimension(width) || !isValidDimension(height) || map == null || map.length != height || map[0].length != height) throw new IllegalArgumentException(); this.width = width; this.height = height; @@ -51,8 +51,15 @@ public class World { } public void startGame() { - this.game = true; - this.activeWorm = 0; + + try { + this.game = true; + this.activeWorm = -1; + activateNextWorm(); + } catch (IllegalStateException e) { + this.game = false; + } + } public void finishGame() { @@ -65,11 +72,14 @@ public class World { throw new IllegalStateException("No worms"); } - getWormList().get(this.activeWorm).setActionPoints((long) getWormList().get(this.activeWorm).getMass()); this.activeWorm++; - if (this.activeWorm == getWormList().size()) { + if (this.activeWorm >= getWormList().size()) { this.activeWorm = 0; } + + Worm active = getWormList().get(this.activeWorm); + active.setActionPoints(active.getMaxActionPoints()); + active.incrementHitPoints(10); } public Worm getActiveWorm() { @@ -79,6 +89,8 @@ public class World { public String getWinner() { + if (!hasActiveGame()) return null; + if (getWormList().size() == 1) { return getWormList().get(0).getName(); } else if (getWormList().size() > 1) { @@ -176,6 +188,10 @@ public class World { * |this.terminated = true */ public void terminate() { + + Set gameObjectList = new HashSet<>(getGameObjects()); + gameObjectList.forEach(g -> g.terminate()); + this.terminated = true; } @@ -212,7 +228,8 @@ public class World { location[1] / lengthY >= getMap().length || location[1] < 0.0) { return false; } - return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]; + + return this.map[map.length - 1 - (int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]; } /** @@ -375,6 +392,16 @@ public class World { return new ArrayList<>(getGameObjects()); } + + private Set teams = new HashSet<>(); + + public void addTeam(Team team) { + + if (teams.size() == 10) throw new IllegalStateException("Maximum 10 teams"); + + if (!teams.add(team)) throw new IllegalArgumentException("Team already exists"); + } + /** * * @return ... @@ -382,11 +409,7 @@ public class World { */ public Set getAllTeams() { - Set teams = new HashSet<>(); - for(Worm worm: getWormList()) { - teams.add(worm.getTeam()); - } - return teams; + return new HashSet<>(teams); } /** @@ -428,6 +451,11 @@ public class World { public void remove(GameObject obj) throws IllegalArgumentException { if (!getGameObjects().remove(obj)) throw new IllegalArgumentException(); + obj.setWorld(null); + + if (obj.getClass().equals(Worm.class)) { + //System.out.println("remove " + ((Worm) obj).getName()); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index a453ef6..4fdbb45 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -127,6 +127,10 @@ public class Worm extends GameObject { long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000); setHitPoints(startHitPoints); + + if (team != null) { + team.addWorm(this); + } } //=================================================================================== @@ -776,6 +780,8 @@ public class Worm extends GameObject { 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; } } @@ -1185,19 +1191,19 @@ public class Worm extends GameObject { if (getActionPoints() >= 30 && getWorld() != null) { int random = ThreadLocalRandom.current().nextInt(2); - Coordinate projLocation = Coordinate.create(cos(getOrientation()) * getRadius(), sin(getOrientation()) * getRadius()); if (random == 0) { decreaseActionPoints(10); - return new Rifle(getWorld(), projLocation,getOrientation()); + return new Rifle(getWorld(), getLocation(), getOrientation(), getRadius()); } else { double force = Bazooka.calcForce(getActionPoints()); decreaseActionPoints(25); - return new Bazooka(getWorld(), projLocation, force, getOrientation()); + + return new Bazooka(getWorld(), getLocation(), getOrientation(), getRadius(), force); } } - return null; + throw new IllegalStateException(); } // public void hitByRifle() {