108 lines
3.5 KiB
Java
108 lines
3.5 KiB
Java
package ruscz.game.gameOfProd.Scripts;
|
|
|
|
import ruscz.game.gameOfProd.Battle.Battle;
|
|
import ruscz.game.gameOfProd.Battle.BattlePerson;
|
|
import ruscz.game.gameOfProd.Inventory.InventoryManager;
|
|
import ruscz.game.gameOfProd.Inventory.Item;
|
|
import ruscz.game.gameOfProd.Inventory.ItemAction;
|
|
|
|
/**
|
|
* This class parses text form player into a set of commands
|
|
* This class is currently not being used in our game, though it will be implemented at a later date
|
|
*/
|
|
public class TextParser {
|
|
private String textToParse;
|
|
private Battle battle;
|
|
private BattlePerson person;
|
|
private BattlePerson target;
|
|
private Item item;
|
|
private String action;
|
|
|
|
/**
|
|
* @param text is input from player
|
|
* Separates incoming text into parts and passes them on to be parsed further
|
|
*/
|
|
public void parseBattleText(String text) {
|
|
textToParse = text;
|
|
String person = textToParse.toLowerCase().substring(0, textToParse.indexOf("."));
|
|
parseBattlePerson(person);
|
|
String target = textToParse.toLowerCase().substring(textToParse.indexOf("(") + 1, textToParse.indexOf(")"));
|
|
parseBattleTarget(target);
|
|
String action = textToParse.toLowerCase().substring(textToParse.indexOf(".") + 1, textToParse.indexOf("("));
|
|
parseBattleAction(action);
|
|
}
|
|
|
|
/**
|
|
* @param personText is name of battle person
|
|
* Finds battle person based on text input and sets it as actor of action
|
|
*/
|
|
private void parseBattlePerson(String personText) {
|
|
switch (personText) {
|
|
case "self":
|
|
case "hero":
|
|
case "me":
|
|
person = battle.getHeroes().getFirst();
|
|
break;
|
|
case "enemy":
|
|
case "monster":
|
|
person = battle.getEnemies().getFirst();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param action is name of action
|
|
* Calls corresponding action to user input
|
|
*/
|
|
private void parseBattleAction(String action) {
|
|
this.action = action;
|
|
switch (action) {
|
|
case "attack":
|
|
person.attack(target);
|
|
case "use":
|
|
ItemAction itemAction = item.getItemAction();
|
|
switch (itemAction) {
|
|
case HEAL:
|
|
person.heal(item.getMultiplicity());
|
|
break;
|
|
case DEFENCE:
|
|
person.addDefence(item.getMultiplicity());
|
|
break;
|
|
case STRENGTH:
|
|
person.addStrength(item.getMultiplicity());
|
|
break;
|
|
case MANA:
|
|
person.addMana(item.getMultiplicity());
|
|
break;
|
|
}
|
|
break;
|
|
case "block":
|
|
case "defend":
|
|
person.block();
|
|
break;
|
|
case "run":
|
|
case "runaway":
|
|
person.runAway();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param targetText name of battle person
|
|
* Finds battle person based on text input and sets it as target of action
|
|
*/
|
|
private void parseBattleTarget(String targetText) {
|
|
switch (targetText) {
|
|
case "self":
|
|
case "hero":
|
|
case "me":
|
|
target = battle.getHeroes().getFirst();
|
|
break;
|
|
case "enemy":
|
|
target = battle.getEnemies().getFirst();
|
|
break;
|
|
default:
|
|
item = InventoryManager.findItemByName(targetText);
|
|
}
|
|
}
|
|
}
|