-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsystem.cpp
More file actions
328 lines (271 loc) · 11.8 KB
/
Copy pathsystem.cpp
File metadata and controls
328 lines (271 loc) · 11.8 KB
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
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-err58-cpp"
#pragma ide diagnostic ignored "hicpp-named-parameter"
#include <menu/system.hpp>
#include <core/english.hpp>
#include <doomkeys.hpp>
#include <grfx/system.hpp>
#include <menu/draw_text.hpp>
#include <stdx/overload.hpp>
#include <numeric>
namespace menu
{
namespace
{
using namespace ::std::string_view_literals;
constexpr auto line_height = 16;
constexpr auto skull_x_offset = -32;
constexpr auto skull_names = std::array{"M_SKULL1"sv, "M_SKULL2"sv};
int string_width(grfx::system& gfx, const std::string_view text)
{
return std::accumulate(text.begin(), text.end(), 0, [&](const int acc, const char chr) {
const auto c = core::index_in_hud_font(chr);
return acc + (core::is_in_hud_font(c) ? gfx.hud_font_patch(c).width : 4);
});
}
void placeholder(system&, int) {}
void read_this_1(system& sys, int);
void read_this_2(system& sys, int);
void finish_read_this(system& sys, int);
void choose_episode(system& sys, int);
void choose_skill(system& sys, int);
void new_game(system& sys, int);
void quit(system&, int key)
{
if (key != 'y') return;
// todo play sound
exit(0);
}
void quit_doom(system& sys, int)
{
const auto text = std::string(ENDGAME);
sys.message(text, quit, true);
}
void draw_main_menu(grfx::system& gfx, core::game_data& data)
{
gfx.draw_patch(94, 2, *core::cache_lump<grfx::patch_t>(data, "M_DOOM"));
}
void draw_help_page_1(grfx::system& gfx, core::game_data& data)
{
gfx.draw_patch(0, 0, *core::cache_lump<grfx::patch_t>(data, "HELP2"));
}
void draw_help_page_2(grfx::system& gfx, core::game_data& data)
{
gfx.draw_patch(0, 0, *core::cache_lump<grfx::patch_t>(data, "HELP1"));
}
void draw_choose_episode_page(grfx::system& gfx, core::game_data& data)
{
gfx.draw_patch(54, 38, *core::cache_lump<grfx::patch_t>(data, "M_EPISOD"));
}
auto main_page = menu_page{
.prev_menu = nullptr,
.items = {{.status = entry_status::ok, .name = "M_NGAME", .routine = new_game, .hot_key = 'n'},
{.status = entry_status::ok, .name = "M_OPTION", .routine = placeholder, .hot_key = 'o'},
{.status = entry_status::ok, .name = "M_LOADG", .routine = placeholder, .hot_key = 'l'},
{.status = entry_status::ok, .name = "M_SAVEG", .routine = placeholder, .hot_key = 's'},
{.status = entry_status::ok, .name = "M_RDTHIS", .routine = read_this_1, .hot_key = 'r'},
{.status = entry_status::ok, .name = "M_QUITG", .routine = quit_doom, .hot_key = 'q'}},
.draw = draw_main_menu,
.x = 97,
.y = 64,
.last_on = 0};
auto help_page_1 = menu_page{.prev_menu = &main_page,
.items = {{.status = entry_status::ok, .routine = read_this_2}},
.draw = draw_help_page_1,
.x = 280,
.y = 185,
.last_on = 0};
auto help_page_2 = menu_page{.prev_menu = &help_page_1,
.items = {{.status = entry_status::ok, .routine = finish_read_this}},
.draw = draw_help_page_2,
.x = 330,
.y = 175,
.last_on = 0};
auto choose_episode_page = menu_page{
.prev_menu = &main_page,
.items =
{
{.status = entry_status::ok, .name = "M_EPI1", .routine = choose_episode, .hot_key = 'k'},
{.status = entry_status::ok, .name = "M_EPI2", .routine = choose_episode, .hot_key = 't'},
{.status = entry_status::ok, .name = "M_EPI3", .routine = choose_episode, .hot_key = 'i'},
//{.status = entry_status::ok, .name = "M_EPI4", .routine = choose_episode, .hot_key = 't'},
},
.draw = draw_choose_episode_page,
.x = 48,
.y = 63,
.last_on = 0};
auto new_game_page =
menu_page{.prev_menu = &choose_episode_page,
.items =
{
{.status = entry_status::ok, .name = "M_JKILL", .routine = choose_skill, .hot_key = 'i'},
{.status = entry_status::ok, .name = "M_ROUGH", .routine = choose_skill, .hot_key = 'h'},
{.status = entry_status::ok, .name = "M_HURT", .routine = choose_skill, .hot_key = 'h'},
{.status = entry_status::ok, .name = "M_ULTRA", .routine = choose_skill, .hot_key = 'u'},
{.status = entry_status::ok, .name = "M_NMARE", .routine = choose_skill, .hot_key = 'n'},
},
.draw = draw_choose_episode_page,
.x = 48,
.y = 63,
.last_on = 2};
void read_this_1(system& sys, int) { sys.switch_to_page(&help_page_1); }
void read_this_2(system& sys, int) { sys.switch_to_page(&help_page_2); }
void finish_read_this(system& sys, int) { sys.switch_to_page(&main_page); }
void choose_episode(system& sys, int choice)
{
if ((sys.game_mode() == core::game_mode::shareware) and (choice != 0))
{
sys.message(SWSTRING, nullptr, false);
sys.switch_to_page(&help_page_1);
return;
}
if ((sys.game_mode() == core::game_mode::registered) and (choice > 2))
{
fmt::print(stderr, "4th episode requires UltimateDOOM\n");
choice = 0;
}
sys.set_episode(choice);
sys.switch_to_page(&new_game_page);
}
void verify_nightmare(system& sys, const int key)
{
if (key != 'y') return;
sys.new_game(new_game_page.items.size() - 1, 1);
}
void choose_skill(system& sys, int skill)
{
if (skill == new_game_page.items.size() - 1)
{
sys.message(NIGHTMARE, verify_nightmare, true);
return;
}
sys.new_game(skill, 2);
}
void new_game(system& sys, int choice)
{
if (sys.game_mode() == core::game_mode::commercial)
sys.switch_to_page(&new_game_page);
else
sys.switch_to_page(&choose_episode_page);
}
}
system::system(const core::iwad_description& iwad, game::system& game_sys)
: game_(game_sys), iwad_(iwad), current_page_(&main_page)
{
}
void system::draw(grfx::system& gfx, core::game_data& data) const
{
if (current_message_.has_value())
{
const auto lines = std::ranges::split_view(current_message_->text, '\n');
const auto line_height = gfx.hud_font_patch(core::hud_font_start).height;
const auto height = std::ranges::distance(lines) * line_height;
for (int y = grfx::original_screen_height / 2 - height / 2; const auto& line_chars : lines)
{
const auto line =
std::string_view(&(*std::ranges::begin(line_chars)), std::ranges::distance(line_chars));
const auto x = grfx::original_screen_width / 2 - string_width(gfx, line) / 2;
draw_text(gfx, x, y, line);
y += line_height;
}
return;
}
if (!is_active_) return;
current_page_->draw(gfx, data);
for (auto y = current_page_->y; const auto& item : current_page_->items)
{
if (!item.name.empty())
gfx.draw_patch(current_page_->x, y, *core::cache_lump<grfx::patch_t>(data, item.name));
y += line_height;
}
gfx.draw_patch(current_page_->x + skull_x_offset, current_page_->y - 5 + selected_item_index_ * line_height,
*core::cache_lump<grfx::patch_t>(data, skull_names[current_skull_]));
}
void system::message(const std::string_view text, void (*routine)(system&, int), bool is_input_required)
{
current_message_ =
message_description{.text = std::string(text), .routine = routine, .is_input_required = is_input_required};
}
void system::new_game(const int skill, const int map)
{
is_active_ = false;
game_.new_game({.skill = skill, .episode = episode_, .map = map});
}
bool system::handle_event(const core::event_t& e)
{
return std::visit(stdx::overload{[&](const core::key_down_event& e) { return handle_key_down_event(e); },
[&](const core::key_up_event& e) { return handle_key_up_event(e); },
[&](const core::quit_event& e) { return handle_quit_event(e); }},
e);
}
bool system::handle_key_down_event(const core::key_down_event& e)
{
if (current_message_)
{
if (current_message_->is_input_required)
{
const auto cancel_keys = std::array<char, 4>{' ', KEY_ESCAPE, 'y', 'n'};
if (std::ranges::find(cancel_keys, e.us_key_code) == cancel_keys.end()) { return false; }
}
if (current_message_->routine) current_message_->routine(*this, e.us_key_code);
current_message_.reset();
// todo play sound
return true;
}
if (!is_active_) return false;
if (e.us_key_code == KEY_DOWNARROW)
{
if (selected_item_index_ + 1 > std::ssize(current_page_->items) - 1)
selected_item_index_ = 0;
else
selected_item_index_++;
// todo play sound
return true;
}
if (e.us_key_code == KEY_UPARROW)
{
if (selected_item_index_ == 0)
selected_item_index_ = std::ssize(current_page_->items) - 1;
else
selected_item_index_--;
// todo play sound
return true;
}
if (e.us_key_code == KEY_ENTER)
{
if (current_page_->items[selected_item_index_].status == entry_status::ok)
{
current_page_->last_on = selected_item_index_;
if (current_page_->items[selected_item_index_].status == entry_status::arrows_ok)
{
current_page_->items[selected_item_index_].routine(*this, 1); // right arrow
// todo play sound
}
else
{
current_page_->items[selected_item_index_].routine(*this, selected_item_index_);
// todo play sound
}
}
return true;
}
return false;
}
bool system::handle_key_up_event(const core::key_up_event&) { return false; }
bool system::handle_quit_event(const core::quit_event&)
{
if (is_active_ && current_message_.has_value() && current_message_->routine == quit) { quit(*this, 'y'); }
else
{
// todo play sound
quit_doom(*this, 0);
}
return true;
}
void system::switch_to_page(menu_page* page)
{
current_page_ = page;
selected_item_index_ = current_page_->last_on;
}
}
#pragma clang diagnostic pop