Fixes a misunderstanding

This commit is contained in:
2018-03-13 21:35:38 +01:00
parent 3c1eca4d39
commit b88f3c0ef4
9 changed files with 162 additions and 60 deletions

View File

@@ -19,6 +19,8 @@ import inf101.v18.grid.ILocation;
import inf101.v18.rogue101.Main;
import inf101.v18.rogue101.enemies.Girl;
import inf101.v18.rogue101.examples.Carrot;
import inf101.v18.rogue101.items.IBuffItem;
import inf101.v18.rogue101.items.IWeapon;
import inf101.v18.rogue101.items.Manga;
import inf101.v18.rogue101.examples.Rabbit;
import inf101.v18.rogue101.items.Sword;
@@ -62,14 +64,12 @@ public class Game implements IGame {
private final ITurtle painter;
private final Printer printer;
private int numPlayers = 0;
private Main main;
public Game(Main main, Screen screen, ITurtle painter, Printer printer) {
public Game(Screen screen, ITurtle painter, Printer printer) {
this.painter = painter;
this.printer = printer;
addFactory();
this.main = main;
// NOTE: in a more realistic situation, we will have multiple levels (one map
// per level), and (at least for a Roguelike game) the levels should be
@@ -121,20 +121,67 @@ public class Game implements IGame {
map.add(currentLocation, item);
}
/**
* Calculates the attack of an IActor based on equipped items.
*
* @return The attack
*/
private int getAttack() {
int damage = currentActor.getAttack() + random.nextInt(20) + 1;
IWeapon weapon = (IWeapon)currentActor.getItem(IWeapon.class);
if (weapon != null) {
damage += weapon.getWeaponDamage();
}
IBuffItem buff = (IBuffItem)currentActor.getItem(IBuffItem.class);
if (buff != null) {
damage += buff.getBuffDamage();
}
return damage;
}
/**
* Gets the defence of the current target.
*
* @param target The target to evaluate
* @return The defence of the target
*/
private int getDefence(IItem target) {
int defence = target.getDefence() + 10;
IActor actor = (IActor) target;
IBuffItem item = (IBuffItem) actor.getItem(IBuffItem.class);
if (item != null) {
defence += item.getBuffDefence();
}
return defence;
}
/**
* Gets the damage done against the current target.
*
* @param target The target to evaluate.
* @return The damage done to the target.
*/
private int getDamage(IItem target) {
int damage = currentActor.getDamage();
IActor actor = (IActor) target;
IBuffItem item = (IBuffItem) actor.getItem(IBuffItem.class);
if (item != null) {
damage -= item.getBuffDamageReduction();
}
return damage;
}
@Override
public ILocation attack(GridDirection dir, IItem target) {
ILocation loc = currentLocation.go(dir);
if (!map.has(loc, target)) {
throw new IllegalMoveException("Target isn't there!");
}
//TODO: Add attack and defence power when appropriate items are equipped.
int damage = currentActor.getAttack() + random.nextInt(20) + 1;
int defence = target.getDefence() + 10;
if (damage >= defence) {
int actualDamage = target.handleDamage(this, target, damage);
if (getAttack() >= getDefence(target)) {
int actualDamage = target.handleDamage(this, target, getDamage(target));
formatMessage("%s hits %s for %d damage", currentActor.getName(), target.getName(), actualDamage);
} else {
displayMessage("The attack missed.");
formatMessage("%s tried to hit %s, but missed", currentActor.getName(), target.getName());
}
map.clean(loc);
@@ -147,6 +194,24 @@ public class Game implements IGame {
}
}
@Override
public ILocation rangedAttack(GridDirection dir, IItem target) {
ILocation loc = currentLocation;
if (getAttack() >= getDefence(target)) {
int damage = getDamage(target) / loc.gridDistanceTo(map.getLocation(target));
int actualDamage = target.handleDamage(this, target, damage);
formatMessage("%s hits %s for %d damage", currentActor.getName(), target.getName(), actualDamage);
} else {
formatMessage("%s tried to hit %s, but missed", currentActor.getName(), target.getName());
}
map.clean(map.getLocation(target));
if (target.isDestroyed() && map.has(currentLocation.go(dir), target)) {
return move(dir);
} else {
return currentLocation;
}
}
/**
* Begin a new game turn, or continue to the previous turn
*
@@ -424,17 +489,13 @@ public class Game implements IGame {
return map.getWidth();
}
public void keyPressed(KeyCode code) {
public boolean keyPressed(KeyCode code) {
// only an IPlayer/human can handle keypresses, and only if it's the human's
// turn
if (currentActor instanceof IPlayer) {
/*((IPlayer) currentActor).keyPressed(this, code); // do your thing
if (movePoints <= 0)
doTurn(); // proceed with turn if we're out of moves*/
if (((IPlayer) currentActor).keyPressed(this, code)) {
main.doTurn();
}
return !((IPlayer) currentActor).keyPressed(this, code);
}
return true;
}
@Override
@@ -470,25 +531,6 @@ public class Game implements IGame {
}
}
@Override
public ILocation rangedAttack(GridDirection dir, IItem target) {
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()) {
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(map.getLocation(target));
if (target.isDestroyed() && map.has(currentLocation.go(dir), target)) {
return move(dir);
} else {
return currentLocation;
}
}
@Override
public ITurtle getPainter() {
return painter;