Legger til en del fremdeles uimplementerte items

This commit is contained in:
2018-03-10 15:17:50 +01:00
parent 478a8f02a3
commit d1b4f4a316
25 changed files with 433 additions and 58 deletions

View File

@@ -43,6 +43,12 @@ public class Game implements IGame {
* Useful random generator
*/
private Random random = new Random();
/**
* Saves the last three messages
*/
private List<String> lastMessages = new ArrayList<>();
/**
* The game map. {@link IGameMap} gives us a few more details than
* {@link IMapView} (write access to item lists); the game needs this but
@@ -69,7 +75,7 @@ public class Game implements IGame {
// inputGrid will be filled with single-character strings indicating what (if
// anything)
// should be placed at that map square
IGrid<String> inputGrid = MapReader.readFile("maps/level1.txt");
IGrid<String> inputGrid = MapReader.readFile("maps/testmap.txt");
if (inputGrid == null) {
System.err.println("Map not found falling back to builtin map");
inputGrid = MapReader.readString(Main.BUILTIN_MAP);
@@ -159,7 +165,7 @@ public class Game implements IGame {
}
// process actors one by one; for the IPlayer, we return and wait for keypresses
// Possible TODO: for INonPlayer, we could also return early (returning
// Possible for INonPlayer, we could also return early (returning
// *false*), and then insert a little timer delay between each non-player move
// (the timer
// is already set up in Main)
@@ -179,6 +185,7 @@ public class Game implements IGame {
((INonPlayer) currentActor).doTurn(this);
// remove any dead items from current location
map.clean(currentLocation);
return false;
} else if (currentActor instanceof IPlayer) {
if (currentActor.isDestroyed()) {
// a dead human player gets removed from the game
@@ -289,11 +296,23 @@ public class Game implements IGame {
@Override
public void displayMessage(String s) {
// it should be safe to print to lines Main.LINE_MSG1, Main.LINE_MSG2,
// Main.LINE_MSG3
// TODO: you can save the last three lines, and display/scroll them
if (lastMessages.size() >= 3) {
lastMessages.remove(2);
}
lastMessages.add(0, s);
printer.clearLine(Main.LINE_MSG1);
printer.printAt(1, Main.LINE_MSG1, s);
printer.clearLine(Main.LINE_MSG2);
printer.clearLine(Main.LINE_MSG3);
System.out.println(lastMessages.size());
if (lastMessages.size() > 0) {
printer.printAt(1, Main.LINE_MSG1, lastMessages.get(0));
}
if (lastMessages.size() > 1) {
printer.printAt(1, Main.LINE_MSG2, lastMessages.get(1));
}
if (lastMessages.size() > 2) {
printer.printAt(1, Main.LINE_MSG3, lastMessages.get(2));
}
System.out.println("Message: «" + s + "»");
}
@@ -425,11 +444,19 @@ public class Game implements IGame {
@Override
public IItem pickUp(IItem item) {
if (item != null && map.has(currentLocation, item)) {
// TODO: bruk getAttack()/getDefence() til å avgjøre om man får til å plukke opp
// tingen
// evt.: en IActor kan bare plukkes opp hvis den har få/ingen helsepoeng igjen
map.remove(currentLocation, item);
return item;
if (item instanceof IActor) {
if (item.getCurrentHealth() / item.getMaxHealth() < 3) {
map.remove(currentLocation, item);
return item;
} else {
return null;
}
} else if (currentActor.getAttack() > item.getDefence()) {
map.remove(currentLocation, item);
return item;
} else {
return null;
}
} else {
return null;
}
@@ -440,13 +467,13 @@ public class Game implements IGame {
ILocation loc = currentLocation;
int damage = (currentActor.getAttack() + random.nextInt(20) + 1)
/ loc.gridDistanceTo(map.getLocation(target)); //Close attacks will take more damage.
if (damage >= target.getDefence() + 10) {
if (damage >= target.getDefence()) {
int actualDamage = target.handleDamage(this, target, damage);
formatMessage("%s hits %s for %d damage", currentActor.getName(), target.getName(), actualDamage);
} else {
displayMessage("The attack missed.");
}
map.clean(loc);
map.clean(map.getLocation(target));
if (target.isDestroyed() && map.has(currentLocation.go(dir), target)) {
return move(dir);
} else {