1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include "DialogueTree.h"
DialogueNode::DialogueNode(string Text) {
text = Text;
}
DialogueOption::DialogueOption(string Text, int ReturnCode, DialogueNode *NextNode) {
text = Text;
returnCode = ReturnCode;
nextNode = NextNode;
}
DialogueTree::DialogueTree() {
}
void DialogueTree::init() {
DialogueNode *node0 = new DialogueNode("Hello brave warrior!");
DialogueNode *node1 = new DialogueNode("I don't want to talk to you");
DialogueNode *node2 = new DialogueNode("I have a quest for you!");
DialogueNode *node3 = new DialogueNode("You will get 5 gold you swine.");
DialogueNode *node4 = new DialogueNode("Collect ten dandilions.");
node0->dialogueOptions.push_back(DialogueOption("Sup noob!", 0, node1));
node0->dialogueOptions.push_back(DialogueOption("Hello strange voice!", 0, node2));
dialogueNodes.push_back(node0);
node1->dialogueOptions.push_back(DialogueOption("Aww.", 0, nullptr));
dialogueNodes.push_back(node1);
node2->dialogueOptions.push_back(DialogueOption("K bye.", 0, nullptr));
node2->dialogueOptions.push_back(DialogueOption("What is it?", 0, node4));
node2->dialogueOptions.push_back(DialogueOption("What's the pay?.", 0, node3));
dialogueNodes.push_back(node2);
node3->dialogueOptions.push_back(DialogueOption("Ok what is it?", 0, node4));
node3->dialogueOptions.push_back(DialogueOption("That sucks I'm out.", 0, nullptr));
dialogueNodes.push_back(node3);
node4->dialogueOptions.push_back(DialogueOption("Let's do it!", 1, nullptr));
node4->dialogueOptions.push_back(DialogueOption("No way.", 0, nullptr));
dialogueNodes.push_back(node4);
}
void DialogueTree::destroyTree() {
for (int i = 0; i < dialogueNodes.size(); i++) {
delete dialogueNodes[i];
}
dialogueNodes.clear();
}
int DialogueTree::performDialogue() {
if (dialogueNodes.empty) {
return -1;
}
DialogueNode *currentNode = dialogueNodes[0];
while (true) {
cout << currentNode->text << endl << endl;
for (int i = 0; i < currentNode->dialogueOptions.size(); i++) {
cout << i + 1 << ": " << currentNode->dialogueOptions[i].text << endl;
}
cout << endl;
int input;
cin >> input;
input--;
if (input < 0 || input >= currentNode->dialogueOptions.size()) {
cout << "Invalid input." << endl;
}
else {
if (currentNode->dialogueOptions[input].nextNode == nullptr) {
return currentNode->dialogueOptions[input].returnCode;
}
currentNode = currentNode->dialogueOptions[input].nextNode;
}
cout << endl;
}
return 0;
}
|