-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamehandler.cc
153 lines (115 loc) · 3.95 KB
/
gamehandler.cc
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
#include "gamehandler.h"
#include "ui.h"
#include "gamemap.h"
#include "pc.h"
#include "human.h"
#include "elf.h"
#include "dwarf.h"
#include "orc.h"
#include <utility>
using namespace std;
GameHandler::GameHandler(GameMap &g, UI &ui) : g{g}, ui{ui} {}
void GameHandler::play(string file) {
ui.output_message("Enter the character you want to play with\n");
ui.output_message("Human (h)\n");
ui.output_message("Elf (e)\n");
ui.output_message("Dwarf (d)\n");
ui.output_message("Orc (o)\n");
CommandType c = ui.getNextInput();
bool default_map; // is true if the map is the defualt one(empty)
if(c == CommandType::e)
hero = shared_ptr <PC> (new Elf());
else if(c == CommandType::d)
hero = shared_ptr <PC> (new Dwarf());
else if(c == CommandType::o)
hero = shared_ptr <PC> (new Orc());
else
hero = shared_ptr <PC> (new Human());
vector <vector <CellType>> layout;
map <pair <int, int>, shared_ptr<Sprite>> sprite_coords;
ui.read_file(file);
default_map = ui.read_floor(layout, sprite_coords);
// Sets up the grid
g.setUpMap(layout);
g.initialize(hero);
if(default_map)
g.populate();
else
g.populate(sprite_coords);
string action = "Player character has spawned";
bool restart = false;
while(true) {
ui.output(g.getGrid());
unsigned int floor_count = g.getFloorCount();
ui.output_message("Race: " + ui.convert(hero->getType()) + " Gold: ");
ui.output_number(hero->getGold());
for(int i = 0; i < 50; i++)
ui.output_message(" ");
ui.output_message("Floor ");
ui.output_number(g.getFloorCount());
ui.newline();
ui.output_message("HP: ");
ui.output_number(hero->getHP());
ui.newline();
ui.output_message("Atk: ");
ui.output_number(hero->getAtk());
ui.newline();
ui.output_message("Def: ");
ui.output_number(hero->getDef());
ui.newline();
ui.output_message("Action: " + action + "\n\n");
if(hero->getHP() <= 0) {
// delete hero;
ui.output_message("Player was killed. You Lost!\n");
break;
}
ui.display_commands();
pair <CommandType, CommandType> command_pair;
// input1 : u/a, input2 : direction
CommandType input1 = CommandType::no_value;
CommandType input2 = ui.getNextInput();
if(input2 == CommandType::a || input2 == CommandType::u) {
input1 = input2;
input2 = ui.getNextInput();
}
else if(input2 == CommandType::r) {
ui.output_message("Restarting the game\n");
restart = true;
break;
}
else if(input2 == CommandType::q) {
ui.output_message("Thanks for playing the game\n");
break;
}
string direction;
if(input1 == CommandType::no_value) {
direction = ui.convert_direction(input2);
}
action = g.nextTurn(make_pair(input1, input2));
ui.put_direction(action, direction);
if(g.getFloorCount() > 5) {
ui.output_message("You Won! Congratulations!\n");
break;
}
if(g.getFloorCount() != floor_count) {
sprite_coords.clear();
if(!default_map) {
layout.clear();
default_map = ui.read_floor(layout, sprite_coords);
}
g.setUpMap(layout);
if(default_map)
g.populate();
else
g.populate(sprite_coords);
}
}
if(restart) {
play(file);
}
else {
ui.output_message("Do you want to play again? (y/n)\n");
if(ui.getNextInput() == CommandType::y)
play(file);
}
}