-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
281 lines (246 loc) · 7.71 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
The MIT License (MIT)
Copyright (c) 2013 Daniel Mansfield
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <cstdlib>
#include <ctime>
#include "atlas.hpp"
#include "item.hpp"
#include "weapon.hpp"
#include "inventory.hpp"
#include "creature.hpp"
#include "dialogue.hpp"
#include "area.hpp"
#include "battle.hpp"
// New character menu
Creature dialogue_newchar();
// Character information menu, displays the items the player has, their
// current stats etc.
void dialogue_menu(Creature& player);
int main(void)
{
std::vector<Creature> creatureAtlas;
std::vector<Item> itemAtlas;
std::vector<Weapon> weaponAtlas;
std::vector<Area> areaAtlas;
Creature player;
// Build the atlases
buildatlas_creature(creatureAtlas);
buildatlas_item(itemAtlas);
buildatlas_weapon(weaponAtlas);
buildatlas_area(areaAtlas, itemAtlas, weaponAtlas, creatureAtlas);
// Seed the random number generator with the system time, so the
// random numbers produced by rand() will be different each time
srand(time(NULL));
// Main game menu dialogue
int result = Dialogue(
"Welcome!",
{"New Game"}).activate();
switch(result)
{
case 1: player = dialogue_newchar(); break;
default: return 0; break;
}
// Set the current area to be the first area in the atlas, essentially
// placing the player there upon game start
Area* currentArea = &(areaAtlas[0]);
// Play the game until a function breaks the loop and closes it
while(1)
{
// If the player has died then inform them as such and close
// the program
if(player.health <= 0)
{
std::cout << "\t----YOU DIED----\n Game Over\n";
return 0;
}
// If the area the player is in has any creatures inside it,
// then begin a battle with the last creature in the list
// before moving on the next one. This makes the creature
// list act like a stack
if(currentArea->creatures.size() > 0)
{
for(int i = currentArea->creatures.size() - 1; i >= 0; --i)
{
Battle(&player, currentArea->creatures[i]).run();
// Remove the creature from the area. This is fine to do
// because if the player wins the creature will not respawn,
// and if the creature wins the player isn't around to see it
// (This does break the 'non-mutable' feature of the atlases,
// but doing so saves a lot of memory, as we don't need to keep
// two versions of each area)
currentArea->creatures.pop_back();
}
}
// Activate the current area's dialogue
result = currentArea->dialogue.activate();
// These could be moved inside of the area code using an event
// style system, but that allows for much less flexibility with
// what happens in each area. Since we're defining the areas in
// code anyway, sticking with this isn't too much of a problem,
// and it keeps things easy to understand
if(currentArea == &(areaAtlas[0]))
{
switch(result)
{
// Open the menu
case 0:
dialogue_menu(player);
break;
case 1:
// Move to area 1
currentArea = &(areaAtlas[1]);
break;
case 2:
// Search the area
currentArea->search(player);
break;
default:
break;
}
}
else if(currentArea == &(areaAtlas[1]))
{
switch(result)
{
// Open the menu
case 0:
dialogue_menu(player);
break;
// Move to area 0
case 1:
currentArea = &(areaAtlas[0]);
break;
// Search the area
case 2:
currentArea->search(player);
break;
default:
break;
}
}
}
return 0;
}
// Create a new character
Creature dialogue_newchar()
{
// Ask for a name and class
// Name does not use a dialogue since dialogues only request options,
// not string input. Could be generalised into its own TextInput
// class, but not really necessary
std::cout << "Choose your name" << std::endl;
std::string name;
std::cin >> name;
int result = Dialogue(
"Choose your class",
{"Fighter", "Rogue"}).activate();
switch(result)
{
// Fighter class favours health and strength
case 1:
return Creature(name, 35, 20, 10, 5, 10.0, 1, "Fighter");
break;
// Rogue class favours dexterity and hit rate
case 2:
return Creature(name, 30, 5, 10, 20, 15.0, 1, "Fighter");
break;
// Default case that should never happen, but it's good to be safe
default:
return Creature(name, 30, 10, 10, 10, 10.0, 1, "Adventurer");
break;
}
}
void dialogue_menu(Creature& player)
{
// Output the menu
int result = Dialogue(
"Menu\n====",
{"Items", "Equipment", "Character"}).activate();
switch(result)
{
// Print the items that the player owns
case 1:
std::cout << "Items\n=====\n";
player.inventory.print();
std::cout << "----------------\n";
break;
// Print the equipment that the player is wearing (if they are
// wearing anything) and then ask if they want to equip a weapon
// or some armour
case 2:
{
std::cout << "Equipment\n=========\n";
std::cout << "Weapon: "
<< (player.equippedWeapon != nullptr ?
player.equippedWeapon->name : "Nothing")
<< std::endl;
int result2 = Dialogue(
"",
{"Equip Weapon", "Close"}).activate();
// Equip a weapon, using the same algorithms as for armour
if(result2 == 1)
{
int userInput = 0;
int numItems = player.inventory.print_weapons(true);
if(numItems == 0) break;
while(!userInput)
{
std::cout << "Equip which item?" << std::endl;
std::cin >> userInput;
if(userInput >= 1 && userInput <= numItems)
{
int i = 1;
for(auto it : player.inventory.weapons)
{
if(i++ == userInput)
{
player.equipWeapon(it.first);
break;
}
}
}
}
}
std::cout << "----------------\n";
break;
}
// Output the character information, including name, class (if
// they have one), stats, level, and experience
case 3:
std::cout << "Character\n=========\n";
std::cout << player.name;
if(player.className != "") std::cout << " the " << player.className;
std::cout << std::endl;
std::cout << "HP: " << player.health << " / " << player.maxHealth << std::endl;
std::cout << "Str: " << player.str << std::endl;
std::cout << "End: " << player.end << std::endl;
std::cout << "Dex: " << player.dex << std::endl;
std::cout << "Lvl: " << player.level << " (" << player.exp;
std::cout << " / " << player.expToLevel(player.level+1) << ")" << std::endl;
std::cout << "----------------\n";
break;
default:
break;
}
return;
}