This commit is contained in:
2018-04-13 23:54:18 +02:00
parent a6c127ee3b
commit 8c2cb68d22
2 changed files with 38 additions and 17 deletions

View File

@@ -507,27 +507,44 @@ public class Worm extends GameObject {
World world = getWorld(); World world = getWorld();
double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2)); double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2));
if (step > radius) step = radius; if (step > radius) step = radius;
Coordinate nextLoc, prevLoc; Coordinate nextLoc = currentLocation;
nextLoc = prevLoc = currentLocation;
while(getDistance(currentLocation, nextLoc) < maxDistance) { while(getDistance(currentLocation, nextLoc) < maxDistance) {
prevLoc = nextLoc; nextLoc = Coordinate.create(round((nextLoc.getX() + 0.1 * cos(direction)) * 100.0) / 100.0,
nextLoc = Coordinate.create(round((nextLoc.getX() + step * cos(direction)) * 100.0) / 100.0,
round((nextLoc.getY() + step * sin(direction)) * 100.0) / 100.0); round((nextLoc.getY() + step * sin(direction)) * 100.0) / 100.0);
if (!world.isPassable(nextLoc.toArray(), radius)) { if (!world.isPassable(nextLoc.toArray(), radius)) {
while (!world.isPassable(nextLoc.toArray(), radius)) { double max = radius * 0.1 + 0.001;
nextLoc = Coordinate.create(round((nextLoc.getX() - 0.1 * cos(direction)) * 10.0) / 10.0, while (!world.isAdjacent(nextLoc.toArray(), radius)) {
round((nextLoc.getY() - 0.1 * sin(direction)) * 10.0) / 10.0); max -= 0.01;
if (nextLoc.getX() < 0 || nextLoc.getY() < 0) { nextLoc = Coordinate.create(round((nextLoc.getX() - 0.01 * cos(direction)) * 100.0) / 100.0,
nextLoc = prevLoc; round((nextLoc.getY() - 0.01 * sin(direction)) * 100.0) / 100.0);
break; if (max < 0) {
return currentLocation;
} }
} }
break; break;
} }
} }
if (getDistance(currentLocation, prevLoc) > getDistance(currentLocation, nextLoc)) return prevLoc; // while(getDistance(currentLocation, nextLoc) < maxDistance) {
// nextLoc = Coordinate.create(round((nextLoc.getX() + 0.1 * cos(direction)) * 100.0) / 100.0,
// round((nextLoc.getY() + 0.1 * sin(direction)) * 100.0) / 100.0);
// if (!world.isPassable(nextLoc.toArray(), radius)) {
// break;
// }
// }
// double max = radius * 0.1 + 0.001;
// while(!world.isAdjacent(nextLoc.toArray(), radius)) {
// max -= 0.01;
// if (max < 0) {
// nextLoc = currentLocation;
// break;
// }
// nextLoc = Coordinate.create(round((nextLoc.getX() - 0.01 * cos(direction)) * 100.0) / 100.0,
// round((nextLoc.getY() - 0.01 * sin(direction)) * 100.0) / 100.0);
// }
//if (getDistance(currentLocation, prevLoc) > getDistance(currentLocation, nextLoc)) return prevLoc;
return nextLoc; return nextLoc;
} }
@@ -537,14 +554,17 @@ public class Worm extends GameObject {
double minDirection = direction - 0.7875; double minDirection = direction - 0.7875;
double maxDirection = direction + 0.7875; double maxDirection = direction + 0.7875;
double maxLocDirection = maxDirection; double maxLocDirection = maxDirection;
Coordinate maxLoc = getFurthestLocationInDirection(direction, this.getRadius()); Coordinate maxLoc = location;
for (; minDirection <= maxDirection; minDirection += 0.0175) { for (; minDirection <= maxDirection; minDirection += 0.0175) {
Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.getRadius()); Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.getRadius());
if (!getWorld().isAdjacent(tempLoc, getRadius())) tempLoc = location;
if (getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) { if (getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) {
maxLoc = tempLoc; maxLoc = tempLoc;
maxLocDirection = minDirection; maxLocDirection = minDirection;
} }
} }
if (maxLoc.getX() == location.getX() && maxLoc.getY() == location.getY()) return direction;
return maxLocDirection; return maxLocDirection;
} }
@@ -844,18 +864,18 @@ public class Worm extends GameObject {
//=================================================================================== //===================================================================================
public void fall() { public void fall() {
double heigth = getWorld().getHeight() - getRadius(); double height = getWorld().getHeight() - getRadius();
Coordinate oldLocation = getLocation(); Coordinate oldLocation = getLocation();
if (canFall()) { if (canFall()) {
for (double y = oldLocation.getY(); y <= heigth; y = y - 0.01) { for (double y = oldLocation.getY(); y < height; y -= 0.01) {
Coordinate newLoc = Coordinate.create(oldLocation.getX(), y); Coordinate newLoc = Coordinate.create(oldLocation.getX(), Math.floor(y * 100.0) / 100.0);
if (!isValidLocation(newLoc)) { if (y - radius < 0) {
terminate(); terminate();
break; break;
} }
if (!getWorld().isAdjacent(newLoc, radius)) { if (getWorld().isPassable(newLoc, getRadius())) {
setLocation(newLoc); setLocation(newLoc);
} else { } else {
break; break;

View File

@@ -70,6 +70,7 @@ public class PartialPart2FacadeTest {
Worm worm = facade.createWorm(world, new double[] { 1.5, 2.5 }, 3*Math.PI/2, 0.5, "Test", null); Worm worm = facade.createWorm(world, new double[] { 1.5, 2.5 }, 3*Math.PI/2, 0.5, "Test", null);
assertFalse(facade.canFall(worm)); assertFalse(facade.canFall(worm));
facade.move(worm); facade.move(worm);
System.out.println(facade.getLocation(worm)[1]);
assertTrue(facade.canFall(worm)); assertTrue(facade.canFall(worm));
facade.fall(worm); facade.fall(worm);
double[] xy = facade.getLocation(worm); double[] xy = facade.getLocation(worm);