-
Notifications
You must be signed in to change notification settings - Fork 0
/
robotgame.cpp
446 lines (385 loc) · 13.9 KB
/
robotgame.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
#if defined(dupa) && defined(unix) || defined(__unix__) || defined(__unix)
#define PGE_GFX_OPENGL33
#endif
#define OLC_PGEX_DEAR_IMGUI_IMPLEMENTATION
#define OLC_PGE_APPLICATION
#include "robotgame.h"
#include <iostream>
RobotGame::RobotGame() : pge_imgui(true), map(), robot(this, &map, &sm, {40, 40})
{
this->sAppName = "Robot game";
this->state = std::make_unique<UIState::EditIdleState>();
this->map.Load();
for (auto const& file : std::filesystem::directory_iterator(this->examplesPath))
{
std::filesystem::path fpath = file.path();
std::string ext = fpath.extension().string();
if (ext == std::string(".lua") || ext == std::string(".txt"))
{
std::ifstream fhandle(fpath.c_str());
this->helpFiles[fpath.filename().string()] = std::string((std::istreambuf_iterator<char>(fhandle)),
std::istreambuf_iterator<char>());
fhandle.close();
}
}
}
bool RobotGame::OnUserCreate()
{
this->tempLayer = this->CreateLayer();
this->connectionsLayer = this->CreateLayer();
this->blocksUILayer = this->CreateLayer();
this->infoUILayer = this->CreateLayer();
this->mapGridLayer = this->CreateLayer();
this->gridLayer = this->CreateLayer();
this->EnableLayer(this->tempLayer, true);
this->EnableLayer(this->connectionsLayer, true);
this->EnableLayer(this->blocksUILayer, true);
this->EnableLayer(this->infoUILayer, true);
this->EnableLayer(this->mapGridLayer, true);
this->EnableLayer(this->gridLayer, true);
this->ClearLayers();
this->sm.LoadAll();
inventory.push_back({2, std::make_shared<ProgrammableBlock>(&sm, olc::vi2d{0, 0}, std::vector<std::string>{"input1", "input2", "input3", "input4"}, std::vector<std::string>{"output1", "output2", "output3", "output4"})});
inventory.push_back({5, std::make_shared<ButtonBlock>(&sm, olc::vi2d{0, 0})});
inventory.push_back({3, std::make_shared<DiodeBlock>(&sm, olc::vi2d{0, 0})});
inventory.push_back({1, std::make_shared<MotorBlock>(&sm, &robot, olc::vi2d{0, 0})});
inventory.push_back({1, std::make_shared<RadarBlock>(&sm, &map, &robot, olc::vi2d{0, 0})});
inventory.push_back({1, std::make_shared<MapBlock>(&sm, olc::vi2d{0, 0})});
return true;
}
void RobotGame::DrawGrid()
{
this->DrawLine({0, 0}, {this->gridSize.x * blocksize - 1, 0});
this->DrawLine({0, 0}, {0, this->gridSize.y * blocksize - 1});
this->SetDrawTarget(this->gridLayer);
for (int i = 1; i <= gridSize.x; i++)
{
this->DrawLine({i * blocksize - 1, 0}, {i * blocksize - 1, this->gridSize.y * blocksize - 1}, olc::GREY);
for (int j = 1; j <= this->gridSize.y; j++)
this->DrawLine({0, j * blocksize - 1}, {this->gridSize.x * blocksize - 1, j * blocksize - 1}, olc::GREY);
}
}
bool RobotGame::CursorCollide(olc::vi2d pos, olc::vi2d size, olc::vi2d mousepos)
{
return pos.x <= mousepos.x &&
pos.x + size.x > mousepos.x &&
pos.y <= mousepos.y &&
pos.y + size.y > mousepos.y;
}
Block* RobotGame::GetBlockAt(olc::vi2d pos)
{
for (auto block = blocks.begin(); block != blocks.end(); ++block)
{
if (this->CursorCollide(block->get()->pos * blocksize, block->get()->size * blocksize, pos))
return block->get();
}
return nullptr;
}
Block* RobotGame::GetBlockUnderMouse()
{
return this->GetBlockAt(this->GetMousePos());
}
olc::vi2d RobotGame::GetGridAt(olc::vi2d pos)
{
return {pos.x / blocksize, pos.y / blocksize};
}
bool RobotGame::CanBePlaced(Block* block, olc::vi2d gridpos)
{
for (int i = 0; i < block->size.x; ++i)
for (int j = 0; j < block->size.y; ++j)
{
olc::vi2d curGridPos = gridpos + olc::vi2d(i, j);
Block* testedBlock = this->GetBlockAt(curGridPos * blocksize);
if (testedBlock && testedBlock != block || curGridPos.x >= this->gridSize.x || curGridPos.y >= this->gridSize.y)
return false;
}
return true;
}
void RobotGame::DrawBlock(Block* block)
{
this->DrawBlockSprite(block->pos * blocksize, block);
}
void RobotGame::DrawBlockSprite(olc::vi2d pos, Block* block, int scale)
{
if (scale < 0)
scale = this->spritescale;
this->DrawSprite(pos, block->GetSprite(), scale);
}
void RobotGame::SelectBlock(Block* block)
{
this->selectedBlock = block;
}
void RobotGame::HandleInput()
{
if (this->IsFocused())
{
InputState input;
input.kbd = this->pKeyboardState;
input.mousepos = this->GetMousePos();
input.lmb = this->GetMouse(olc::Mouse::LEFT);
input.rmb = this->GetMouse(olc::Mouse::RIGHT);
input.mmb = this->GetMouse(olc::Mouse::MIDDLE);
std::unique_ptr<IState> newState = this->state->HandleInput(this);
if (newState)
{
this->state->OnExit(this);
this->state = std::move(newState);
this->state->OnEnter(this);
}
for (auto block : blocks)
{
block->HandleInput(this->GetBlockUnderMouse() == block.get(), &input);
}
}
}
void RobotGame::UpdateState()
{
this->state.get()->Update(this);
}
void RobotGame::DrawInfoUI()
{
this->infoMenuPos.x = gridSize.x * blocksize;
this->SetDrawTarget(this->infoUILayer);
this->Clear(olc::BLANK);
if (this->selectedBlock)
{
int scale = spritescale;
olc::Sprite* sprite = this->selectedBlock->GetDefaultSprite().get();
if (this->selectedBlock->size.x > 2 || this->selectedBlock->size.y > 2)
scale /= 2;
this->DrawSprite(this->infoMenuPos + olc::vi2d{uiPadding, 0}, sprite, scale);
olc::vi2d pos = this->infoMenuPos;
pos.y += sprite->height * scale;
olc::vi2d size = olc::vi2d{this->ScreenWidth(), this->ScreenHeight()} - pos;
olc::vi2d canvasSize = {this->ScreenWidth(), this->ScreenHeight()};
olc::vi2d windowSize = this->GetWindowSize();
float ratio = std::fminf((float)windowSize.x/(float)canvasSize.x, (float)windowSize.y/(float)canvasSize.y);
olc::vi2d canvasScaled = {(int32_t)((float)canvasSize.x/(1.0f/ratio)), (int32_t)((float)canvasSize.y/(1.0f/ratio))};
olc::vi2d barsSize = (windowSize - canvasScaled) / 2;
pos.x = (float)pos.x * ratio + barsSize.x;
pos.y = (float)pos.y * ratio + barsSize.y;
size.x = (float)size.x * ratio + barsSize.x;
ImVec2 imguiPos = {(float)pos.x, (float)pos.y};
ImVec2 imguiSize = {(float)size.x, (float)size.y};
ImGui::SetNextWindowPos(imguiPos);
ImGui::SetNextWindowSize(imguiSize);
ImGui::Begin("##Info", NULL, this->infoMenuFlags);
ImGui::TextWrapped(this->selectedBlock->GetDescription().c_str());
ImGui::End();
}
}
olc::vi2d RobotGame::GetBlockCenter(Block* block)
{
return ((block->pos * 2 + block->size) * blocksize) / 2;
}
void RobotGame::DrawConnections()
{
this->SetDrawTarget(this->connectionsLayer);
for (auto block = blocks.begin(); block != blocks.end(); ++block)
{
for (auto port = block->get()->ports.begin(); port != block->get()->ports.end(); ++port)
{
for (auto conn = port->second->connections.begin(); conn != port->second->connections.end(); ++conn)
{
this->DrawLine(this->GetBlockCenter(block->get()), this->GetBlockCenter((*conn)->owner), olc::GREEN);
}
}
}
}
void RobotGame::DrawBlocks()
{
this->SetDrawTarget(this->mapGridLayer);
for (auto block = blocks.begin(); block != blocks.end(); ++block)
this->DrawBlock(block->get());
}
void RobotGame::ClearLayers()
{
this->SetDrawTarget(this->connectionsLayer);
this->Clear(olc::BLANK);
this->SetDrawTarget(this->gridLayer);
this->Clear(olc::BLANK);
this->SetDrawTarget(this->infoUILayer);
this->Clear(olc::BLANK);
this->SetDrawTarget(this->blocksUILayer);
this->Clear(olc::BLANK);
this->SetDrawTarget(this->tempLayer);
this->Clear(olc::BLANK);
this->SetDrawTarget(this->mapGridLayer);
this->Clear(olc::BLANK);
}
void RobotGame::ShowDebugWindow()
{
static bool gridEnabled = true;
static bool connEnabled = true;
static bool infoUIEnabled = true;
static bool blocksUIEnabled = true;
ImGui::Begin("Debug");
ImGui::Checkbox("gridLayer", &gridEnabled);
ImGui::Checkbox("connectionsLayer", &connEnabled);
ImGui::Checkbox("infoMenuLayer", &infoUIEnabled);
ImGui::Checkbox("blocksMenuLayer", &blocksUIEnabled);
ImGui::SliderInt("GridSize X", &gridSize.x, 1, 20);
ImGui::SliderInt("GridSize Y", &gridSize.y, 1, 20);
Block* block = this->GetBlockUnderMouseInv();
if (block)
ImGui::Text("%s", block->GetDescription().c_str());
ImGui::End();
// this->EnableLayer(gridLayer, gridEnabled);
// this->EnableLayer(connectionsLayer, connEnabled);
// this->EnableLayer(infoUILayer, infoUIEnabled);
// this->EnableLayer(blocksUILayer, blocksUIEnabled);
}
olc::vi2d RobotGame::GetResolution()
{
int w = this->gridSize.x * this->blocksize + this->infoMenuSize.x;
int h = std::max(this->gridSize.y * this->blocksize, this->infoMenuSize.y);
return {w, h};
}
void RobotGame::PlaceBlock(std::shared_ptr<Block> block)
{
this->blocks.push_back(block);
}
int RobotGame::InvCount(const Block* schema)
{
for (auto block = this->inventory.begin(); block != this->inventory.end(); ++block)
{
if (block->second.get() == schema)
{
return block->first;
}
}
return 0;
}
void RobotGame::IncrInvCount(const Block* schema, int count)
{
for (auto block = this->inventory.begin(); block != this->inventory.end(); ++block)
{
if (block->second.get() == schema)
{
block->first += count;
assert(block->first >= 0);
break;
}
}
}
void RobotGame::RemoveBlock(Block* other)
{
for (auto block = this->blocks.begin(); block != this->blocks.end(); ++block)
{
if (block->get() == other)
{
if (other == this->selectedBlock)
this->selectedBlock = nullptr;
this->blocks.erase(block);
break;
}
}
}
Block* RobotGame::GetBlockUnderMouseInv()
{
int posx = 4;
for (auto block : inventory)
{
int scale = spritescale;
if (block.second->size.x > 2 || block.second->size.y > 2)
scale /= 2;
if (this->CursorCollide({posx, this->blocksMenuPos.y}, block.second->size * spritesize * scale, this->GetMousePos()))
return block.second.get();
posx += block.second->size.x * spritesize * scale + uiPadding;
}
return nullptr;
}
void RobotGame::DrawBlocksUI()
{
this->SetDrawTarget(this->blocksUILayer);
this->blocksMenuPos = {0, this->gridSize.y * blocksize};
int posx = 4;
for (const auto& [count, block] : inventory)
{
int scale = spritescale;
if (block->size.x > 2 || block->size.y > 2)
scale /= 2;
this->DrawBlockSprite({posx, this->blocksMenuPos.y}, block.get(), scale);
this->DrawString(posx, this->blocksMenuPos.y + block->size.y * spritesize * scale, std::to_string(count), count > 0 ? olc::WHITE : olc::RED, 3);
posx += block->size.x * spritesize * scale + uiPadding;
}
int posy = ((this->gridSize.y + 2) * blocksize + 20);
olc::vi2d canvasSize = {this->ScreenWidth(), this->ScreenHeight()};
olc::vi2d windowSize = this->GetWindowSize();
float ratio = std::fminf((float)windowSize.x/(float)canvasSize.x, (float)windowSize.y/(float)canvasSize.y);
olc::vi2d canvasScaled = {(int32_t)((float)canvasSize.x/(1.0f/ratio)), (int32_t)((float)canvasSize.y/(1.0f/ratio))};
olc::vi2d barsSize = (windowSize - canvasScaled) / 2;
posy = (float)posy * ratio + barsSize.y;
ImVec2 imguiPos = {0, (float)posy};
ImVec2 imguiSize = {(float)windowSize.x, 60.0f + (float)barsSize.y};
ImGui::SetNextWindowPos(imguiPos);
ImGui::SetNextWindowSize(imguiSize);
ImGui::Begin("##Help", NULL, this->infoMenuFlags);
ImGui::TextWrapped("H: Help M: switch mode (building/interactive) LMB on block in inventory: placement mode RMB on block on the grid: linking mode\n"
"Please check help menu (press H) to learn how to use the programmable block and other blocks, especially see basic.lua");
ImGui::End();
}
void RobotGame::DrawHelp()
{
static bool visible = false;
if (this->GetKey(olc::Key::H).bPressed)
visible = !visible;
if (visible)
{
ImGui::SetNextWindowSizeConstraints(ImVec2(400, 300), ImVec2(FLT_MAX, FLT_MAX));
if (ImGui::Begin("Help", &visible))
{
if (ImGui::BeginTabBar("HelpTab"))
{
for (auto item : this->helpFiles)
{
if (ImGui::BeginTabItem(item.first.c_str()))
{
ImVec2 textInputSize = ImGui::GetContentRegionAvail();
ImGui::InputTextMultiline(("##help"+item.first).c_str(), &item.second, textInputSize, ImGuiInputTextFlags_ReadOnly);
ImGui::EndTabItem();
}
}
ImGui::EndTabBar();
}
}
ImGui::End();
}
}
void RobotGame::SimStart()
{
for (auto block : blocks)
block->Start();
}
void RobotGame::SimStop()
{
for (auto block : blocks)
block->Stop();
}
void RobotGame::SimTick()
{
for (auto block : blocks)
block->Update(this->timedelta);
for (auto block : blocks)
block->Swap();
}
bool RobotGame::OnUserUpdate(float fElapsedTime)
{
this->timedelta = fElapsedTime;
this->ClearLayers();
this->HandleInput();
this->UpdateState();
this->DrawGrid();
this->DrawBlocksUI();
this->DrawInfoUI();
this->DrawConnections();
this->DrawBlocks();
this->DrawHelp();
//ImGui::ShowDemoWindow();
//this->ShowDebugWindow();
return true;
}
void RobotGame::DrawUI(void)
{
pge_imgui.ImGui_ImplPGE_Render();
}