From 4d2a9579956af0b037de1aed775917aa35786c68 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Tue, 22 May 2018 22:13:50 +0200 Subject: [PATCH] small refactoring --- OGP1718-Worms/src/worms/facade/Facade.java | 2 +- OGP1718-Worms/src/worms/model/GameObject.java | 15 ++++++------ OGP1718-Worms/src/worms/model/Program.java | 21 +++++++--------- OGP1718-Worms/src/worms/model/Projectile.java | 24 +++++-------------- OGP1718-Worms/src/worms/model/World.java | 2 ++ OGP1718-Worms/src/worms/model/Worm.java | 20 ++++------------ 6 files changed, 30 insertions(+), 54 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index 32687ef..8cc91ce 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -1072,7 +1072,7 @@ public class Facade implements IFacade { @Override public List executeProgram(Worm worm) throws ModelException { try { - return worm.getProgram().debugExecute(worm); + return worm.getProgram().debugExecute(); } catch(IllegalArgumentException | IllegalStateException | ClassCastException e) { throw new ModelException(e); } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index cf3d170..03c94bd 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -7,10 +7,6 @@ import static java.lang.Math.*; public abstract class GameObject { - /** - * This variable contains the radius of the game object - */ - protected double radius; /** * */ @@ -18,15 +14,15 @@ public abstract class GameObject { /** * */ - private World world; + protected World world; /** * */ - private boolean terminated = false; + protected boolean terminated = false; /** * this variable contains the location of the worm (a Coordinate) */ - private Coordinate location; + protected Coordinate location; /** * @param world @@ -192,6 +188,11 @@ public abstract class GameObject { this.radius = radius; } + /** + * This variable contains the radius of the game object + */ + protected double radius; + /** * Check whether the given radius is a valid radius for the worm * diff --git a/OGP1718-Worms/src/worms/model/Program.java b/OGP1718-Worms/src/worms/model/Program.java index 8bc9169..a657806 100644 --- a/OGP1718-Worms/src/worms/model/Program.java +++ b/OGP1718-Worms/src/worms/model/Program.java @@ -17,11 +17,9 @@ public class Program { private final List printList = new ArrayList<>(); private Stack currentType = new Stack<>(); - private int inLoop = 0; private int inProcedure = 0; private boolean breakProcedure = false; - private boolean enoughAP = true; private IActionHandler actionHandler; @@ -35,10 +33,8 @@ public class Program { proc.forEach(p -> procMap.put(p.getName(), p.getBody())); } - @SuppressWarnings("unchecked") - protected void execute(Worm worm) { + protected void execute() { - // reset everything enoughAP = true; if (callStack.empty()) { @@ -75,9 +71,8 @@ public class Program { } } - public List debugExecute(Worm worm) { - - execute(worm); + public List debugExecute() { + execute(); if (!enoughAP) return null; callStack.clear(); @@ -231,7 +226,7 @@ public class Program { } - public void setActionHandler(IActionHandler actionHandler) { + protected void setActionHandler(IActionHandler actionHandler) { this.actionHandler = actionHandler; } @@ -239,7 +234,7 @@ public class Program { return this.worm; } - public void setWorm(Worm worm) { + protected void setWorm(Worm worm) { this.worm = worm; } @@ -248,10 +243,10 @@ public class Program { } private class CallStackNode { - public Iterator statementIterator; - public Statement lastStatement; + private Iterator statementIterator; + private Statement lastStatement; - public CallStackNode(Iterator st, Statement ls) { + private CallStackNode(Iterator st, Statement ls) { statementIterator = st; lastStatement = ls; } diff --git a/OGP1718-Worms/src/worms/model/Projectile.java b/OGP1718-Worms/src/worms/model/Projectile.java index 3595770..2549dc3 100644 --- a/OGP1718-Worms/src/worms/model/Projectile.java +++ b/OGP1718-Worms/src/worms/model/Projectile.java @@ -138,29 +138,18 @@ public abstract class Projectile extends GameObject { double v = jumpVelocity(); double t = getJumpTime(jumpTimeStep); double a = getOrientation(); - Coordinate newLocation; + Coordinate newLocation = Coordinate.create(getLocation().toArray()); List worms = getWorld().getGameObjectsByClass(Worm.class); - for (Worm worm: worms) { - if (this.getDistance(worm) < 0) { - newLocation = Coordinate.create(getLocation().getX(), getLocation().getY()); - setLocation(newLocation); - } - } - if (getWorld().isAdjacent(getLocation(),getRadius())) { - newLocation = Coordinate.create(getLocation().getX(), getLocation().getY()); - } - else { + if (!getWorld().isAdjacent(getLocation(),getRadius())) { newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0); } - - if (! isValidLocation(newLocation)) { + if (!isValidLocation(newLocation)) { terminate(); return; } - setLocation(newLocation); for (Worm worm: worms) { @@ -177,10 +166,9 @@ public abstract class Projectile extends GameObject { wormLocation.getY() + sin(wormOrientation) * (wormRadius + radius)); } - public void hit(Worm... worm){ - for (Worm wormA : worm){ - wormA.decreaseHitPoints(getHitPoints()); - } + public void hit(Worm worm){ + + worm.decreaseHitPoints(getHitPoints()); terminate(); } } \ No newline at end of file diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 4f1f8ab..1b42e35 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -80,6 +80,8 @@ public class World { Worm active = getWormList().get(this.activeWorm); active.setActionPoints(active.getMaxActionPoints()); active.incrementHitPoints(10); + + if (active.getProgram() != null) active.getProgram().execute(); } public Worm getActiveWorm() { diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 9201984..749fc66 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -7,7 +7,6 @@ import worms.util.IllegalNameException; import static java.lang.Math.*; -import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -111,7 +110,7 @@ public class Worm extends GameObject { throws IllegalArgumentException { super(world, location, radius); - setOrientation(orientation); + this.orientation = orientation; if (!isValidMinRadius(minRadius)) throw new IllegalArgumentException("Invalid min radius"); @@ -119,20 +118,18 @@ public class Worm extends GameObject { if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius"); - setActionPoints(getMaxActionPoints()); + this.actionPoints = this.maxActionPoints; int validName = isValidName(name); if (validName != -1) throw new IllegalNameException(validName, name); this.name = name; - long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000); - setHitPoints(startHitPoints); + this.hitPoints = ThreadLocalRandom.current().nextLong(1001, 2000); if (team != null) { team.addWorm(this); } - setTeam(team); } //=================================================================================== @@ -933,8 +930,6 @@ public class Worm extends GameObject { */ public void fight() { - World world = getWorld(); - for (Worm w : world.getGameObjectsByClass(Worm.class)) { if (w.equals(this)) continue; if (w.getDistance(this) < 0) { @@ -1186,6 +1181,7 @@ public class Worm extends GameObject { if (!world.isAdjacent(location, radius)) { Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY()); if (world.isAdjacent(newLoc, radius)) { + this.radius = radius; setLocation(newLoc); setRadius(radius); return; @@ -1306,14 +1302,12 @@ public class Worm extends GameObject { //=================================================================================== private Program program = null; - private IActionHandler actionHandler = null; public void loadProgram(Program program, IActionHandler actionHandler) { if (getWorld().hasActiveGame()) throw new IllegalStateException(); this.program = program; - this.actionHandler = actionHandler; program.setWorm(this); program.setActionHandler(actionHandler); } @@ -1321,10 +1315,6 @@ public class Worm extends GameObject { public worms.model.Program getProgram() { return program; } - - public IActionHandler getActionHandler() { - return actionHandler; - } // =================================================================================== // endregion @@ -1334,7 +1324,7 @@ public class Worm extends GameObject { super.terminate(); if (team != null) { team.removeWormsFromTeam(this); - setTeam(null); + team = null; } } }