-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQPvFE.cpp
More file actions
358 lines (285 loc) · 9.6 KB
/
QPvFE.cpp
File metadata and controls
358 lines (285 loc) · 9.6 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
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
//Includes From std lib
#include "UseFull/Utils/StdDiagnosticIgnore.hpp"
#include <corecrt.h>
#include <cstdlib>
#include <thread>
#include <chrono>
#include "UseFull/Utils/StdDiagnosticIgnoreEnd.hpp"
//Includes from UseFull
#include <UseFull/SFMLUp/GUI/Button.hpp>
#include <UseFull/Utils/Random.hpp>
#include <UseFull/Utils/Stream.hpp>
#include "UseFull/Utils/FlagParser.hpp"
//Includes From GameCore
#include "SourceCode/GameCore/GameCore.hpp"
//Other Game Includes
#include "SourceCode/Window.hpp"
#include "SourceCode/Game/Entity.hpp"
#include "SourceCode/Game/Player.hpp"
#include "SourceCode/Game/Block.hpp"
#include "SourceCode/Game/BlockTeleporter.hpp"
using namespace sfup;
using namespace sfup::gui;
using namespace uft;
//-lWs2_32 -liphlpapi
#define EPS 0.001
enum class MainLoopType {server, client, resume, init};
void initLevel1() {
Entity * en2 = nullptr;
en2 = new Entity(Codir<2>({0, 0}, {50, 100}));
en2->addDefault();
en2->moveRelative({500,-600});
Player * p1 = new Player(Codir<2>({150, 100}, {200, 200}));
p1->addDefault();
p1->moveRelative({350, 0});
p1->setImage(p1->texture_unfolded);
Block * block;
block = new Block(Codir<2>({100, 201}, {500, 300}));
block->addDefault();
block = new Block(Codir<2>({50, 800}, {15000, 900}));
block->addDefault();
BlockTeleporter * lift = new BlockTeleporter(Codir<2>({600, 800}, {700, 900}));
lift->target_object = p1;
lift->target = ClassType::Entity;
lift->teleportForm = BlockTeleporter::TeleportForm::absolute;
lift->teleportType = BlockTeleporter::TeleportType::center;
lift->delta = {700, -500};
lift->codir_teleport = Codir<2>(lift->codir.left_up + XY{0, -300}, lift->right_up);
lift->timeout_limit = 60;
lift->active = true;
lift->draw_teleport_zone = true;
lift->addRasBasic(OController.action_set);
block = new Block(Codir<2>({0, -500}, {50, 900}));
block->addDefault();
WorldView.followUp(p1->center);
}
struct GameState {
bool active = true;
bool pause = true;
XY current_view;
Window * main_window = nullptr;
};
void mainLoop(MainLoopType loopType, GameState & state) {
if (loopType == MainLoopType::init) initLevel1();
Entity * entity = nullptr;
sf::RenderWindow & window = GameWindow::window;
size_t & limit_fps = GameWindow::limit_fps;
state.active = true;
while (window.isOpen()) {
//EventKeeper processor
Event.flush();
Event.load(window);
if (Event.KeyPressing[sf::Keyboard::Add]) {
limit_fps++;
window.setFramerateLimit(limit_fps);
}
else if (Event.KeyPressing[sf::Keyboard::Subtract]) {
if (limit_fps > 1) limit_fps -= 1;
window.setFramerateLimit(limit_fps);
}
if (Event.KeyPressed[sf::Keyboard::Q]) {
XY tlu = {
randomFromInterval<double>(50, 150),
randomFromInterval<double>(50, 150)
};
XY trd = tlu;
trd += {
randomFromInterval<double>(50, 100),
randomFromInterval<double>(50, 100)
};
entity = new Entity(Codir<2>(tlu, trd));
entity->addDefault();
entity->moveRelative({
randomFromInterval<double>(300, 400),
-randomFromInterval<double>(0, 100)
});
}
if (Event.KeyPressed[sf::Keyboard::E]) {
if (entity != nullptr) {
entity->remove();
entity = nullptr;
OController.action_set._fixArray();
}
}
if (Event.KeyPressed[sf::Keyboard::Escape]) {
state.pause = true;
return;
}
OController.stepAction();
if (Event.KeyPressed[sf::Keyboard::F1])
OController.debug = !OController.debug;
WorldView.update();
//Clear screen
window.clear(sf::Color::Black);
//Draw Processor
WorldView.use();
OController.stepDraw();
WorldView.use();
for (size_t i = 0; i < 100; i++) Drawer.drawText(i*50, {(double)(i*50), 500});
//Display part
window.display();
}
state.active = false;
return;
}
void createMenu(GameState & state) {
Window * main_window = new Window(Codir<2>({0, 0}, {200, 400}));
main_window->unique_name = "main window";
main_window->label.setText("Main window");
main_window->moveRelative({300, 100});
main_window->addParent(OController.action_win_set);
Button * single_player = new Button(Codir<2>({0, 0}, {150, 20}));
single_player->unique_name = "single_player";
single_player->label.setText("Single Player Game");
single_player->moveRelative({25, 40});
single_player->lambda = [&state] () mutable {
MainLoopType main_loop_type = state.active ? MainLoopType::resume : MainLoopType::init;
mainLoop(main_loop_type, state);
};
main_window->addChild(single_player);
Button * setting = new Button(Codir<2>({0, 0}, {150, 20}));
setting->unique_name = "settings";
setting->label.setText("Setting");
setting->moveRelative({25, 80});
setting->lambda = []() {
std::chrono::seconds sec(1);
std::this_thread::sleep_for(sec);
};
main_window->addChild(setting);
Window * exit_window = new Window(Codir<2>({0, 0}, {150, 120}));
exit_window->unique_name = "exit_window";
exit_window->label.setText("Exit?");
exit_window->moveRelative({300, 100});
Button * exit_button_yes = new Button(Codir<2>({0, 0}, {100, 20}));
exit_button_yes->unique_name = "exit button yes";
exit_button_yes->label.setText("Yes");
exit_button_yes->moveRelative({20, 40});
exit_button_yes->lambda = []() {
exit(1);
};
exit_window->addChild(exit_button_yes);
Button * exit_button_no = new Button(Codir<2>({0, 0}, {100, 20}));
exit_button_no->unique_name = "exit button no";
exit_button_no->label.setText("Back");
exit_button_no->moveRelative({20, 80});
exit_button_no->lambda = [main_window, exit_window]() {
Ras<BaseGui> * ras = exit_window->ras_record->storage;
exit_window->removeParent();
main_window->addParent(*ras);
};
exit_window->addChild(exit_button_no);
Button * exit = new Button(Codir<2>({0, 0}, {150, 20}));
exit->unique_name = "exit";
exit->label.setText("Exit");
exit->moveRelative({25, 120});
//вылетает потому что main_window и exit_window уже не существует
exit->lambda = [main_window, exit_window]() {
Ras<BaseGui> * ras = main_window->ras_record->storage;
main_window->removeParent();
exit_window->addParent(*ras);
exit_window->moveRelative(main_window->focus_offset + XY({20, 0}) - exit_window->focus_offset);
};
main_window->addChild(exit);
state.main_window = main_window;
}
enum class UbermenuType{main, ingame};
int ubermenu(UbermenuType type, bool active, bool pause) {
GameState state;
state.active = active;
state.pause = pause;
state.current_view = {400, 300};
if (type == UbermenuType::main) {
createMenu(state);
}
sf::RenderWindow & window = GameWindow::window;
while (window.isOpen()) {
//EventKeeper processor
Event.flush();
Event.load(window);
if (Event.KeyPressed[sf::Keyboard::Escape] && state.active) {
state.main_window
->getByName("single_player")
.ok("single_player name not found")
->actionRealized();
}
//Action processor
if (!pause) OController.stepAction();
OController.stepActionWin();
WorldView.update();
//Clear screen
window.clear(sf::Color::Black);
//Draw Processor
WorldView.use();
OController.stepDraw();
GuiView.use();
OController.stepDrawWin();
//Display part
window.display();
}
return 0;
}
int main(int argc, char * argv[]) {
FlagParser parser(argc, argv);
//FlagParserAutoHelpSource = &parser;
parser.add(Flag(
{"-help", "--help", "-h", "--h", "?", "-?", "help"},
"show all flags and it's descriptions",
[](FlagParser * parser) {
parser->printAutoGeneratedHelp();
exit(0);
}
));
parser.add(Flag(
{"-width", "-w"},
"set game window width. Expect one int parameter: width",
[](const char * const * argv, size_t left) -> size_t {
if (left < 1) {
printf("flag [%s] expected one parameter", argv[0]);
exit(0);
}
GameWindow::width = std::stoi(argv[1]);
return 1;
}
));
parser.add(Flag(
{"-height", "-h"},
"set game window height. Expect one int parameter: height",
[](const char * const * argv, size_t left) -> size_t {
if (left < 1) {
printf("flag [%s] expected one parameter", argv[0]);
exit(0);
}
GameWindow::height = std::stoi(argv[1]);
return 1;
}
));
parser.add(Flag(
{"-fps"},
"set fps for game window. Expect one int parameter: fps count",
[](const char * const * argv, size_t left) -> size_t {
if (left < 1) {
printf("flag [%s] expected one parameter", argv[0]);
exit(0);
}
GameWindow::limit_fps = std::stoi(argv[1]);
return 1;
}
));
parser.execute();
GameWindow::create(sf::Style::Close);
Drawer.target = &GameWindow::window;
Mouse.window = &GameWindow::window;
GameWindow::resetView(WorldView);
GameWindow::resetView(GuiView);
Basic::texture_default = Textures::load("assets/objects/Default/textures/void.png").ok();
Player::texture_folded = Textures::load("assets/objects/Robot/textures/robot-closed.png").ok();
Player::texture_unfolded = Textures::load("assets/objects/Robot/textures/robot-open.png").ok();
Fonts.load("assets/fonts/verdana.ttf", "verdana");
Fonts.load("assets/fonts/ubuntumono/UbuntuMono-R.ttf" , "UbuntuMono-R" );
Fonts.load("assets/fonts/ubuntumono/UbuntuMono-RI.ttf", "UbuntuMono-RI");
Fonts.load("assets/fonts/ubuntumono/UbuntuMono-B.ttf" , "UbuntuMono-B" );
Fonts.load("assets/fonts/ubuntumono/UbuntuMono-BI.ttf", "UbuntuMono-BI");
Fonts.setFontToText("UbuntuMono-R", Drawer.text);
ubermenu(UbermenuType::main, false, true);
return 0;
}