enemies and combat

This commit is contained in:
jslightham
2019-05-27 12:47:12 -04:00
parent 8f8a55949a
commit 9e8c5e812c
10 changed files with 160 additions and 26 deletions

4
data/enemies.dat Normal file
View File

@@ -0,0 +1,4 @@
Enemy Name: Henry Pelatt
Description: A guy
Starting Room: Circle Room
Damage Given: 25

View File

@@ -56,4 +56,5 @@ towels, item
open, verb open, verb
i, verb i, verb
unlock, verb unlock, verb
drop, verb drop, verb
HenryPelatt, enemy

View File

@@ -0,0 +1,12 @@
package com.bayviewglen.zork;
import com.bayviewglen.zork.Entities.Entity;
public class Combat {
private Entity player;
private Entity enemy;
public Combat(Entity player, Entity enemy) {
this.player = player;
this.enemy = enemy;
}
}

View File

@@ -28,13 +28,14 @@ class Command {
private String direction; private String direction;
private ArrayList<String> otherWords; private ArrayList<String> otherWords;
private String item; private String item;
private String enemy;
/** /**
* Create a command object. First and second word must be supplied, but * Create a command object. First and second word must be supplied, but
* either one (or both) can be null. The command word should be null to * either one (or both) can be null. The command word should be null to
* indicate that this was a command that is not recognised by this game. * indicate that this was a command that is not recognised by this game.
*/ */
public Command(String firstWord, ArrayList<String> otherWords, String direction, String item) { public Command(String firstWord, ArrayList<String> otherWords, String direction, String item, String enemy) {
commandWord = firstWord; commandWord = firstWord;
this.otherWords = otherWords; this.otherWords = otherWords;
this.direction = direction; this.direction = direction;
@@ -51,6 +52,7 @@ class Command {
if(direction.equals("d")) if(direction.equals("d"))
this.direction = "down"; this.direction = "down";
this.item = item; this.item = item;
this.enemy = enemy;
} }
/** /**
@@ -100,4 +102,10 @@ class Command {
public String getItem() { public String getItem() {
return item; return item;
} }
public String getEnemy() {
return enemy;
}
public boolean hasEnemy() {
return !enemy.equals("");
}
} }

View File

@@ -64,6 +64,13 @@ class CommandWords {
} }
} }
public static boolean isEnemy(String aString) {
try {
return m_words.get(aString).equals("enemy");
} catch(Exception e) {
return false;
}
}
/* /*
* Print all valid commands to System.out. * Print all valid commands to System.out.
*/ */

View File

@@ -0,0 +1,45 @@
package com.bayviewglen.zork.Entities.Enemies;
import com.bayviewglen.zork.Entities.Entity;
public class Enemy extends Entity{
private int damageGiven;
private String name;
private String description;
private String room;
public Enemy(){
super(100.0, 100.0);
damageGiven = 10;
}
public Enemy(int damageGiven){
super(100.0, 100.0);
this.damageGiven = damageGiven;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String desc) {
this.description = desc;
}
public void setRoom(String room) {
this.room = room;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getRoom() {
return room;
}
public void setDamageGiven(int damageGiven) {
this.damageGiven = damageGiven;
}
}

View File

@@ -1,16 +0,0 @@
package com.bayviewglen.zork.Entities;
public class Enemy {
int damageGiven;
public Enemy(){
super();
damageGiven = 10;
}
public Enemy(int damageGiven){
super();
this.damageGiven = damageGiven;
}
}

View File

@@ -7,6 +7,7 @@ import java.util.HashMap;
import java.util.Scanner; import java.util.Scanner;
import com.bayviewglen.zork.Entities.Player; import com.bayviewglen.zork.Entities.Player;
import com.bayviewglen.zork.Entities.Enemies.Enemy;
import com.bayviewglen.zork.Items.*; import com.bayviewglen.zork.Items.*;
/** /**
@@ -37,6 +38,8 @@ class Game {
// masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the // masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the
// Great Room (assuming you have one). // Great Room (assuming you have one).
private HashMap<String, Room> masterRoomMap; private HashMap<String, Room> masterRoomMap;
private HashMap<Enemy, String> masterEnemyMap;
private Combat currentCombat = null;
//private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>(); //private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
private void initRooms(String fileName) throws Exception { private void initRooms(String fileName) throws Exception {
@@ -108,6 +111,34 @@ class Game {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void initEnemies(String fileName) throws Exception {
masterEnemyMap = new HashMap<Enemy, String>();
Scanner enemyScanner = null;
Enemy e = null;
try {
enemyScanner = new Scanner(new File(fileName));
while (enemyScanner.hasNext()) {
e = new Enemy();
// Read the Name
String enemyName = enemyScanner.nextLine();
e.setName(enemyName.split(":")[1].trim());
// Read the Description
String enemyDescription = enemyScanner.nextLine();
e.setDescription(enemyDescription.split(":")[1].replaceAll("<br>", "\n").trim());
// Read the Room
String startingRoom = enemyScanner.nextLine();
e.setRoom(startingRoom.split(":")[1].trim());
// Read the Damage Given
int damageGiven = Integer.parseInt(enemyScanner.nextLine().split(":")[1].trim());
e.setDamageGiven(damageGiven);
}
}catch(Exception ex) {
}
masterEnemyMap.put(e, e.getRoom());
enemyScanner.close();
}
/** /**
* Create the game and initialise its internal map. * Create the game and initialise its internal map.
@@ -115,6 +146,7 @@ class Game {
public Game() { public Game() {
try { try {
initRooms("data/rooms.dat"); initRooms("data/rooms.dat");
initEnemies("data/enemies.dat");
currentRoom = masterRoomMap.get("CIRCLE_ROOM"); currentRoom = masterRoomMap.get("CIRCLE_ROOM");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@@ -135,7 +167,8 @@ class Game {
System.out.println("\nType 'help' if you need help, consult the wiki \non GitHub if you are confused and enjoy the game!\n"); System.out.println("\nType 'help' if you need help, consult the wiki \non GitHub if you are confused and enjoy the game!\n");
System.out.println("\n\nEscape Casa Loma: A text-based escape game"); System.out.println("\n\nEscape Casa Loma: A text-based escape game");
System.out.println("---------------------\n"); System.out.println("---------------------\n");
System.out.println(currentRoom.longDescription()); System.out.print(currentRoom.longDescription());
System.out.println(currentRoom.exitString());
boolean finished = false; boolean finished = false;
while (!finished) { while (!finished) {
Command command = parser.getCommand(); Command command = parser.getCommand();
@@ -325,6 +358,23 @@ class Game {
System.out.println("Drop what?"); System.out.println("Drop what?");
} }
break; break;
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) {
}
}
}
break;
default: default:
return false; return false;
} }
@@ -361,8 +411,28 @@ class Game {
} }
else { else {
currentRoom = nextRoom; currentRoom = nextRoom;
System.out.println(currentRoom.longDescription()); System.out.print(currentRoom.longDescription());
boolean hasEnemy = false;
Enemy enemy = null;
String room = "";
for (String i : masterEnemyMap.values()) {
if(currentRoom.getRoomName().equals(i)) {
hasEnemy = true;
room = i;
}
}
for (Enemy i : masterEnemyMap.keySet()) {
if(masterEnemyMap.get(i).equals(room)) {
enemy = i;
}
}
if(hasEnemy) {
System.out.println(enemy.getName() + ", " + enemy.getDescription() + " has appeared!");
System.out.println(currentRoom.exitString());
}else {
System.out.println(currentRoom.exitString());
}
} }
} }
} }

View File

@@ -35,6 +35,7 @@ class Parser {
String verb = ""; String verb = "";
String direction = ""; String direction = "";
String item = ""; String item = "";
String enemy = "";
boolean open = false; boolean open = false;
//String word2; //String word2;
ArrayList<String> words = new ArrayList<String>(); ArrayList<String> words = new ArrayList<String>();
@@ -61,6 +62,8 @@ class Parser {
} }
}else if(CommandWords.isItem(words.get(i))){ }else if(CommandWords.isItem(words.get(i))){
item = words.get(i); item = words.get(i);
}else if(CommandWords.isEnemy(words.get(i))){
enemy = words.get(i);
}else{ }else{
otherWords.add(words.get(i)); otherWords.add(words.get(i));
} }
@@ -68,11 +71,11 @@ class Parser {
//System.out.println(verb); //System.out.println(verb);
if (CommandWords.isCommand(verb)) if (CommandWords.isCommand(verb))
if(!open) if(!open)
return new Command(verb, otherWords, direction, item); return new Command(verb, otherWords, direction, item, enemy);
else else
return new Command("open", otherWords, direction, item); return new Command("open", otherWords, direction, item, enemy);
else else
return new Command(null, otherWords, direction, item); return new Command(null, otherWords, direction, item, enemy);
} }
/** /**

View File

@@ -158,14 +158,14 @@ class Room {
*/ */
public String longDescription() { public String longDescription() {
return "Room: " + roomName + "\n\n" + description + "\n" + exitString(); return "Room: " + roomName + "\n\n" + description + "\n";
} }
/** /**
* Return a string describing the room's exits, for example "Exits: north * Return a string describing the room's exits, for example "Exits: north
* west ". * west ".
*/ */
private String exitString() { public String exitString() {
String returnString = "Exits:"; String returnString = "Exits:";
Set keys = exits.keySet(); Set keys = exits.keySet();
for (Iterator iter = keys.iterator(); iter.hasNext();) for (Iterator iter = keys.iterator(); iter.hasNext();)