enemies and combat
This commit is contained in:
4
data/enemies.dat
Normal file
4
data/enemies.dat
Normal file
@@ -0,0 +1,4 @@
|
||||
Enemy Name: Henry Pelatt
|
||||
Description: A guy
|
||||
Starting Room: Circle Room
|
||||
Damage Given: 25
|
||||
@@ -57,3 +57,4 @@ open, verb
|
||||
i, verb
|
||||
unlock, verb
|
||||
drop, verb
|
||||
HenryPelatt, enemy
|
||||
12
src/com/bayviewglen/zork/Combat.java
Normal file
12
src/com/bayviewglen/zork/Combat.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -28,13 +28,14 @@ class Command {
|
||||
private String direction;
|
||||
private ArrayList<String> otherWords;
|
||||
private String item;
|
||||
private String enemy;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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;
|
||||
this.otherWords = otherWords;
|
||||
this.direction = direction;
|
||||
@@ -51,6 +52,7 @@ class Command {
|
||||
if(direction.equals("d"))
|
||||
this.direction = "down";
|
||||
this.item = item;
|
||||
this.enemy = enemy;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,4 +102,10 @@ class Command {
|
||||
public String getItem() {
|
||||
return item;
|
||||
}
|
||||
public String getEnemy() {
|
||||
return enemy;
|
||||
}
|
||||
public boolean hasEnemy() {
|
||||
return !enemy.equals("");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
45
src/com/bayviewglen/zork/Entities/Enemies/Enemy.java
Normal file
45
src/com/bayviewglen/zork/Entities/Enemies/Enemy.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.bayviewglen.zork.Entities.Player;
|
||||
import com.bayviewglen.zork.Entities.Enemies.Enemy;
|
||||
import com.bayviewglen.zork.Items.*;
|
||||
|
||||
/**
|
||||
@@ -37,6 +38,8 @@ class Game {
|
||||
// masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the
|
||||
// Great Room (assuming you have one).
|
||||
private HashMap<String, Room> masterRoomMap;
|
||||
private HashMap<Enemy, String> masterEnemyMap;
|
||||
private Combat currentCombat = null;
|
||||
//private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
|
||||
|
||||
private void initRooms(String fileName) throws Exception {
|
||||
@@ -109,12 +112,41 @@ class Game {
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
public Game() {
|
||||
try {
|
||||
initRooms("data/rooms.dat");
|
||||
initEnemies("data/enemies.dat");
|
||||
currentRoom = masterRoomMap.get("CIRCLE_ROOM");
|
||||
} catch (Exception e) {
|
||||
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("\n\nEscape Casa Loma: A text-based escape game");
|
||||
System.out.println("---------------------\n");
|
||||
System.out.println(currentRoom.longDescription());
|
||||
System.out.print(currentRoom.longDescription());
|
||||
System.out.println(currentRoom.exitString());
|
||||
boolean finished = false;
|
||||
while (!finished) {
|
||||
Command command = parser.getCommand();
|
||||
@@ -325,6 +358,23 @@ class Game {
|
||||
System.out.println("Drop what?");
|
||||
}
|
||||
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:
|
||||
return false;
|
||||
}
|
||||
@@ -361,7 +411,27 @@ class Game {
|
||||
}
|
||||
else {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ class Parser {
|
||||
String verb = "";
|
||||
String direction = "";
|
||||
String item = "";
|
||||
String enemy = "";
|
||||
boolean open = false;
|
||||
//String word2;
|
||||
ArrayList<String> words = new ArrayList<String>();
|
||||
@@ -61,6 +62,8 @@ class Parser {
|
||||
}
|
||||
}else if(CommandWords.isItem(words.get(i))){
|
||||
item = words.get(i);
|
||||
}else if(CommandWords.isEnemy(words.get(i))){
|
||||
enemy = words.get(i);
|
||||
}else{
|
||||
otherWords.add(words.get(i));
|
||||
}
|
||||
@@ -68,11 +71,11 @@ class Parser {
|
||||
//System.out.println(verb);
|
||||
if (CommandWords.isCommand(verb))
|
||||
if(!open)
|
||||
return new Command(verb, otherWords, direction, item);
|
||||
return new Command(verb, otherWords, direction, item, enemy);
|
||||
else
|
||||
return new Command("open", otherWords, direction, item);
|
||||
return new Command("open", otherWords, direction, item, enemy);
|
||||
else
|
||||
return new Command(null, otherWords, direction, item);
|
||||
return new Command(null, otherWords, direction, item, enemy);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -158,14 +158,14 @@ class Room {
|
||||
*/
|
||||
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
|
||||
* west ".
|
||||
*/
|
||||
private String exitString() {
|
||||
public String exitString() {
|
||||
String returnString = "Exits:";
|
||||
Set keys = exits.keySet();
|
||||
for (Iterator iter = keys.iterator(); iter.hasNext();)
|
||||
|
||||
Reference in New Issue
Block a user