worked on combat
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
Enemy Name: Henry Pelatt
|
Enemy Name: HenryPelatt
|
||||||
Description: A guy
|
Description: A guy
|
||||||
Starting Room: Circle Room
|
Starting Room: Circle Room
|
||||||
Damage Given: 25
|
Damage Given: 25
|
||||||
@@ -57,4 +57,5 @@ open, verb
|
|||||||
i, verb
|
i, verb
|
||||||
unlock, verb
|
unlock, verb
|
||||||
drop, verb
|
drop, verb
|
||||||
HenryPelatt, enemy
|
henrypelatt, enemy
|
||||||
|
attack, verb
|
||||||
@@ -1,12 +1,58 @@
|
|||||||
package com.bayviewglen.zork;
|
package com.bayviewglen.zork;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
import com.bayviewglen.zork.Entities.Entity;
|
import com.bayviewglen.zork.Entities.Entity;
|
||||||
|
import com.bayviewglen.zork.Entities.Enemies.Enemy;
|
||||||
|
import com.bayviewglen.zork.Items.Item;
|
||||||
|
|
||||||
public class Combat {
|
public class Combat {
|
||||||
private Entity player;
|
private Entity player;
|
||||||
private Entity enemy;
|
private Enemy enemy;
|
||||||
public Combat(Entity player, Entity 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.player = player;
|
||||||
this.enemy = enemy;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,4 +42,7 @@ public class Enemy extends Entity{
|
|||||||
public void setDamageGiven(int damageGiven) {
|
public void setDamageGiven(int damageGiven) {
|
||||||
this.damageGiven = damageGiven;
|
this.damageGiven = damageGiven;
|
||||||
}
|
}
|
||||||
|
public int getDamage() {
|
||||||
|
return this.damageGiven;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,7 @@ public class Entity {
|
|||||||
public double getHealth() {
|
public double getHealth() {
|
||||||
return this.health;
|
return this.health;
|
||||||
}
|
}
|
||||||
|
public void setHealth(double health) {
|
||||||
|
this.health = health;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -171,6 +171,11 @@ class Game {
|
|||||||
System.out.println(currentRoom.exitString());
|
System.out.println(currentRoom.exitString());
|
||||||
boolean finished = false;
|
boolean finished = false;
|
||||||
while (!finished) {
|
while (!finished) {
|
||||||
|
if(currentCombat != null) {
|
||||||
|
if(currentCombat.getTurn() == 1) {
|
||||||
|
currentCombat.enemyAttack();
|
||||||
|
}
|
||||||
|
}
|
||||||
Command command = parser.getCommand();
|
Command command = parser.getCommand();
|
||||||
finished = processCommand(command);
|
finished = processCommand(command);
|
||||||
}
|
}
|
||||||
@@ -361,18 +366,31 @@ class Game {
|
|||||||
case "attack":
|
case "attack":
|
||||||
if(currentCombat == null) {
|
if(currentCombat == null) {
|
||||||
if(command.hasEnemy()) {
|
if(command.hasEnemy()) {
|
||||||
Class<?> clazz;
|
Enemy enemy = null;
|
||||||
Enemy object;
|
for (Enemy i : masterEnemyMap.keySet()) {
|
||||||
try {
|
if(masterEnemyMap.get(i).equals(currentRoom.getRoomName())) {
|
||||||
clazz = Class.forName("com.bayviewglen.zork.Enemies." + command.getEnemy().substring(0, 1).toUpperCase().trim() + command.getEnemy().substring(1).trim());
|
enemy = i;
|
||||||
Constructor<?> ctor = clazz.getConstructor();
|
}
|
||||||
object = (Enemy) ctor.newInstance();
|
}
|
||||||
currentCombat = new Combat(player, object);
|
if(enemy != null) {
|
||||||
}catch(Exception e) {
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public class Item {
|
|||||||
this.isConsumable = isConsumable;
|
this.isConsumable = isConsumable;
|
||||||
this.health = health;
|
this.health = health;
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
|
this.damage = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Item(int id, String name, String description, int weight, int damage) {
|
public Item(int id, String name, String description, int weight, int damage) {
|
||||||
@@ -63,5 +64,8 @@ public class Item {
|
|||||||
public boolean equals(Item 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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
super(11, "Pen", "An old-fashioned feathered pen with ink dried on the tip", false, 10, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user