[ebd3538] | 1 | package main;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import utils.Utils;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.awt.FontMetrics;
|
---|
| 8 | import java.awt.Point;
|
---|
| 9 |
|
---|
| 10 | public class NPC extends Creature
|
---|
| 11 | {
|
---|
| 12 | public Dialog dialog;
|
---|
| 13 | int state;
|
---|
| 14 |
|
---|
| 15 | public NPC(final String name, final int xOffset, final int yOffset) {
|
---|
| 16 | super(name, xOffset, yOffset);
|
---|
| 17 | final boolean b = true;
|
---|
| 18 | this.hitpoints = (b ? 1 : 0);
|
---|
| 19 | this.maxHitpoints = (b ? 1 : 0);
|
---|
| 20 | this.dialog = null;
|
---|
| 21 | this.state = 0;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public NPC(final NPC cr) {
|
---|
| 25 | super(cr);
|
---|
| 26 | this.dialog = cr.dialog;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | @Override
|
---|
| 30 | public NPC copy(final Point newLoc) {
|
---|
| 31 | final NPC newCr = new NPC(this);
|
---|
| 32 | newCr.initLoc(newLoc);
|
---|
| 33 | return newCr;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public void loadDialog(final String fileName, final FontMetrics metrics, final int width) {
|
---|
| 37 | final ArrayList<Dialog> dialogs = new ArrayList<Dialog>();
|
---|
| 38 | final HashMap<Integer, Dialog> dialogReferences = new HashMap<Integer, Dialog>();
|
---|
| 39 | final ArrayList<ArrayList<Integer>> referencesList = new ArrayList<ArrayList<Integer>>();
|
---|
| 40 | try {
|
---|
| 41 | final BufferedReader in = Utils.loadTextFile("dialog/" + fileName);
|
---|
| 42 | String curLine = in.readLine();
|
---|
| 43 | while (in.ready()) {
|
---|
| 44 | final int num = Integer.parseInt(curLine.substring(1));
|
---|
| 45 | final Dialog curDialog = new Dialog(in.readLine(), metrics, width);
|
---|
| 46 | final ArrayList<Integer> curReferences = new ArrayList<Integer>();
|
---|
| 47 | dialogReferences.put(num, curDialog);
|
---|
| 48 | in.readLine();
|
---|
| 49 | curLine = in.readLine();
|
---|
| 50 | do {
|
---|
| 51 | if (curLine.equals("[NULL]")) {
|
---|
| 52 | curReferences.add(null);
|
---|
| 53 | }
|
---|
| 54 | else {
|
---|
| 55 | final int ref = Integer.parseInt(curLine.substring(1, curLine.indexOf("]")));
|
---|
| 56 | curReferences.add(ref);
|
---|
| 57 | }
|
---|
| 58 | curLine = in.readLine();
|
---|
| 59 | if (curLine.substring(0, 1).equals("%")) {
|
---|
| 60 | curDialog.addTrigger(curDialog.getNumOptions(), Integer.parseInt(curLine.substring(1)));
|
---|
| 61 | curDialog.addOption(in.readLine(), null);
|
---|
| 62 | }
|
---|
| 63 | else {
|
---|
| 64 | curDialog.addOption(curLine, null);
|
---|
| 65 | }
|
---|
| 66 | if (!in.ready()) {
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | in.readLine();
|
---|
| 70 | curLine = in.readLine();
|
---|
| 71 | } while (!curLine.substring(0, 1).equals("#"));
|
---|
| 72 | dialogs.add(curDialog);
|
---|
| 73 | referencesList.add(curReferences);
|
---|
| 74 | }
|
---|
| 75 | for (int numDialogs = dialogs.size(), x = 0; x < numDialogs; ++x) {
|
---|
| 76 | for (int numOptions = dialogs.get(x).getNumOptions(), y = 0; y < numOptions; ++y) {
|
---|
| 77 | if (referencesList.get(x).get(y) != null) {
|
---|
| 78 | dialogs.get(x).changeReference(y, dialogReferences.get(referencesList.get(x).get(y)));
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | this.dialog = dialogs.get(0);
|
---|
| 83 | in.close();
|
---|
| 84 | }
|
---|
| 85 | catch (Exception e) {
|
---|
| 86 | e.printStackTrace();
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public Dialog processDialog(final Dialog curDialog, final int selection, final Player player) {
|
---|
| 91 | if (curDialog.getTrigger(selection) != null) {
|
---|
| 92 | if (this.name.equals("Seer")) {
|
---|
| 93 | this.handleTriggers1(curDialog.getTrigger(selection), player);
|
---|
| 94 | }
|
---|
| 95 | if (this.name.equals("Wayfarer")) {
|
---|
| 96 | this.handleTriggers2(curDialog.getTrigger(selection), player);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | return curDialog.getReference(selection);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | private void handleTriggers1(final int triggerNum, final Player player) {
|
---|
| 103 | if (this.state == 0) {
|
---|
| 104 | switch (triggerNum) {
|
---|
| 105 | case 1: {
|
---|
| 106 | this.loadDialog("healDialog.txt", this.dialog.metrics, this.dialog.width);
|
---|
| 107 | this.state = 1;
|
---|
| 108 | break;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | else if (this.state == 1) {
|
---|
| 113 | switch (triggerNum) {
|
---|
| 114 | case 1: {
|
---|
| 115 | player.hitpoints = player.maxHitpoints;
|
---|
| 116 | break;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private void handleTriggers2(final int triggerNum, final Player player) {
|
---|
| 123 | if (this.state == 0) {
|
---|
| 124 | switch (triggerNum) {
|
---|
| 125 | case 1: {
|
---|
| 126 | player.pickUp(LostHavenRPG.itemMap.get("Long Sword"));
|
---|
| 127 | player.pickUp(LostHavenRPG.itemMap.get("Cloth Boots"));
|
---|
| 128 | this.state = 1;
|
---|
| 129 | break;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|