From 98de279626d1262ec5774984657a78c93270cb02 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 23 May 2018 21:38:44 +0200 Subject: [PATCH] refactoring jeej --- OGP1718-Worms/src/worms/model/GameObject.java | 6 +- OGP1718-Worms/src/worms/model/Projectile.java | 9 +- OGP1718-Worms/src/worms/model/Rifle.java | 1 - OGP1718-Worms/src/worms/model/Team.java | 31 ++-- OGP1718-Worms/src/worms/model/World.java | 144 ++++++++++-------- OGP1718-Worms/src/worms/model/Worm.java | 10 +- 6 files changed, 112 insertions(+), 89 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 7995e46..1a3c4b4 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -6,7 +6,7 @@ import worms.util.Coordinate; import static java.lang.Math.*; public abstract class GameObject { - + // region Constructor //=================================================================================== @@ -46,7 +46,7 @@ public abstract class GameObject { setLocation(location); setRadius(radius); if (isValidWorld(world)) world.add(this); - this.world = world; + setWorld(world); } // =================================================================================== @@ -272,7 +272,7 @@ public abstract class GameObject { public double getDistance(GameObject o) { - return getDistance(o.getLocation(), this.radius); + return getDistance(o.getLocation(), o.getRadius()); } public double getDistance(Coordinate otherLocation, double radius) { diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index 6a12637..84e5360 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -6,13 +6,12 @@ import java.util.List; public abstract class Projectile extends GameObject implements IJumpable { - private static final int rho = 7800; - // region constructor //=================================================================================== public static final double G = 5.0; public static final double FORCE_TIME = 0.5; + private static final int rho = 7800; protected Projectile (Worm worm, double mass, double force) { super(worm.getWorld(), calcLocation(worm.getLocation(), worm.getOrientation(), worm.getRadius(), mass), calcRadius(mass)); @@ -33,17 +32,21 @@ public abstract class Projectile extends GameObject implements IJumpable { return orientation; } + private final double orientation; + + public static double calcRadius(double mass) { return pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * PI))), 1.0 / 3.0); } + public double getForce() { return this.force; } protected final double force; - private final double orientation; + // =================================================================================== // endregion diff --git a/OGP1718-Worms/src/worms/model/Rifle.java b/OGP1718-Worms/src/worms/model/Rifle.java index c144564..c20cdef 100644 --- a/OGP1718-Worms/src/worms/model/Rifle.java +++ b/OGP1718-Worms/src/worms/model/Rifle.java @@ -9,7 +9,6 @@ import static java.lang.Math.sin; public class Rifle extends Projectile { - public Rifle(Worm worm) { super(worm, 10, 1.5); } diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index c718b18..8ad7e56 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -32,9 +32,7 @@ public class Team { world.addTeam(this); } } - - //=================================================================================== - // endregion + /** * Check whether the given object has a valid name for a object. @@ -49,10 +47,14 @@ public class Team { */ @Override public boolean equals(Object obj) { - if (!(obj instanceof Team)) return false; + if (!(obj instanceof Team)) return false; - return this.name.equals(((Team) obj).getName()); + return this.name.equals(((Team) obj).getName()); } + + //=================================================================================== + // endregion + // region changesTeam @@ -165,16 +167,6 @@ public class Team { return this.wormCollection.contains(worm); } - /** - * Gives all the worms of the team. - * - * @return All the worms of the team in a Collection. - * |result == wormCollection - */ - public Collection getAllWormsOfTeam() { - return wormCollection; - } - /** * Gives all the worms of the team. * @@ -230,6 +222,8 @@ public class Team { //=================================================================================== // endregion + + // region mass //=================================================================================== @@ -253,7 +247,9 @@ public class Team { //=================================================================================== // endregion - + + + // region name //=================================================================================== @@ -293,8 +289,9 @@ public class Team { //=================================================================================== // endregion + - // region terminate + // region destructor //=================================================================================== /** diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 42c4e79..1056b52 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -7,6 +7,9 @@ import java.util.stream.Collectors; public class World { + // region constructor / destructor + //=================================================================================== + /** * @param width * @param height @@ -42,6 +45,37 @@ public class World { } + /** + * @return ... + * result = this.terminated + */ + public boolean isTerminated() { + return this.terminated; + } + + /** + * @post ... + * |teams.clear() + * @post ... + * |this.terminated = true + */ + public void terminate() { + + Set gameObjectList = new HashSet<>(getGameObjects()); + gameObjectList.forEach(GameObject::terminate); + + this.teams.clear(); + + this.terminated = true; + } + + private boolean terminated = false; + + + // =================================================================================== + // endregion + + // region game (Total) //=================================================================================== @@ -152,6 +186,27 @@ public class World { private final double height; + + /** + * @return ... + * |result == this.lengtX + */ + public double getLengthX() { + return this.lengthX; + } + + /** + * @return ... + * |result == this.lengthY + */ + public double getLengthY() { + return this.lengthY; + } + + private final double lengthX; + + private final double lengthY; + // =================================================================================== // endregion @@ -168,37 +223,16 @@ public class World { return map; } - /** - * @return ... - * result = this.terminated - */ - public boolean isTerminated() { - return this.terminated; - } - - /** - * @post ... - * |teams.clear() - * @post ... - * |this.terminated = true - */ - public void terminate() { - - Set gameObjectList = new HashSet<>(getGameObjects()); - gameObjectList.forEach(g -> g.terminate()); - - this.teams.clear(); - - this.terminated = true; - } private final boolean[][] map; - private boolean terminated = false; + // =================================================================================== // endregion + + // region passable (Total) //=================================================================================== @@ -266,12 +300,6 @@ public class World { 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.0000000001; -// } -// else if (i < 0.79 && i > 0.78 ) { -// lenX -= 0.0000000001; -// } if (center[0] + radius == lenX) { lenX -= 0.0000000001; } @@ -304,31 +332,11 @@ public class World { public boolean isAdjacent(Coordinate center, double radius) { double maxDistance = radius * 0.1; - double length = lengthX; - if (lengthX > lengthY) length = lengthY; return isPassable(center, radius) && !isPassable(center, radius + maxDistance + 0.00001); } - /** - * @return ... - * |result == this.lengtX - */ - public double getLengthX() { - return this.lengthX; - } - - /** - * @return ... - * |result == this.lengthY - */ - public double getLengthY() { - return this.lengthY; - } - private final double lengthX; - - private final double lengthY; /** * @param x @@ -447,7 +455,6 @@ public class World { obj.setWorld(null); throw new IllegalArgumentException(); } - obj.setWorld(this); getGameObjects().add(obj); } @@ -522,7 +529,7 @@ public class World { public void castSpell() { - Set gameObjects = getGameObjects(); + Set gameObjects = this.gameObjects; int size = gameObjects.size(); if (size < 2) throw new IllegalStateException("Less than 2 objects"); int nb1 = new Random().nextInt(size); @@ -547,18 +554,23 @@ public class World { if (item1 instanceof Worm && item2 instanceof Worm) { - 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); + + Worm worm1 = (Worm) item1; + Worm worm2 = (Worm) item2; + + + if ((worm1.getTeam() != null && worm2.getTeam() != null) && worm1.getTeam().getName().equals(worm2.getTeam().getName())) { + long hitpoints = (long) Math.floor((worm1.getHitPoints() + worm2.getHitPoints()) / 2); + worm1.setHitPoints(hitpoints); + worm2.setHitPoints(hitpoints); } else { - Worm lgWorm = (Worm) item1; - Worm smWorm = (Worm) item2; + Worm lgWorm = worm1; + Worm smWorm = worm2; if (lgWorm.getRadius() < smWorm.getRadius()) { - lgWorm = (Worm) item2; - smWorm = (Worm) item1; + lgWorm = worm2; + smWorm = worm1; } long lgHitPoints = lgWorm.getActionPoints(); if (lgHitPoints < 5) { @@ -594,11 +606,15 @@ public class World { } else if (item1 instanceof Food && item2 instanceof Food) { - if (((Food) item1).isPoisonous()) ((Food) item1).heal(); - else ((Food) item1).poison(); - if (((Food) item2).isPoisonous()) ((Food) item2).heal(); - else ((Food) item2).poison(); + Food food1 = (Food) item1; + Food food2 = (Food) item2; + + if (food1.isPoisonous()) food1.heal(); + else food1.poison(); + + if (food2.isPoisonous()) food2.heal(); + else food2.poison(); } else if (item1 instanceof Food && item2 instanceof Projectile || item1 instanceof Projectile && item2 instanceof Food) { diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index c1b7a53..bf94317 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -1004,6 +1004,10 @@ public class Worm extends GameObject implements IJumpable{ // =================================================================================== // endregion + + // region eat + //=================================================================================== + public void eat(Food food) { this.eat(food, true); } @@ -1094,6 +1098,10 @@ public class Worm extends GameObject implements IJumpable{ } + // =================================================================================== + // endregion + + // region firing and projectiles //=================================================================================== @@ -1116,7 +1124,6 @@ public class Worm extends GameObject implements IJumpable{ decrementActionPoints(10); return new Rifle(this); } else { - double force = Bazooka.calcForce(this.actionPoints); decrementActionPoints(25); return new Bazooka(this); } @@ -1132,6 +1139,7 @@ public class Worm extends GameObject implements IJumpable{ // =================================================================================== // endregion + // region program //===================================================================================