Fixed conflicts

This commit is contained in:
Luca Carnegie
2019-05-27 14:43:00 -04:00
7 changed files with 89 additions and 13 deletions

View File

@@ -57,4 +57,5 @@ open, verb
i, verb
unlock, verb
drop, verb
HenryPelatt, enemy
henrypelatt, enemy
attack, verb

View File

@@ -1,12 +1,58 @@
package com.bayviewglen.zork;
import java.lang.reflect.Constructor;
import com.bayviewglen.zork.Entities.Entity;
import com.bayviewglen.zork.Entities.Enemies.Enemy;
import com.bayviewglen.zork.Items.Item;
public class Combat {
private Entity player;
private Entity enemy;
public Combat(Entity player, Entity enemy) {
private Enemy enemy;
// if turn is 0 it is player's turn, if 1 it is enemy's turn
private int turn;
public Combat(Entity player, Enemy enemy) {
this.player = player;
this.enemy = enemy;
}
// return new health of enemy
public double playerAttack(String item) {
Class<?> clazz;
Item object;
try {
clazz = Class.forName("com.bayviewglen.zork.Items." + item.substring(0, 1).toUpperCase().trim() + item.substring(1).trim());
Constructor<?> ctor = clazz.getConstructor();
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 {
System.out.println("You missed!");
}
}catch(Exception e) {
}
turn = 1;
return enemy.getHealth();
}
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 {
System.out.println(enemy.getName() + " missed!");
}
turn = 0;
return player.getHealth();
}
public int getTurn() {
return this.turn;
}
}

View File

@@ -42,4 +42,7 @@ public class Enemy extends Entity{
public void setDamageGiven(int damageGiven) {
this.damageGiven = damageGiven;
}
public int getDamage() {
return this.damageGiven;
}
}

View File

@@ -12,4 +12,7 @@ public class Entity {
public double getHealth() {
return this.health;
}
public void setHealth(double health) {
this.health = health;
}
}

View File

@@ -193,6 +193,11 @@ class Game {
System.out.println(currentRoom.exitString());
boolean finished = false;
while (!finished) {
if(currentCombat != null) {
if(currentCombat.getTurn() == 1) {
currentCombat.enemyAttack();
}
}
Command command = parser.getCommand();
finished = processCommand(command);
}
@@ -373,18 +378,31 @@ class Game {
case "attack":
if(currentCombat == null) {
if(command.hasEnemy()) {
Class<?> clazz;
Enemy object;
try {
clazz = Class.forName("com.bayviewglen.zork.Enemies." + command.getEnemy().substring(0, 1).toUpperCase().trim() + command.getEnemy().substring(1).trim());
Constructor<?> ctor = clazz.getConstructor();
object = (Enemy) ctor.newInstance();
currentCombat = new Combat(player, object);
}catch(Exception e) {
Enemy enemy = null;
for (Enemy i : masterEnemyMap.keySet()) {
if(masterEnemyMap.get(i).equals(currentRoom.getRoomName())) {
enemy = i;
}
}
if(enemy != null) {
if(command.hasItem()) {
currentCombat = new Combat(player, enemy);
currentCombat.playerAttack(command.getItem());
}else {
System.out.println("Attack with what?");
}
}else {
System.out.println("That enemy is not in this room!");
}
}
}else {
System.out.println("Attack what?");
}
} else {
if(command.hasItem()) {
currentCombat.playerAttack(command.getItem());
}
}
break;
default:

View File

@@ -16,6 +16,7 @@ public class Item {
this.isConsumable = isConsumable;
this.health = health;
this.weight = weight;
this.damage = 1;
}
public Item(int id, String name, String description, int weight, int damage) {
@@ -63,5 +64,8 @@ public class Item {
public boolean equals(Item item){
return this.id == item.id && this.name.equals(item.name) && this.description.equals(item.description) && this.isConsumable == item.isConsumable && this.health == item.health && this.weight == item.weight;
}
public int getDamage() {
return this.damage;
}
}

View File

@@ -5,4 +5,5 @@ public class Pen extends Item{
super(11, "Pen", "An old-fashioned feathered pen with ink dried on the tip", false, 10, 1);
}
}