From d3a947549460000f0ca2f7a50c783fa719cec8bc Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Fri, 18 May 2018 16:38:16 +0200 Subject: [PATCH] small update --- OGP1718-Worms/src/worms/facade/Facade.java | 10 +- OGP1718-Worms/src/worms/model/Projectile.java | 112 +++++++++--------- OGP1718-Worms/src/worms/model/Team.java | 3 +- OGP1718-Worms/src/worms/model/World.java | 19 +-- OGP1718-Worms/src/worms/model/Worm.java | 9 +- 5 files changed, 84 insertions(+), 69 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index b599179..da8ea45 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -176,7 +176,7 @@ public class Facade implements IFacade { Coordinate jumpStep = worm.jumpStep(t); return jumpStep.toArray(); } - catch (IllegalArgumentException e) { + catch (IllegalArgumentException | IllegalStateException e) { throw new ModelException(e); } } @@ -927,7 +927,7 @@ public class Facade implements IFacade { public Team createTeam(World world, String name) throws ModelException, MustNotImplementException { try { return new Team(name, world); - } catch(IllegalNameException e) { + } catch(IllegalNameException | IllegalStateException e) { throw new ModelException(e.getMessage()); } } @@ -1011,7 +1011,11 @@ public class Facade implements IFacade { */ @Override public void mergeTeams(Team receivingTeam, Team supplyingTeam) throws ModelException, MustNotImplementException { - Team.mergeTeams(receivingTeam, supplyingTeam); + try { + Team.mergeTeams(receivingTeam, supplyingTeam); + } catch (IllegalArgumentException e) { + throw new ModelException(e); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index 13011d2..bb68957 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -81,47 +81,45 @@ 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; - return 0.0; + // wat met de action points? + double force = 5 * Worm.getActionPoints() + getMass() * G; + return force/getMass() * FORCE_TIME; } 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; - return 0.0; + // 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; } private boolean canJump() { @@ -131,25 +129,25 @@ public abstract class Projectile extends GameObject { } 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) { diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 11012d2..8f68550 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -82,6 +82,7 @@ public class Team { Collection worms = getAllWormsOfTeam(); for (Worm w : worm) { + if (w == null) return false; if (!names.add(w.getName())) return false; if (worms.contains(w)) return false; if (worms.size() == 0 && !w.isTerminated()) continue; @@ -173,8 +174,8 @@ public class Team { } Worm[] supWorms = supplyingTeam.getAllWormsOfTeam().toArray(new Worm[0]); + supplyingTeam.removeWormsFromTeam(supWorms); receivingTeam.addWorm(supWorms); - supplyingTeam.removeWormsFromTeam(supWorms); } /** diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 08ae145..c39e3fa 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -192,6 +192,8 @@ public class World { Set gameObjectList = new HashSet<>(getGameObjects()); gameObjectList.forEach(g -> g.terminate()); + this.teams.clear(); + this.terminated = true; } @@ -558,7 +560,7 @@ public class World { if (item1 instanceof Worm && item2 instanceof Worm) { - if (((Worm) item1).getTeam().getName().equals(((Worm) item2).getTeam().getName())) { + if ((((Worm) item1).getTeam() != null && ((Worm) item2).getTeam() != null) && ((Worm) item1).getTeam().getName().equals(((Worm) item2).getTeam().getName())) { long hitpoints = (long) Math.floor((((Worm) item1).getHitPoints() + ((Worm) item2).getHitPoints()) / 2); ((Worm) item1).setHitPoints(hitpoints); ((Worm) item2).setHitPoints(hitpoints); @@ -567,24 +569,27 @@ public class World { Worm lgWorm = (Worm) item1; Worm smWorm = (Worm) item2; - if (item1.getRadius() < item2.getRadius()) { + if (lgWorm.getRadius() < smWorm.getRadius()) { lgWorm = (Worm) item2; smWorm = (Worm) item1; } - long lgHitPoints = lgWorm.getHitPoints(); + long lgHitPoints = lgWorm.getActionPoints(); if (lgHitPoints < 5) { - lgWorm.setHitPoints(0); - smWorm.incrementHitPoints(lgHitPoints); + lgWorm.setActionPoints(0); + smWorm.incrementActionPoints(smWorm.getActionPoints() + lgHitPoints); } else { - smWorm.incrementHitPoints(5); - lgWorm.decreaseHitPoints(5); + smWorm.incrementActionPoints(5); + lgWorm.decreaseActionPoints(5); } } } else if (item1 instanceof Worm && item2 instanceof Food) { ((Worm) item1).eat((Food) item2, false); } + else if(item1 instanceof Food && item2 instanceof Worm) { + ((Worm) item2).eat((Food) item1, false); + } else if (item1 instanceof Worm && item2 instanceof Projectile || item1 instanceof Projectile && item2 instanceof Worm) { // TODO hit by projectile, projectile gets random hit points diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 26b3c24..dce98ad 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -323,6 +323,10 @@ public class Worm extends GameObject { setActionPoints(getActionPoints() - delta); } + public void incrementActionPoints(long delta) { + setActionPoints(getActionPoints() + delta); + } + /** * This variable contains the current action points of the worm. */ @@ -966,7 +970,10 @@ public class Worm extends GameObject { * |new.getHitPoints() == old.getHitPoints() + value */ public void incrementHitPoints(long value) { - setHitPoints(getHitPoints() + value); + + double current = getHitPoints(); + if (current + value < 0) setHitPoints(0); + else setHitPoints(getHitPoints() + value); }