This commit is contained in:
2018-04-16 21:02:31 +02:00
parent 35fb734030
commit e7490de309
3 changed files with 81 additions and 3 deletions

View File

@@ -272,7 +272,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void eat(Worm worm) { public void eat(Worm worm) {
worm.eat(); worm.checkEat();
} }
// /** // /**

View File

@@ -273,6 +273,16 @@ public class World {
return list; return list;
} }
public List<Food> getFoodList() {
List<Food> list = new ArrayList<>();
for (GameObject x : getGameObjects()) {
if (x.getClass().equals(Food.class)) {
list.add((Food) x);
}
}
return list;
}
public List<GameObject> getGameObjectList(Class cl) { public List<GameObject> getGameObjectList(Class cl) {
List<GameObject> list = new ArrayList<>(); List<GameObject> list = new ArrayList<>();
for (GameObject x : getGameObjects()) { for (GameObject x : getGameObjects()) {

View File

@@ -6,6 +6,7 @@ import worms.util.IllegalNameException;
import static java.lang.Math.*; import static java.lang.Math.*;
import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@@ -495,6 +496,8 @@ public class Worm extends GameObject {
setLocation(newLocation); setLocation(newLocation);
subtractActionPoints(cost); subtractActionPoints(cost);
checkEat();
} }
/** /**
@@ -669,6 +672,8 @@ public class Worm extends GameObject {
} }
setLocation(newLocation); setLocation(newLocation);
setActionPoints(0); setActionPoints(0);
checkEat();
} }
/** /**
@@ -932,6 +937,8 @@ public class Worm extends GameObject {
long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY()); long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY());
decreaseHitPoints(cost); decreaseHitPoints(cost);
checkEat();
} }
public boolean canFall() { public boolean canFall() {
@@ -967,9 +974,70 @@ public class Worm extends GameObject {
// endregion // endregion
public void eat() { public void eat(Food food) {
throw new IllegalStateException();
food.terminate();
double radius = getRadius();
double changeRadius = radius * 0.1;
radius *= 1.1;
setRadius(radius);
World world = getWorld();
Coordinate location = getLocation();
if (!world.isPassable(location, radius)) {
Coordinate newLoc = Coordinate.create(location.getX() + changeRadius, location.getY());
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY());
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX(), location.getY() - changeRadius);
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX(), location.getY() + changeRadius);
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() - changeRadius);
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() + changeRadius);
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX() + changeRadius, location.getY() - changeRadius);
if (world.isPassable(newLoc, radius)) {
setLocation(newLoc);
return;
}
newLoc = Coordinate.create(location.getX() - changeRadius, location.getY() + changeRadius);
if (!world.isPassable(newLoc, radius)) {
terminate();
return;
}
}
setLocation(location);
} }
public void checkEat() {
World world = getWorld();
List<Food> foodList = world.getFoodList();
for (Food food: foodList) {
if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) {
eat(food);
}
}
}
} }