forked from 15-466/15-466-f20-base4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MazeMode.cpp
531 lines (467 loc) · 13 KB
/
MazeMode.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "MazeMode.hpp"
#include "HomeMode.hpp"
#include "EventLog.hpp"
#include "Player.hpp"
#include "data_path.hpp"
#include <stack>
#include <random>
#include <set>
#include <sstream>
std::shared_ptr<MazeMode> mazemode;
static constexpr float kPlayerDrawableRadius{0.02f};
static constexpr int kCircleVertexCount{20};
static constexpr glm::u8vec4 kPlayerColor{0xbb, 0xb0, 0xe8, 0xff};
Load< Sound::Sample > treasure_sfx_sample(LoadTagDefault, []() -> Sound::Sample const * {
return new Sound::Sample(data_path("coin.wav"));
});
Load< Sound::Sample > fight_sfx_sample(LoadTagDefault, []() -> Sound::Sample const * {
return new Sound::Sample(data_path("fight.wav"));
});
MazeMode::MazeMode()
{
texture2d_program->SetBox(maze_background_drawable_,
glm::vec4(kMazeStartPos[0] - Room::kWallSize,
kMazeStartPos[1] + Room::kWallSize,
kMazeStartPos[0] + kMazeWidth * Room::kRoomSize,
kMazeStartPos[1] - kMazeHeight * Room::kRoomSize),
Room::kWallColor);
}
MazeMode::~MazeMode()
{
player_pos_drawable_.Clear();
ClearRooms();
}
bool MazeMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size)
{
if (evt.type == SDL_KEYDOWN)
{
Room ¤t_room = map_[position_.x][position_.y];
if (evt.key.keysym.sym == SDLK_w)
{
switch (current_room.type_)
{
case Room::Type::EXIT:
case Room::Type::NORMAL:
case Room::Type::TREASURE:
EnterRoom(0);
break;
case Room::Type::MONSTER:
dynamic_cast<MenuDialog *>(dialog_system->GetDialog("maze_monster"))->PreviousChoice();
break;
default:;
}
return true;
}
else if (evt.key.keysym.sym == SDLK_d)
{
if (current_room.type_ != Room::Type::MONSTER)
{
EnterRoom(1);
}
}
else if (evt.key.keysym.sym == SDLK_a)
{
if (current_room.type_ != Room::Type::MONSTER)
{
EnterRoom(3);
}
}
else if (evt.key.keysym.sym == SDLK_s)
{
switch (current_room.type_)
{
case Room::Type::EXIT:
case Room::Type::NORMAL:
case Room::Type::TREASURE:
EnterRoom(2);
break;
case Room::Type::MONSTER:
dynamic_cast<MenuDialog *>(dialog_system->GetDialog("maze_monster"))->NextChoice();
break;
default:;
}
return true;
}
else if (evt.key.keysym.sym == SDLK_RETURN)
{
switch (current_room.type_)
{
case Room::Type::EXIT:
{
Sound::play(*sound_clonk);
static int adventure_time = 0;
++adventure_time;
EventLog::Instance().LogEvent("Adventure succeeded!");
Player::Instance().GainMoney(adventure_time * 100);
Player::Instance().GainExperience(adventure_time * 50);
Exit();
break;
}
case Room::Type::MONSTER:
{
int current_choice = dynamic_cast<MenuDialog *>(dialog_system->GetDialog("maze_monster"))->GetCurrentChoice();
FightMonster(current_choice);
break;
}
case Room::Type::TREASURE:
PickupTreasure();
break;
default:;
}
return true;
}
}
return false;
}
void MazeMode::update(float elapsed)
{
auto get_screen_pos = [&](const glm::uvec2 &pos) {
return glm::vec2(kMazeStartPos[0] + (pos[1] + 0.5f) * Room::kRoomSize, kMazeStartPos[1] - (pos[0] + 0.5f) * Room::kRoomSize);
};
texture2d_program->SetCircle(player_pos_drawable_, get_screen_pos(position_), kPlayerDrawableRadius, kCircleVertexCount, kPlayerColor);
}
void MazeMode::draw(glm::uvec2 const &drawable_size)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.1f, 0.1f, 0.5f, 1.0f);
glClearDepth(1.0f); //1.0 is actually the default value to clear the depth buffer to, but FYI you can change it.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); //this is the default depth comparison function, but FYI you can change it.
{ //use DrawLines to overlay some text:
glDisable(GL_DEPTH_TEST);
// Draw maze background
texture2d_program->DrawBox(maze_background_drawable_);
// Draw maze
for (auto &row : map_)
{
for (auto &room : row)
{
room.Draw();
}
}
// Draw player position
texture2d_program->DrawCircle(player_pos_drawable_);
// Draw dialogs
dialog_system->Draw(drawable_size);
// Draw player info
Player::Instance().DrawInfo(drawable_size);
// Draw monster info
if (monster_) {
monster_->info_dialog_.Draw(drawable_size);
}
// Draw event info
EventLog::Instance().Draw(drawable_size);
}
}
void MazeMode::Initialize()
{
GenerateMaze();
position_ = glm::uvec2(0, 0);
}
void MazeMode::ClearRooms()
{
for (auto &row : map_)
{
for (auto &room : row)
{
room.Clear();
}
}
}
void MazeMode::GenerateMaze()
{
ClearRooms();
glm::uvec2 current_pos;
std::stack<glm::uvec2> fringe;
fringe.emplace(0, 0);
// std::vector<std::vector<bool>> visited(kMazeHeight, std::vector<bool>(kMazeWidth, false));
std::random_device rd;
std::mt19937 mt{rd()};
while (!fringe.empty())
{
current_pos = fringe.top();
auto ¤t_room = map_[current_pos.x][current_pos.y];
current_room.flag_ |= Room::Flag::VISITED;
std::vector<int> choices;
for (int i = 0; i < 4; ++i)
{
const glm::uvec2 &dir = kDirection[i];
glm::uvec2 new_pos = current_pos + dir;
if (new_pos.x < kMazeHeight && new_pos.x >= 0 && new_pos.y < kMazeWidth && new_pos.y >= 0)
{
if (!(map_[new_pos.x][new_pos.y].flag_ & Room::Flag::VISITED))
{
choices.push_back(i);
}
}
}
int choice_num = static_cast<int>(choices.size());
if (choice_num > 0)
{
int choice = choices[std::uniform_int_distribution<int>(0, choice_num - 1)(mt)];
glm::uvec2 new_pos = current_pos + kDirection[choice];
auto &new_room = map_[new_pos.x][new_pos.y];
current_room.flag_ |= Room::kDirectionToFlag[choice];
new_room.flag_ |= Room::kDirectionToFlag[(choice + 2) % 4];
fringe.push(new_pos);
}
else
{
fringe.pop();
}
}
// Select treasure room, monster room, etc...
std::uniform_int_distribution<int> special_room_dist(1, kMazeHeight * kMazeWidth - 1);
std::set<int> room_set;
auto set_room_type = [&](int room_num, Room::Type type) {
map_[room_num / kMazeWidth][room_num % kMazeWidth].type_ = type;
};
auto generate_room = [&](Room::Type type) {
while (true)
{
int room_num = special_room_dist(mt);
if (room_set.count(room_num) == 0)
{
room_set.insert(room_num);
set_room_type(room_num, type);
break;
}
}
};
for (int i = 0; i < kMonsterNum; ++i)
{
generate_room(Room::Type::MONSTER);
}
generate_room(Room::Type::EXIT);
generate_room(Room::Type::TREASURE);
for (int i = 0; i < kMazeHeight; ++i)
{
for (int j = 0; j < kMazeWidth; ++j)
{
Room &room = map_[i][j];
UpdateRoomColor(glm::uvec2(i, j));
if (!(room.flag_ & Room::Flag::CONNECT_RIGHT))
{
texture2d_program->SetBox(room.wall_drawables_[0],
glm::vec4(
kMazeStartPos[0] + (j + 1) * Room::kRoomSize - Room::kWallSize,
kMazeStartPos[1] - i * Room::kRoomSize,
kMazeStartPos[0] + (j + 1) * Room::kRoomSize,
kMazeStartPos[1] - (i + 1) * Room::kRoomSize),
Room::kWallColor);
}
if (!(room.flag_ & Room::Flag::CONNECT_DOWN))
{
texture2d_program->SetBox(room.wall_drawables_[1],
glm::vec4(
kMazeStartPos[0] + j * Room::kRoomSize,
kMazeStartPos[1] - ((i + 1) * Room::kRoomSize - Room::kWallSize),
kMazeStartPos[0] + (j + 1) * Room::kRoomSize,
kMazeStartPos[1] - (i + 1) * Room::kRoomSize),
Room::kWallColor);
}
}
}
}
void MazeMode::Room::Draw()
{
texture2d_program->DrawBox(room_drawable_);
for (auto &wall_drawable : wall_drawables_)
{
texture2d_program->DrawBox(wall_drawable);
}
}
void MazeMode::Room::Clear()
{
type_ = Type::NORMAL;
flag_ = 0;
room_drawable_.Clear();
for (auto &wall_drawable : wall_drawables_)
{
wall_drawable.Clear();
}
}
void MazeMode::EnterRoom(int direction)
{
glm::uvec2 new_pos = position_ + kDirection[direction];
if (new_pos.x < kMazeHeight && new_pos.x >= 0 && new_pos.y < kMazeWidth && new_pos.y >= 0)
{
if (map_[position_.x][position_.y].flag_ & (Room::Flag::CONNECT_UP << direction))
{
position_ = new_pos;
// Exit room clean up
dialog_system->CloseAllDialogs();
// Enter room
switch (map_[new_pos.x][new_pos.y].type_)
{
case Room::Type::EXIT:
dialog_system->ShowDialogs(std::vector<std::string>{"maze_exit"});
break;
case Room::Type::NORMAL:
break;
case Room::Type::TREASURE:
dialog_system->ShowDialogs(std::vector<std::string>{"maze_treasure"});
break;
case Room::Type::MONSTER:
dialog_system->ShowDialogs(std::vector<std::string>{"maze_monster"});
monster_ = new Monster(Player::Instance().GetLevel());
break;
default:;
}
}
}
}
void MazeMode::FightMonster(int choice)
{
std::random_device rd;
std::mt19937 mt{rd()};
auto fight_succeed = [&]() {
int i = position_.x;
int j = position_.y;
Room ¤t_room = map_[i][j];
current_room.type_ = Room::Type::NORMAL;
dialog_system->CloseAllDialogs();
UpdateRoomColor(position_);
delete monster_;
monster_ = nullptr;
};
enum Choice
{
ATTACK = 0,
ESCAPE
};
switch (choice)
{
case ATTACK: {
Sound::play(*fight_sfx_sample);
int damage = Player::Instance().Attack(monster_->defense_);
if (monster_->ApplyDamage(damage)) {
int money = static_cast<int>((std::uniform_real_distribution<float>(-0.2f, 0.2f)(mt) + monster_->level_) * 50);
int exp = static_cast<int>((std::uniform_real_distribution<float>(-0.2f, 0.2f)(mt) + monster_->level_) * 50);
Player::Instance().GainMoney(money);
Player::Instance().GainExperience(exp);
fight_succeed();
return;
}
break;
}
case ESCAPE:
if (std::uniform_int_distribution<int>(1, 10)(mt) <= 3)
{
EventLog::Instance().LogEvent("Escape succeed!");
fight_succeed();
return;
}
EventLog::Instance().LogEvent("Escape failed!");
break;
default:
throw std::runtime_error("Unknown fight monster option!");
}
// Monster attack you
if (Player::Instance().ApplyDamage(monster_->attack_))
{
delete monster_;
monster_ = nullptr;
Exit();
}
}
void MazeMode::UpdateRoomColor(const glm::uvec2 &pos)
{
int i = pos.x;
int j = pos.y;
Room ¤t_room = map_[i][j];
texture2d_program->SetBox(current_room.room_drawable_,
glm::vec4(
kMazeStartPos[0] + j * Room::kRoomSize,
kMazeStartPos[1] - i * Room::kRoomSize,
kMazeStartPos[0] + (j + 1) * Room::kRoomSize,
kMazeStartPos[1] - (i + 1) * Room::kRoomSize),
Room::kRoomColors[static_cast<int>(current_room.type_)]);
}
void MazeMode::Exit()
{
dialog_system->CloseAllDialogs();
homemode->Initialize();
Mode::set_current(homemode);
}
void MazeMode::PickupTreasure()
{
std::random_device rd;
std::mt19937 mt{rd()};
map_[position_.x][position_.y].type_ = Room::Type::NORMAL;
UpdateRoomColor(position_);
dialog_system->CloseAllDialogs();
Sound::play(*treasure_sfx_sample);
int treasure = std::uniform_int_distribution<int>(0, 1)(mt);
enum Treasure : uint8_t
{
MONEY = 0,
EXP
};
int level = Player::Instance().GetLevel();
switch (treasure)
{
case MONEY:
{
EventLog::Instance().LogEvent("Found money!");
int money = (std::uniform_int_distribution<int>(50, 500)(mt) * level) / 2;
Player::Instance().GainMoney(money);
break;
}
case EXP:
{
EventLog::Instance().LogEvent("Found exp!");
int exp = (std::uniform_int_distribution<int>(50, 500)(mt) * level) / 2;
Player::Instance().GainExperience(exp);
break;
}
default:;
}
}
MazeMode::Monster::Monster(int player_level) :
info_dialog_(data_path("ariblk.ttf").c_str(), kFontColors.at("black"), glm::vec4(-0.9f, 0.8f, -0.7f, 0.5f), kFontSize, kFontColors.at("white"))
{
info_dialog_.SetVisibility(true);
std::ostringstream oss;
glm::vec2 anchor { -0.89f, 0.75f };
info_dialog_.AddText("Monster", anchor);
std::random_device rd;
std::mt19937 mt { rd() };
level_ = std::max(1, std::uniform_int_distribution<int>(-3, 3)(mt) + player_level);
anchor[1] -= kFontLineSpace;
oss.str("");
oss << "Level: " << level_;
info_dialog_.AddText(oss.str().c_str(), anchor);
hp_ = std::max(1, static_cast<int>((std::uniform_real_distribution<float>(-2.0f, 2.0f)(mt) + 10.0f) * level_));
anchor[1] -= kFontLineSpace;
oss.str("");
oss << "HP: " << hp_;
info_dialog_.AddText(oss.str().c_str(), anchor);
attack_ = std::max(1, 15 + level_ * 3 + std::uniform_int_distribution<int>(-level_ - 5, level_ + 5)(mt));
anchor[1] -= kFontLineSpace;
oss.str("");
oss << "Attack: " << attack_;
info_dialog_.AddText(oss.str().c_str(), anchor);
defense_ = std::max(1, 10 + level_ + std::uniform_int_distribution<int>(-level_ - 5, level_ + 5)(mt));
anchor[1] -= kFontLineSpace;
oss.str("");
oss << "Defense: " << defense_;
info_dialog_.AddText(oss.str().c_str(), anchor);
}
bool MazeMode::Monster::ApplyDamage(int damage)
{
std::ostringstream oss;
oss << "Monster received " << damage << " damage!";
EventLog::Instance().LogEvent(oss.str());
hp_ -= damage;
if (hp_ <= 0) {
info_dialog_.SetVisibility(false);
return true;
}
oss.str("");
oss << "HP: " << hp_;
info_dialog_.GetText(2)->SetText(oss.str().c_str(), kFontSize);
return false;
}