-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
100 lines (94 loc) · 2.59 KB
/
main.cpp
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
93
94
95
96
97
98
99
100
/*
CPPXMLRPG
XML-powered text adventure game engine written in C++
by Josh Maines.
Default story by Josh Maines.
Upcoming features:
- Ability to add stories to a main menu!
- Graphical menu!
- full-screen GUI!
Required libraries:
- TinyXML2 for XML loading and parsing. (zlib license)
*/
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <tinyxml2.h>
#include <dialogue.h>
using namespace std;
using namespace tinyxml2;
using namespace dialogue;
int playerHP,playerMaxHP;
int timesPlayed = 0; //count how many times the player has replayed.
string playerName,replayYN;
bool game_running = true;
XMLDocument doc;
XMLElement* storyContainer;
XMLElement* storyChild;
std::string operator+(std::string const &a, int b){
std::ostringstream oss;
oss<<a<<b;
return oss.str();
}
// pauseForKeyPress method is used whenever we want to give the player a chance to read some text.
void pauseForKeyPress() {
cout << "Enter any key to continue...\n";
int tmp;
cin >> tmp;
}
void waitForPlayerInput() {
getline (cin,replayYN);
}
void playerStatus() {
if (timesPlayed > 0) {
cout << "Times replayed this session: " << timesPlayed << "\n";
}
cout << "Player: " << playerName << "\n" << "\tPlayerHP: " << playerHP << "/" << playerMaxHP << "\nwww.getlives.com\n";
}
void resetGame() {
if (doc.LoadFile( "adventures/default.xml" ) != tinyxml2::XML_SUCCESS) {
cout << "Cannot read XML file.";
pauseForKeyPress();
exit(0);
}
playerMaxHP = 10;
playerHP = playerMaxHP;
cout << "\n";
cout << "Insert your name and press enter: ";
getline (cin,playerName);
playerStatus();
cout << "You are " << playerName << ", ";
storyContainer = doc.FirstChildElement("story");
storyChild = storyContainer->FirstChildElement();
}
void gameover() {
replayYN = "";
while (replayYN != "y" && replayYN != "Y" && replayYN != "Yes" && replayYN != "yes" && replayYN != "n" && replayYN != "N" && replayYN != "no" && replayYN != "No") {
cout << "Game over! Play again? y/n?";
getline (cin,replayYN);
}
if (replayYN != "y" && replayYN != "Y" && replayYN != "Yes" && replayYN != "yes") {
game_running = false;
exit(0);
} else {
timesPlayed++;
resetGame();
}
}
void hurt(int dmg) {
playerHP -= dmg;
if (playerHP < 1) {
gameover();
}
}
void say(string storyText) {
cout << storyText << "\n";
waitForPlayerInput();
}
int main() {
resetGame();
dialogue.DialogueTree dialogTree;
dialogTree.init();
return 0;
}