Fredagsoppdatering

This commit is contained in:
Kristian Knarvik 2018-03-09 21:29:56 +01:00
parent 0b143037de
commit 5ec539fda2
3 changed files with 44 additions and 15 deletions

View File

@ -28,7 +28,6 @@ public class Rabbit implements INonPlayer {
}
//TODO: It seems the rabbit moves twice. We don't want that.
if (NPC.tryAttack(game)) {
return;
}
@ -40,24 +39,18 @@ public class Rabbit implements INonPlayer {
if (eaten > 0) {
System.out.println("ate carrot worth " + eaten + "!");
food += eaten;
//game.displayMessage("You hear a faint crunching (" + getName() + " eats " + item.getArticle() + " "
//+ item.getName() + ")");
game.displayMessage("You hear a faint crunching (" + getName() + " eats " + item.getArticle() + " "
+ item.getName() + ")");
return;
}
}
}
if (NPC.trackItem(game, Carrot.class)) {
return;
}
List<GridDirection> possibleMoves = game.getPossibleMoves();
for (GridDirection dir : possibleMoves) {
ILocation loc = game.getLocation(dir);
for (IItem item : game.getMap().getItems(loc)) {
if (item instanceof Carrot) {
game.move(dir);
return;
}
}
}
if (!possibleMoves.isEmpty()) {
Collections.shuffle(possibleMoves);
game.move(possibleMoves.get(0));
@ -106,7 +99,7 @@ public class Rabbit implements INonPlayer {
@Override
public int getVision() {
return 5;
return 4;
}
@Override

View File

@ -376,7 +376,21 @@ public class Game implements IGame {
@Override
public List<ILocation> getVisible() {
return map.getNeighbourhood(currentLocation, currentActor.getVision());
List<ILocation> neighbours = map.getNeighbourhood(currentLocation, currentActor.getVision());
List<ILocation> valid = new ArrayList<>();
for (ILocation neighbour : neighbours) {
boolean blocked = false;
for (ILocation tile : currentLocation.gridLineTo(neighbour)) {
if (map.hasWall(tile)) {
blocked = true;
break;
}
}
if (!blocked) {
valid.add(neighbour);
}
}
return valid;
}
@Override

View File

@ -4,6 +4,7 @@ import inf101.v18.grid.GridDirection;
import inf101.v18.grid.ILocation;
import inf101.v18.rogue101.game.IGame;
import inf101.v18.rogue101.objects.IActor;
import inf101.v18.rogue101.objects.IItem;
import java.util.List;
@ -33,4 +34,25 @@ public class NPC {
}
return false;
}
public static boolean trackItem(IGame game, Class<?> element) {
List<ILocation> neighbours = game.getVisible();
for (ILocation neighbour : neighbours) {
boolean hasItem = false;
for (IItem item : game.getMap().getAll(neighbour)) {
if (element.isInstance(item)) {
hasItem = true;
}
}
if (hasItem) {
ILocation current = game.getLocation();
GridDirection dir = game.locationDirection(current, neighbour);
if (game.canGo(dir)) {
game.move(dir);
return true;
}
}
}
return false;
}
}