small fixes

This commit is contained in:
2018-05-20 17:19:03 +02:00
parent 6313adaf4c
commit c684158ba9
6 changed files with 21 additions and 29 deletions

View File

@@ -36,7 +36,7 @@ public class Facade implements IFacade {
try { try {
worm.turn(angle); worm.turn(angle);
} }
catch (AssertionError e) { catch (AssertionError | IllegalArgumentException e) {
throw new ModelException(e); throw new ModelException(e);
} }
} }
@@ -359,7 +359,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double[] getJumpStep(Projectile projectile, double elapsedTime) { public double[] getJumpStep(Projectile projectile, double elapsedTime) {
return new double[0]; return projectile.getJumpStep(elapsedTime).toArray();
} }
/** /**
@@ -372,7 +372,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getJumpTime(Projectile projectile, double jumpTimeStep) throws ModelException { public double getJumpTime(Projectile projectile, double jumpTimeStep) throws ModelException {
return 0; return projectile.getJumpTime(jumpTimeStep);
} }
/** /**
@@ -386,7 +386,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void jump(Projectile projectile, double jumpTimeStep) throws ModelException { public void jump(Projectile projectile, double jumpTimeStep) throws ModelException {
projectile.jump(jumpTimeStep);
} }
/** /**

View File

@@ -82,7 +82,7 @@ public abstract class Projectile extends GameObject {
return getForce()/getMass() * FORCE_TIME; return getForce()/getMass() * FORCE_TIME;
} }
private double getJumpTime(double jumpTimeStep) { public double getJumpTime(double jumpTimeStep) {
double v = jumpVelocity(); double v = jumpVelocity();
double time = jumpTimeStep; double time = jumpTimeStep;
double a = getOrientation(); double a = getOrientation();

View File

@@ -249,7 +249,11 @@ public class Worm extends GameObject {
*/ */
@Raw @Raw
private boolean canHaveAsRadius(double radius) { private boolean canHaveAsRadius(double radius) {
return !Double.isNaN(radius) && radius >= getMinRadius() && !Double.isInfinite(radius); if (getWorld() != null) {
return !Double.isNaN(radius) && radius >= getMinRadius() && !Double.isInfinite(radius) && getWorld().isPassable(getLocation(), radius);
} else {
return !Double.isNaN(radius) && radius >= getMinRadius() && !Double.isInfinite(radius);
}
} }
/** /**
@@ -1134,7 +1138,6 @@ public class Worm extends GameObject {
} }
decreaseActionPoints(8); decreaseActionPoints(8);
setRadius(radius);
World world = getWorld(); World world = getWorld();
Coordinate location = getLocation(); Coordinate location = getLocation();
@@ -1142,36 +1145,43 @@ public class Worm extends GameObject {
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)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY()); newLoc = Coordinate.create(location.getX() - changeRadius, location.getY());
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX(), location.getY() - changeRadius); newLoc = Coordinate.create(location.getX(), location.getY() - changeRadius);
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX(), location.getY() + changeRadius); newLoc = Coordinate.create(location.getX(), location.getY() + changeRadius);
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() - changeRadius); newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() - changeRadius);
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() + changeRadius); newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() + changeRadius);
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() - changeRadius); newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() - changeRadius);
if (world.isAdjacent(newLoc, radius)) { if (world.isAdjacent(newLoc, radius)) {
setLocation(newLoc); setLocation(newLoc);
setRadius(radius);
return; return;
} }
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() + changeRadius); newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() + changeRadius);
@@ -1181,6 +1191,7 @@ public class Worm extends GameObject {
} }
} }
setLocation(location); setLocation(location);
setRadius(radius);
} }
/** /**
@@ -1221,7 +1232,7 @@ public class Worm extends GameObject {
if (! getWorld().getGameObjectsByClass(Projectile.class).isEmpty()) { if (! getWorld().getGameObjectsByClass(Projectile.class).isEmpty()) {
List<Projectile> projectiles = getWorld().getGameObjectsByClass(Projectile.class); List<Projectile> projectiles = getWorld().getGameObjectsByClass(Projectile.class);
for (Projectile project: projectiles) { for (Projectile project: projectiles) {
if (this.getDistance(project) <= 0) { if (this.getDistance(project) < 0) {
project.hit(this); project.hit(this);
return null; return null;
} }

View File

@@ -243,7 +243,7 @@ public class ProgramFactory implements IProgramFactory<Expression, Statement, Pr
@Override @Override
public Expression createSelfExpression(SourceLocation location) throws ModelException { public Expression createSelfExpression(SourceLocation location) throws ModelException {
return new UnaryArgumentExpression<Worm>(Program::getWorm); return Program::getWorm;
} }
/** /**

View File

@@ -1,19 +0,0 @@
package worms.programs;
import worms.model.Program;
import java.util.function.Function;
public class UnaryArgumentExpression<R> implements Expression<R> {
private final Function<Program, R> function;
public UnaryArgumentExpression(Function<Program, R> unaryOperator) {
this.function = unaryOperator;
}
@Override
public R execute(Program program) {
return function.apply(program);
}
}

View File

@@ -976,7 +976,7 @@ public class Part3_FullFacadeTest {
@Test @Test
public void createWorm_IllegalWorld() { public void createWorm_IllegalWorld() {
max_score += 2; max_score += 2;
World someWorld = facade.createWorld(10.0, 10.0, map10x10); World someWorld = facade.createWorld(10.0, 20.0, map10x10);
facade.terminate(someWorld); facade.terminate(someWorld);
try { try {
facade.createWorm(someWorld, new double[] { 8.68, 8.68 }, 0.3, 0.3, "Worm", null); facade.createWorm(someWorld, new double[] { 8.68, 8.68 }, 0.3, 0.3, "Worm", null);