From e7490de3098654c1020543b60279fca473892eb9 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Mon, 16 Apr 2018 21:02:31 +0200 Subject: [PATCH] fix eat --- OGP1718-Worms/src/worms/facade/Facade.java | 2 +- OGP1718-Worms/src/worms/model/World.java | 10 +++ OGP1718-Worms/src/worms/model/Worm.java | 72 +++++++++++++++++++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index 76980c7..6950e35 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -272,7 +272,7 @@ public class Facade implements IFacade { */ @Override public void eat(Worm worm) { - worm.eat(); + worm.checkEat(); } // /** diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 43e945b..7d03150 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -273,6 +273,16 @@ public class World { return list; } + public List getFoodList() { + List list = new ArrayList<>(); + for (GameObject x : getGameObjects()) { + if (x.getClass().equals(Food.class)) { + list.add((Food) x); + } + } + return list; + } + public List getGameObjectList(Class cl) { List list = new ArrayList<>(); for (GameObject x : getGameObjects()) { diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 2e41356..9dd4b94 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -6,6 +6,7 @@ import worms.util.IllegalNameException; import static java.lang.Math.*; +import java.util.List; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -495,6 +496,8 @@ public class Worm extends GameObject { setLocation(newLocation); subtractActionPoints(cost); + + checkEat(); } /** @@ -669,6 +672,8 @@ public class Worm extends GameObject { } setLocation(newLocation); setActionPoints(0); + + checkEat(); } /** @@ -932,6 +937,8 @@ public class Worm extends GameObject { long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY()); decreaseHitPoints(cost); + + checkEat(); } public boolean canFall() { @@ -967,9 +974,70 @@ public class Worm extends GameObject { // endregion - public void eat() { - throw new IllegalStateException(); + public void eat(Food food) { + + 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 foodList = world.getFoodList(); + for (Food food: foodList) { + if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) { + eat(food); + } + } + } }