small refactoring
This commit is contained in:
@@ -1072,7 +1072,7 @@ public class Facade implements IFacade {
|
||||
@Override
|
||||
public List<Object> executeProgram(Worm worm) throws ModelException {
|
||||
try {
|
||||
return worm.getProgram().debugExecute(worm);
|
||||
return worm.getProgram().debugExecute();
|
||||
} catch(IllegalArgumentException | IllegalStateException | ClassCastException e) {
|
||||
throw new ModelException(e);
|
||||
}
|
||||
|
@@ -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
|
||||
*
|
||||
|
@@ -17,11 +17,9 @@ public class Program {
|
||||
private final List<Object> printList = new ArrayList<>();
|
||||
|
||||
private Stack<Statement.Type> 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<Object> debugExecute(Worm worm) {
|
||||
|
||||
execute(worm);
|
||||
public List<Object> 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<Statement> statementIterator;
|
||||
public Statement lastStatement;
|
||||
private Iterator<Statement> statementIterator;
|
||||
private Statement lastStatement;
|
||||
|
||||
public CallStackNode(Iterator<Statement> st, Statement ls) {
|
||||
private CallStackNode(Iterator<Statement> st, Statement ls) {
|
||||
statementIterator = st;
|
||||
lastStatement = ls;
|
||||
}
|
||||
|
@@ -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<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())) {
|
||||
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();
|
||||
}
|
||||
}
|
@@ -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() {
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user