This commit is contained in:
Leen Dereu
2018-05-22 22:15:59 +02:00
6 changed files with 30 additions and 54 deletions

View File

@@ -1072,7 +1072,7 @@ public class Facade implements IFacade {
@Override @Override
public List<Object> executeProgram(Worm worm) throws ModelException { public List<Object> executeProgram(Worm worm) throws ModelException {
try { try {
return worm.getProgram().debugExecute(worm); return worm.getProgram().debugExecute();
} catch(IllegalArgumentException | IllegalStateException | ClassCastException e) { } catch(IllegalArgumentException | IllegalStateException | ClassCastException e) {
throw new ModelException(e); throw new ModelException(e);
} }

View File

@@ -7,10 +7,6 @@ import static java.lang.Math.*;
public abstract class GameObject { 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) * this variable contains the location of the worm (a Coordinate)
*/ */
private Coordinate location; protected Coordinate location;
/** /**
* @param world * @param world
@@ -192,6 +188,11 @@ public abstract class GameObject {
this.radius = radius; 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 * Check whether the given radius is a valid radius for the worm
* *

View File

@@ -17,11 +17,9 @@ public class Program {
private final List<Object> printList = new ArrayList<>(); private final List<Object> printList = new ArrayList<>();
private Stack<Statement.Type> currentType = new Stack<>(); private Stack<Statement.Type> currentType = new Stack<>();
private int inLoop = 0; private int inLoop = 0;
private int inProcedure = 0; private int inProcedure = 0;
private boolean breakProcedure = false; private boolean breakProcedure = false;
private boolean enoughAP = true; private boolean enoughAP = true;
private IActionHandler actionHandler; private IActionHandler actionHandler;
@@ -35,10 +33,8 @@ public class Program {
proc.forEach(p -> procMap.put(p.getName(), p.getBody())); proc.forEach(p -> procMap.put(p.getName(), p.getBody()));
} }
@SuppressWarnings("unchecked") protected void execute() {
protected void execute(Worm worm) {
// reset everything
enoughAP = true; enoughAP = true;
if (callStack.empty()) { if (callStack.empty()) {
@@ -75,9 +71,8 @@ public class Program {
} }
} }
public List<Object> debugExecute(Worm worm) { public List<Object> debugExecute() {
execute();
execute(worm);
if (!enoughAP) return null; if (!enoughAP) return null;
callStack.clear(); callStack.clear();
@@ -231,7 +226,7 @@ public class Program {
} }
public void setActionHandler(IActionHandler actionHandler) { protected void setActionHandler(IActionHandler actionHandler) {
this.actionHandler = actionHandler; this.actionHandler = actionHandler;
} }
@@ -239,7 +234,7 @@ public class Program {
return this.worm; return this.worm;
} }
public void setWorm(Worm worm) { protected void setWorm(Worm worm) {
this.worm = worm; this.worm = worm;
} }
@@ -248,10 +243,10 @@ public class Program {
} }
private class CallStackNode { private class CallStackNode {
public Iterator<Statement> statementIterator; private Iterator<Statement> statementIterator;
public Statement lastStatement; private Statement lastStatement;
public CallStackNode(Iterator<Statement> st, Statement ls) { private CallStackNode(Iterator<Statement> st, Statement ls) {
statementIterator = st; statementIterator = st;
lastStatement = ls; lastStatement = ls;
} }

View File

@@ -139,29 +139,18 @@ public abstract class Projectile extends GameObject {
double v = jumpVelocity(); double v = jumpVelocity();
double t = getJumpTime(jumpTimeStep); double t = getJumpTime(jumpTimeStep);
double a = getOrientation(); double a = getOrientation();
Coordinate newLocation; Coordinate newLocation = Coordinate.create(getLocation().toArray());
List<Worm> worms = getWorld().getGameObjectsByClass(Worm.class); List<Worm> 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())) { if (!getWorld().isAdjacent(getLocation(),getRadius())) {
newLocation = Coordinate.create(getLocation().getX(), getLocation().getY());
}
else {
newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0); 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(); terminate();
return; return;
} }
setLocation(newLocation); setLocation(newLocation);
for (Worm worm: worms) { for (Worm worm: worms) {
@@ -178,10 +167,9 @@ public abstract class Projectile extends GameObject {
wormLocation.getY() + sin(wormOrientation) * (wormRadius + radius)); wormLocation.getY() + sin(wormOrientation) * (wormRadius + radius));
} }
public void hit(Worm... worm){ public void hit(Worm worm){
for (Worm wormA : worm){
wormA.decreaseHitPoints(getHitPoints()); worm.decreaseHitPoints(getHitPoints());
}
terminate(); terminate();
} }
} }

View File

@@ -80,6 +80,8 @@ public class World {
Worm active = getWormList().get(this.activeWorm); Worm active = getWormList().get(this.activeWorm);
active.setActionPoints(active.getMaxActionPoints()); active.setActionPoints(active.getMaxActionPoints());
active.incrementHitPoints(10); active.incrementHitPoints(10);
if (active.getProgram() != null) active.getProgram().execute();
} }
public Worm getActiveWorm() { public Worm getActiveWorm() {

View File

@@ -7,7 +7,6 @@ import worms.util.IllegalNameException;
import static java.lang.Math.*; import static java.lang.Math.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@@ -111,7 +110,7 @@ public class Worm extends GameObject {
throws IllegalArgumentException { throws IllegalArgumentException {
super(world, location, radius); super(world, location, radius);
setOrientation(orientation); this.orientation = orientation;
if (!isValidMinRadius(minRadius)) if (!isValidMinRadius(minRadius))
throw new IllegalArgumentException("Invalid min radius"); throw new IllegalArgumentException("Invalid min radius");
@@ -119,20 +118,18 @@ public class Worm extends GameObject {
if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius"); if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius");
setActionPoints(getMaxActionPoints()); this.actionPoints = this.maxActionPoints;
int validName = isValidName(name); int validName = isValidName(name);
if (validName != -1) if (validName != -1)
throw new IllegalNameException(validName, name); throw new IllegalNameException(validName, name);
this.name = name; this.name = name;
long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000); this.hitPoints = ThreadLocalRandom.current().nextLong(1001, 2000);
setHitPoints(startHitPoints);
if (team != null) { if (team != null) {
team.addWorm(this); team.addWorm(this);
} }
setTeam(team);
} }
//=================================================================================== //===================================================================================
@@ -933,8 +930,6 @@ public class Worm extends GameObject {
*/ */
public void fight() { public void fight() {
World world = getWorld();
for (Worm w : world.getGameObjectsByClass(Worm.class)) { for (Worm w : world.getGameObjectsByClass(Worm.class)) {
if (w.equals(this)) continue; if (w.equals(this)) continue;
if (w.getDistance(this) < 0) { if (w.getDistance(this) < 0) {
@@ -1186,6 +1181,7 @@ public class Worm extends GameObject {
if (!world.isAdjacent(location, radius)) { if (!world.isAdjacent(location, radius)) {
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY()); Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY());
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
this.radius = radius;
setLocation(newLoc); setLocation(newLoc);
setRadius(radius); setRadius(radius);
return; return;
@@ -1306,14 +1302,12 @@ public class Worm extends GameObject {
//=================================================================================== //===================================================================================
private Program program = null; private Program program = null;
private IActionHandler actionHandler = null;
public void loadProgram(Program program, IActionHandler actionHandler) { public void loadProgram(Program program, IActionHandler actionHandler) {
if (getWorld().hasActiveGame()) throw new IllegalStateException(); if (getWorld().hasActiveGame()) throw new IllegalStateException();
this.program = program; this.program = program;
this.actionHandler = actionHandler;
program.setWorm(this); program.setWorm(this);
program.setActionHandler(actionHandler); program.setActionHandler(actionHandler);
} }
@@ -1321,10 +1315,6 @@ public class Worm extends GameObject {
public worms.model.Program getProgram() { public worms.model.Program getProgram() {
return program; return program;
} }
public IActionHandler getActionHandler() {
return actionHandler;
}
// =================================================================================== // ===================================================================================
// endregion // endregion
@@ -1334,7 +1324,7 @@ public class Worm extends GameObject {
super.terminate(); super.terminate();
if (team != null) { if (team != null) {
team.removeWormsFromTeam(this); team.removeWormsFromTeam(this);
setTeam(null); team = null;
} }
} }
} }