finished combat, fixed open door null pointer bug
This commit is contained in:
@@ -25,11 +25,21 @@ public class Combat {
|
||||
object = (Item) ctor.newInstance();
|
||||
|
||||
double rand = Math.random();
|
||||
if(rand>0.1) {
|
||||
enemy.setHealth(enemy.getHealth()-object.getDamage());
|
||||
System.out.println("You did " + object.getDamage() + " damage! " + enemy.getName() + " is now at " + enemy.getHealth() + "% health.");
|
||||
}else {
|
||||
if(rand<0.1) {
|
||||
System.out.println("You missed!");
|
||||
|
||||
|
||||
}else if(rand<0.15) {
|
||||
enemy.setHealth(enemy.getHealth()-object.getDamage()*1.5);
|
||||
if(enemy.getHealth() < 0)
|
||||
enemy.setHealth(0);
|
||||
System.out.println("You hit " + enemy.getName() + " with a critical hit, doing " + object.getDamage()*1.5 + " damage! His health is now " + enemy.getHealth() + "%");
|
||||
}
|
||||
else {
|
||||
enemy.setHealth(enemy.getHealth()-object.getDamage());
|
||||
if(enemy.getHealth() < 0)
|
||||
enemy.setHealth(0);
|
||||
System.out.println("You did " + object.getDamage() + " damage! " + enemy.getName() + " is now at " + enemy.getHealth() + "% health.");
|
||||
}
|
||||
|
||||
}catch(Exception e) {
|
||||
@@ -41,12 +51,19 @@ public class Combat {
|
||||
|
||||
public double enemyAttack() {
|
||||
double rand = Math.random();
|
||||
if(rand>0.1) {
|
||||
player.setHealth(player.getHealth()-enemy.getDamage());
|
||||
System.out.println(enemy.getName() + " did " + enemy.getDamage() + " damage to you! Your health is now " + player.getHealth() + "%");
|
||||
|
||||
}else {
|
||||
if(rand<0.1) {
|
||||
System.out.println(enemy.getName() + " missed!");
|
||||
}else if(rand < 0.15) {
|
||||
player.setHealth(player.getHealth()-enemy.getDamage()*1.5);
|
||||
if(player.getHealth() < 0)
|
||||
player.setHealth(0);
|
||||
System.out.println(enemy.getName() + " hit you with a critical hit, doing " + enemy.getDamage()*1.5 + " damage! Your health is now " + player.getHealth() + "%");
|
||||
}
|
||||
else {
|
||||
player.setHealth(player.getHealth()-enemy.getDamage());
|
||||
if(player.getHealth() < 0)
|
||||
player.setHealth(0);
|
||||
System.out.println(enemy.getName() + " did " + enemy.getDamage() + " damage to you! Your health is now " + player.getHealth() + "%");
|
||||
}
|
||||
turn = 0;
|
||||
return player.getHealth();
|
||||
@@ -55,4 +72,17 @@ public class Combat {
|
||||
public int getTurn() {
|
||||
return this.turn;
|
||||
}
|
||||
|
||||
public void setEnemyTurn() {
|
||||
turn = 1;
|
||||
}
|
||||
|
||||
public Enemy getEnemy() {
|
||||
return enemy;
|
||||
}
|
||||
|
||||
public Entity getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -194,10 +194,28 @@ class Game {
|
||||
boolean finished = false;
|
||||
while (!finished) {
|
||||
if(currentCombat != null) {
|
||||
if(currentCombat.getTurn() == 1) {
|
||||
if(currentCombat.getEnemy().getHealth() <= 0.0) {
|
||||
System.out.println("You destroyed " + currentCombat.getEnemy().getName());
|
||||
masterEnemyMap.values().remove(currentRoom.getRoomName());
|
||||
currentCombat = null;
|
||||
}
|
||||
else if(currentCombat.getTurn() == 1) {
|
||||
currentCombat.enemyAttack();
|
||||
if(currentCombat.getPlayer().getHealth() <=0.0) {
|
||||
System.out.println("You were destroyed by " + currentCombat.getEnemy().getName());
|
||||
for(int i =0; i<player.getInventory().size(); i++) {
|
||||
currentRoom.addItem(player.getInventory().get(i));
|
||||
player.removeFromInventory(player.getInventory().get(i));
|
||||
}
|
||||
currentRoom = masterRoomMap.get("CIRCLE_ROOM");
|
||||
System.out.println("You are now back in the circle room. Your items remain where you died.");
|
||||
player.setHealth(100.0);
|
||||
currentCombat.getEnemy().setHealth(100.0);
|
||||
currentCombat = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Command command = parser.getCommand();
|
||||
finished = processCommand(command);
|
||||
}
|
||||
@@ -229,6 +247,7 @@ class Game {
|
||||
|
||||
if(command.hasDirection() && hasLockPick) {
|
||||
Room nextRoom = currentRoom.nextRoom(command.getDirection());
|
||||
try {
|
||||
if(nextRoom.getLocked()) {
|
||||
nextRoom.setLocked(false);
|
||||
player.removeFromInventory(new Lockpick());
|
||||
@@ -236,6 +255,9 @@ class Game {
|
||||
}else{
|
||||
System.out.println("That door is already unlocked!");
|
||||
}
|
||||
}catch(Exception e) {
|
||||
System.out.println("There is no door there!");
|
||||
}
|
||||
}else if(!command.hasDirection()){
|
||||
System.out.println("You must specify a direction!");
|
||||
}else {
|
||||
@@ -243,7 +265,10 @@ class Game {
|
||||
}
|
||||
break;
|
||||
case "go": case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down": case "d": case "u":
|
||||
if(currentCombat == null)
|
||||
goRoom(command);
|
||||
else
|
||||
System.out.println("You can't leave the room during combat!");
|
||||
break;
|
||||
case "help":
|
||||
printHelp();
|
||||
@@ -257,7 +282,7 @@ class Game {
|
||||
System.out.println("If you insist... \n Poof! You're gone. You're out of the castle now, but now a new, grand new adventure begins...");
|
||||
return true;
|
||||
case "eat":
|
||||
//System.out.println("Do you really think you should be eating at a time like this?");
|
||||
|
||||
if(command.hasItem()) {
|
||||
Class<?> clazz;
|
||||
Item object;
|
||||
@@ -273,9 +298,11 @@ class Game {
|
||||
}
|
||||
if(object.isConsumable() && hasItem) {
|
||||
System.out.println("Yum!");
|
||||
System.out.println("Your health is now " + player.getHealth() + "%");
|
||||
player.eat();
|
||||
System.out.println("Your health is now " + player.getHealth() + "%");
|
||||
player.removeFromInventory(object);
|
||||
if(currentCombat != null)
|
||||
currentCombat.setEnemyTurn();
|
||||
}else if(object.isConsumable()) {
|
||||
System.out.println("You do not have a " + command.getItem());
|
||||
}else {
|
||||
@@ -386,8 +413,18 @@ class Game {
|
||||
}
|
||||
if(enemy != null) {
|
||||
if(command.hasItem()) {
|
||||
currentCombat = new Combat(player, enemy);
|
||||
currentCombat.playerAttack(command.getItem());
|
||||
boolean has = false;
|
||||
for(Item i : player.getInventory()) {
|
||||
if(i.getName().toLowerCase().equals(command.getItem())) {
|
||||
has = true;
|
||||
}
|
||||
}
|
||||
if(has) {
|
||||
currentCombat = new Combat(player, enemy);
|
||||
currentCombat.playerAttack(command.getItem());
|
||||
}else {
|
||||
System.out.println("You do not have that weapon!");
|
||||
}
|
||||
}else {
|
||||
System.out.println("Attack with what?");
|
||||
}
|
||||
@@ -401,7 +438,17 @@ class Game {
|
||||
|
||||
} else {
|
||||
if(command.hasItem()) {
|
||||
currentCombat.playerAttack(command.getItem());
|
||||
boolean has = false;
|
||||
for(Item i : player.getInventory()) {
|
||||
if(i.getName().toLowerCase().equals(command.getItem())) {
|
||||
has = true;
|
||||
}
|
||||
}
|
||||
if(has) {
|
||||
currentCombat.playerAttack(command.getItem());
|
||||
}else {
|
||||
System.out.println("You do not have that weapon!");
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -465,4 +512,7 @@ class Game {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeEnemy(String r) {
|
||||
masterEnemyMap.values().remove(r);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.bayviewglen.zork.Items;
|
||||
public class Sword extends Item{
|
||||
|
||||
public Sword(){
|
||||
super(33, "Sword", "A steel, double-edged sword which seems to have been sharpened recently", 20, 50);
|
||||
super(33, "Sword", "A steel, double-edged sword which seems to have been sharpened recently", 20, 20);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.bayviewglen.zork;
|
||||
package com.bayviewglen.zork;
|
||||
|
||||
public class Zork {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user