From e4ba347415432c8ceb32fedbc1e989b81af93d16 Mon Sep 17 00:00:00 2001 From: jslightham <31053827+jslightham@users.noreply.github.com> Date: Mon, 27 May 2019 18:39:23 -0400 Subject: [PATCH] finished combat, fixed open door null pointer bug --- data/enemies.dat | 2 +- data/rooms.dat | 2 +- data/words.dat | 2 +- src/com/bayviewglen/zork/Combat.java | 48 ++++++++++++++---- src/com/bayviewglen/zork/Game.java | 62 ++++++++++++++++++++--- src/com/bayviewglen/zork/Items/Sword.java | 2 +- src/com/bayviewglen/zork/Zork.java | 2 +- 7 files changed, 100 insertions(+), 20 deletions(-) diff --git a/data/enemies.dat b/data/enemies.dat index 20db5aa..7e46975 100644 --- a/data/enemies.dat +++ b/data/enemies.dat @@ -1,4 +1,4 @@ -Enemy Name: Henry Pellatt +Enemy Name: HenryPellatt Description: The owner of the Castle Starting Room: Circle Room Damage Given: 25 \ No newline at end of file diff --git a/data/rooms.dat b/data/rooms.dat index 8835007..063f510 100644 --- a/data/rooms.dat +++ b/data/rooms.dat @@ -1,7 +1,7 @@ Room name: Circle Room Room Description: You are in the circular room. The windows to the west are bolted shut and curtains cover them.
To the east, a hallway. A scroll hangs on the north wall. Writing is visible. Locked: false -Items:Lockpick, Scroll +Items:Lockpick,Milk,Scroll Exit Rooms: W-Apple Hallway Room name: Apple Hallway Room Description: You are in an empty hallway. Many closed doors surround you. To the west is the Circle Room and north is the Porcupine Stairs. The door to the stairs is locked. There is no key to the door. diff --git a/data/words.dat b/data/words.dat index aaa1f33..16ceeac 100644 --- a/data/words.dat +++ b/data/words.dat @@ -57,5 +57,5 @@ open, verb i, verb unlock, verb drop, verb -henrypelatt, enemy +henrypellatt, enemy attack, verb \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Combat.java b/src/com/bayviewglen/zork/Combat.java index 33ece35..2791445 100644 --- a/src/com/bayviewglen/zork/Combat.java +++ b/src/com/bayviewglen/zork/Combat.java @@ -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; + } + } diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index 4aa4dc9..5caa3cd 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -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 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); + } } \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Items/Sword.java b/src/com/bayviewglen/zork/Items/Sword.java index 89f655c..abbcf05 100644 --- a/src/com/bayviewglen/zork/Items/Sword.java +++ b/src/com/bayviewglen/zork/Items/Sword.java @@ -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); } } diff --git a/src/com/bayviewglen/zork/Zork.java b/src/com/bayviewglen/zork/Zork.java index 1ec0f82..646914d 100644 --- a/src/com/bayviewglen/zork/Zork.java +++ b/src/com/bayviewglen/zork/Zork.java @@ -1,4 +1,4 @@ - package com.bayviewglen.zork; +package com.bayviewglen.zork; public class Zork { public static void main(String[] args) {