-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoScene.cpp
742 lines (671 loc) · 22.4 KB
/
DemoScene.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#include <set>
#include <functional>
#include <Logic/Logics/Camera/sge_logic_camera_zoom.hpp>
#include <Object/Shape/sge_shape.hpp>
#include <algorithm>
#include <random>
#include <thread>
#include "DemoScene.hpp"
#include "Image.hpp"
#include "Logics.hpp"
#include "Actions.hpp"
#include "DemoBot.hpp"
#include "Utilities.hpp"
#include "SteeringBehavioursUpdate.hpp"
#include "Game/InputHandler/sge_input_binder.hpp"
#include "Renderer/SpriteBatch/sge_sprite_batch.hpp"
#include "Renderer/sge_renderer.hpp"
#include "QuadBatch.hpp"
#include "QuadObject.hpp"
#include <queue>
#include "Graph.hpp"
#include "Actions.hpp"
#ifdef _WIN32
#include <windows.h>
#endif
class RGTrace: public SGE::Object
{
public:
RGTrace(): Object(b2Vec2_zero, true)
{
this->Object::setVisible(false);
}
~RGTrace() = default;
};
class Distance
{
public:
float operator()(const GridVertex* cur, const GridVertex* end) const
{
return b2Distance(cur->Label().position, end->Label().position);
}
};
class DiagonalDistance
{
const static float sqrt2;
public:
float operator()(const GridVertex* cur, const GridVertex* end) const
{
auto node = cur->Label().position, goal = end->Label().position;
float dx = abs(node.x - goal.x), dy = abs(node.y - goal.y);
return dx < dy ? dx * sqrt2 + dy - dx : dy * sqrt2 + dx - dy;
}
};
const float DiagonalDistance::sqrt2 = sqrt(2.f);
void DemoGameState::InitRandomEngine()
{
this->rand = std::bind(std::uniform_int_distribution<size_t>(0, graph.VertexCount()-1u), std::default_random_engine{});
}
GridCell* DemoGameState::GetCell(b2Vec2 pos)
{
size_t x = size_t(std::floor(pos.x)), y = size_t(std::floor(pos.y));
return &(cells[y][x]);
}
GridVertex* DemoGameState::GetVertex(b2Vec2 pos)
{
GridVertex* res = this->GetCell(pos)->vertex;
if(!res)
{
b2Vec2 dir = {-0.5f, 0.f};
int reset = 8;
while(!res)
{
res = this->GetCell(pos + dir)->vertex;
dir = b2Mul(b2Rot(0.25f * b2_pi), dir);
if(reset-- == 0)
{
reset = 8;
dir *= 2.f;
}
}
}
return res;
}
GridVertex* DemoGameState::GetRandomVertex()
{
GridVertex* res = graph[this->rand()];
return res;
}
GridVertex* DemoGameState::GetRandomVertex(const b2Vec2& position, const float limit, bool inside = true)
{
GridVertex* res = graph[this->rand()];
if(inside)
{
while(b2DistanceSquared(position, res->Label().position) > limit * limit)
{
res = graph[this->rand()];
}
}
else
{
while(b2DistanceSquared(position, res->Label().position) < limit * limit)
{
res = graph[this->rand()];
}
}
return res;
}
Path DemoGameState::GetPath(GridVertex * begin, GridVertex * end)
{
this->graph.AStar(begin, end, DiagonalDistance());
return Path(begin, end);
}
void DemoGameState::UseItem(Item* item)
{
for(auto& bot: this->bots)
{
bot.items.erase(item);
}
this->world->RemoveItem(item);
}
void DemoGameState::NewRocket(b2Vec2 pos, b2Vec2 direction)
{
direction.Normalize();
Rocket* rocket = new Rocket(pos + 0.5f * direction, direction);
this->world->AddRocket(rocket);
this->rocketBatch->addObject(rocket);
this->rockets.push_back(rocket);
}
void DemoGameState::AddExplosion(Rocket* rocket)
{
rocket->setShape(Rocket::ExplosionShape());
rocket->setLayer(-0.6f);
this->explosionBatch->addObject(rocket);
this->explosions.push_back(rocket);
}
void DemoGameState::RemoveRocket(Rocket* rocket)
{
this->world->RemoveRocket(rocket);
this->rocketBatch->removeObject(rocket);
this->rockets.erase(std::find(this->rockets.begin(), this->rockets.end(), rocket));
this->AddExplosion(rocket);
}
void DemoGameState::RemoveExplosion(Rocket* rocket)
{
this->explosionBatch->removeObject(rocket);
this->explosions.erase(std::find(this->explosions.begin(), this->explosions.end(), rocket));
delete rocket;
}
template <typename T>
void DemoGameState::GenerateItems(const size_t bots, SGE::RealSpriteBatch* batch)
{
for(size_t i = 0u; i < bots; ++i)
{
Item* item = new T(this->GetRandomVertex()->Label().position);
batch->addObject(item);
this->items.push_back(item);
}
}
void DemoGameState::RegisterTypes()
{
this->lua.new_usertype<DemoBot>("Bot",
//Static member variables
"RailgunReload", sol::var(std::ref(DemoBot::RailgunReload)),
"LauncherReload", sol::var(std::ref(DemoBot::LauncherReload)),
"RailgunDamage", sol::var(std::ref(DemoBot::RailgunDamage)),
"LauncherDamage", sol::var(std::ref(DemoBot::LauncherDamage)),
"RailgunSpread", sol::var(std::ref(DemoBot::RailgunSpread)),
"LauncherSpread", sol::var(std::ref(DemoBot::LauncherSpread)),
"RailgunRate", sol::var(std::ref(DemoBot::RailgunRate)),
"LauncherRate", sol::var(std::ref(DemoBot::LauncherRate)),
"RailgunDefaultAmmo", sol::var(std::ref(DemoBot::RailgunDefaultAmmo)),
"LauncherDefaultAmmo", sol::var(std::ref(DemoBot::LauncherDefaultAmmo)),
"DefaultHealth", sol::var(std::ref(DemoBot::DefaultHealth)),
"DefaultArmor", sol::var(std::ref(DemoBot::DefaultArmor)),
"MaxSpeed", sol::var(std::ref(DemoBot::maxSpeed)),
"MaxForce", sol::var(std::ref(DemoBot::maxForce)),
"SwapSpeed", sol::var(std::ref(DemoBot::SwapSpeed)),
"RocketSpeed", sol::var(std::ref(DemoBot::RocketSpeed)),
//Readonly Properites
"RGLoadedAmmo", sol::property(&DemoBot::RGAmmo),
"RLLoadedAmmo", sol::property(&DemoBot::RLAmmo),
"RGSpareAmmo", sol::property(&DemoBot::RGSpareAmmo),
"RLSpareAmmo", sol::property(&DemoBot::RLSpareAmmo),
"TotalAmmo", sol::property(&DemoBot::TotalAmmo),
"RLReloadTime", sol::property(&DemoBot::RLCD),
"RGReloadTime", sol::property(&DemoBot::RGCD),
"IsSwappingWeapon", sol::property(&DemoBot::IsSwapping),
"IsReloadingWeapon", sol::property(&DemoBot::IsCurrentWeaponReloading),
"Health", sol::property(&DemoBot::Health),
"Armor", sol::property(&DemoBot::Armor),
"State", sol::property(&DemoBot::getState),
"CurrentWeapon", sol::property(&DemoBot::CurrentWeapon),
//Modifiers
"AddHealth", &DemoBot::AddHealth,
"AddArmor", &DemoBot::AddArmor,
"AddRailgunAmmo", &DemoBot::AddRailgunAmmo,
"AddLauncherAmmo", &DemoBot::AddLauncherAmmo,
"AddDamage", &DemoBot::Damage,
"SwapWeapon", &DemoBot::SwapWeapon
);
this->lua.new_enum<BotState,false>("BotState",
{
{"Wandering", BotState::Wandering},
{"Attacking", BotState::Attacking},
{"Running", BotState::Running},
{"GettingAmmo", BotState::GettingAmmo},
{"GettingHealth", BotState::GettingHealth},
{"GettingArmor", BotState::GettingArmor}
});
this->lua.new_enum<CurrentWeapon,false>("CurrentWeapon",
{
{"Railgun", CurrentWeapon::Railgun},
{"Launcher", CurrentWeapon::Launcher}
});
this->lua.new_enum<Item::IType,false>("ItemType",
{
{"HealthPack", Item::IType::Health},
{"ArmorPack", Item::IType::Armor},
{"RailgunAmmo", Item::IType::RGAmmo},
{"LauncherAmmo", Item::IType::RLAmmo},
});
this->lua.new_usertype<HealthPack>("HealthPack", "Value", sol::var(std::ref(HealthPack::HealthValue)));
this->lua.new_usertype<ArmorPack>("ArmorPack", "Value", sol::var(std::ref(ArmorPack::ArmorValue)));
this->lua.new_usertype<RailgunAmmo>("RailgunAmmo", "Value", sol::var(std::ref(RailgunAmmo::AmmoValue)));
this->lua.new_usertype<RocketAmmo>("LauncherAmmo", "Value", sol::var(std::ref(RocketAmmo::AmmoValue)));
this->lua.new_usertype<Item>("Items", "RespawnTime", sol::var(std::ref(Item::itemCD)));
this->lua.script_file("./base_script.lua");
}
bool DemoScene::init()
{
return true;
}
constexpr size_t ObstaclesNum = 12u;
DemoScene::DemoScene(SGE::Game* game, const char* path) : Scene(), world(Width, Height), game(game),
path([game](const char* path)
{
return game->getGamePath() + path;
}(path))
{
static bool initialized = init();
}
struct GridCellBuild
{
size_t x = 0u, y = 0u;
enum State
{
Accepted,
Queued,
Untested,
Invalid
} state = State::Untested;
SGE::Object* dummy = nullptr;
GridVertex* vertex = nullptr;
};
class GraphCellDummy: public SGE::Object
{
public:
GraphCellDummy(size_t x, size_t y) : Object(0.5f + x, 0.5f + y, true, getCircle()){}
GraphCellDummy(b2Vec2 pos) : Object(pos.x, pos.y, true, getCircle()) {}
};
class GraphCellDummy1: public GraphCellDummy
{
public:
GraphCellDummy1(size_t x, size_t y): GraphCellDummy(x,y)
{
this->Object::setShape(SGE::Shape::Circle(0.3f, true));
}
GraphCellDummy1(b2Vec2 pos): GraphCellDummy(pos)
{
this->Object::setShape(SGE::Shape::Circle(0.3f, true));
}
};
class GraphEdgeDummy: public SGE::Object
{
public:
GraphEdgeDummy(size_t x, size_t y): Object(0.5f + x, 0.5f + y, true, getCircle())
{}
GraphEdgeDummy(b2Vec2 pos): Object(pos.x, pos.y, true, getCircle())
{}
};
constexpr size_t Bots = 5u;
class InputLua : public SGE::Action
{
sol::state_view lua;
std::thread thread;
public:
InputLua(sol::state& state) : Action(true), lua(state)
{
}
void runLua()
{
std::cout << "> ";
std::string script;
std::getline(std::cin, script);
if (script.find("load ") == 0)
{
auto result = this->lua.script_file(script.substr(5), sol::script_pass_on_error);
if (!result.valid())
{
std::cerr << "Failed to execute: " << script << std::endl;
sol::error err = result;
std::cerr << err.what() << std::endl;;
}
}
else
{
auto result = this->lua.script(script, sol::script_pass_on_error);
if (!result.valid())
{
std::cerr << "Failed to execute: " << script << std::endl;
sol::error err = result;
std::cerr << err.what() << std::endl;;
}
}
SGE::Game::getGame()->raiseWindow();
}
virtual void action_begin() override
{
}
virtual void action_main() override
{
#ifdef _WIN32
//This works!
HWND console = GetConsoleWindow();
BringWindowToTop(console);
SetActiveWindow(console);
#endif
if (thread.joinable())
thread.join();
this->thread = std::thread(&InputLua::runLua, this);
}
virtual void action_ends() override
{
}
};
void DemoScene::loadScene()
{
this->gs = new DemoGameState();
this->gs->lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::string);
this->gs->RegisterTypes();
this->gs->world = &this->world;
//RenderBatches
SGE::BatchRenderer* renderer = SGE::Game::getGame()->getRenderer();
this->level = SGE::Level();
GLuint basicProgram = renderer->getProgramID("BatchShader.vert", "BatchShader.frag");
GLuint scaleUVProgram = renderer->getProgramID("BatchUVShader.vert", "BatchShader.frag");
GLuint QuadProgram = renderer->getProgramID("QuadBatchShader.vert", "BatchShader.frag");
std::string lightBrickTexPath = "Resources/Textures/light_bricks.png";
std::string zombieTexPath = "Resources/Textures/zombie.png";
std::string cellTexPath = "Resources/Textures/cell.png";
std::string beamPath = "Resources/Textures/pointer.png";
std::string rocketPath = "Resources/Textures/rocket.png";
std::string explosionPath = "Resources/Textures/explosion.png";
std::string healthPath = "Resources/Textures/health.png";
std::string armorPath = "Resources/Textures/armor.png";
std::string rlammoPath = "Resources/Textures/rlammo.png";
std::string rgammoPath = "Resources/Textures/rgammo.png";
SGE::RealSpriteBatch* wallBatch = renderer->getBatch(renderer->newBatch(scaleUVProgram, lightBrickTexPath, 4, false, true));
SGE::RealSpriteBatch* obstacleBatch = renderer->getBatch(renderer->newBatch<QuadBatch>(QuadProgram, lightBrickTexPath, 12, false, true));
SGE::RealSpriteBatch* botBatch = renderer->getBatch(renderer->newBatch(basicProgram, zombieTexPath, Bots));
this->gs->railBatch = renderer->getBatch(renderer->newBatch(basicProgram, beamPath, Bots));
this->gs->rocketBatch = renderer->getBatch(renderer->newBatch(basicProgram, rocketPath, Bots * 100u));
this->gs->explosionBatch = renderer->getBatch(renderer->newBatch(basicProgram, explosionPath, Bots * 100u));
SGE::RealSpriteBatch* healthBatch = renderer->getBatch(renderer->newBatch(basicProgram, healthPath, Bots * 5u));
SGE::RealSpriteBatch* armorBatch = renderer->getBatch(renderer->newBatch(basicProgram, armorPath, Bots * 5u));
SGE::RealSpriteBatch* rgammoBatch = renderer->getBatch(renderer->newBatch(basicProgram, rgammoPath, Bots * 5u));
SGE::RealSpriteBatch* rlammoBatch = renderer->getBatch(renderer->newBatch(basicProgram, rlammoPath, Bots * 5u));
SGE::RealSpriteBatch* graphTestBatch = renderer->getBatch(renderer->newBatch(basicProgram, cellTexPath, size_t(Width * Height), false, true));
SGE::RealSpriteBatch* graphEdgeTestBatch = renderer->getBatch(renderer->newBatch(basicProgram, "Resources/Textures/path.png", size_t(Width * Height * 8u), false, true));
QuadBatch* obBatch = dynamic_cast<QuadBatch*>(obstacleBatch);
if (!obBatch)
throw std::runtime_error("QuadBatch cast failed!");
GLuint IBO = botBatch->initializeIBO();
GLuint sampler = botBatch->initializeSampler();
obstacleBatch->initializeIBO(IBO);
obstacleBatch->initializeSampler(GL_REPEAT, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
for (SGE::RealSpriteBatch* b : { wallBatch, this->gs->railBatch, this->gs->rocketBatch, healthBatch, armorBatch, rgammoBatch, rlammoBatch })
{
b->initializeIBO(IBO);
b->initializeSampler(sampler);
}
//!RenderBatches
auto& world = this->level.getWorld();
world.reserve(4 + ObstaclesNum);
//Boundaries
SGE::Shape* horizontal = SGE::Shape::Rectangle(Width, 1.f, false);
SGE::Shape* vertical = SGE::Shape::Rectangle(1.f, Height + 2.f, false);
world.emplace_back(-0.5f, Height * 0.5f, lightBrickTexPath);
world.back().setShape(vertical);
this->world.AddWall(&world.back(), Wall::Right);
wallBatch->addObject(&world.back());
world.emplace_back(Width + .5f, Height * 0.5f, lightBrickTexPath);
world.back().setShape(vertical);
this->world.AddWall(&world.back(), Wall::Left);
wallBatch->addObject(&world.back());
world.emplace_back(Width * 0.5f, Height + .5f, lightBrickTexPath);
world.back().setShape(horizontal);
this->world.AddWall(&world.back(), Wall::Bottom);
wallBatch->addObject(&world.back());
world.emplace_back(Width * 0.5f, -0.5f, lightBrickTexPath);
world.back().setShape(horizontal);
this->world.AddWall(&world.back(), Wall::Top);
wallBatch->addObject(&world.back());
//!Boundaries
//Camera
SGE::Camera2d* cam = game->getCamera();
cam->setPosition(glm::vec2{ 32.f * Width, 32.f * Height });
cam->setCameraScale(0.197f);
this->addLogic(new SpectatorCamera(10, SGE::Key::W, SGE::Key::S, SGE::Key::A, SGE::Key::D, cam));
this->addLogic(new SGE::Logics::CameraZoom(cam, 0.5f, 1.f, 0.197f, SGE::Key::Q, SGE::Key::E));
//!Camera
//Obstacles
using Quad = QuadBatch::Quad;
std::uniform_real_distribution<float> angle_distribution(-b2_pi, b2_pi);
std::mt19937 engine((std::random_device{})());
auto angle = std::bind(angle_distribution, engine);
constexpr float RB1 = 24.f; //Region Boundary
constexpr float RB2 = 10.f; //Region Boundary
Quad Diamond1 = {64.f * glm::vec2{-20.f, 0.f}, 64.f * glm::vec2{0, -8.f}, 64.f * glm::vec2{20.f, 0.f}, 64.f * glm::vec2{0.f, 8.f } };
Quad Diamond2 = {64.f * glm::vec2{-8.f, 0.f}, 64.f * glm::vec2{0, -3.f}, 64.f * glm::vec2{8.f, 0.f}, 64.f * glm::vec2{0.f, 3.f}};
SGE::Object* obstacle1 = new QuadObstacle(RB1, RB1, angle(), Diamond1);
SGE::Object* obstacle2 = new QuadObstacle(Width - RB1, RB1, angle(), Diamond1);
SGE::Object* obstacle3 = new QuadObstacle(Width - RB1, Height - RB1, angle(), Diamond1);
SGE::Object* obstacle4 = new QuadObstacle(RB1, Height - RB1, angle(), Diamond1);
SGE::Object* obstacle5 = new QuadObstacle(RB2, RB2, angle(), Diamond2);
SGE::Object* obstacle6 = new QuadObstacle(Width - RB2, RB2, angle(), Diamond2);
SGE::Object* obstacle7 = new QuadObstacle(Width - RB2, Height - RB2, angle(), Diamond2);
SGE::Object* obstacle8 = new QuadObstacle(RB2, Height - RB2, angle(), Diamond2);
SGE::Object* obstacle9 = new QuadObstacle(RB2, .5f * Height, angle(), Diamond2);
SGE::Object* obstacle10 = new QuadObstacle(.5f * Width, RB2, angle(), Diamond2);
SGE::Object* obstacle11 = new QuadObstacle(Width - RB2, .5f * Height, angle(), Diamond2);
SGE::Object* obstacle12 = new QuadObstacle(.5f * Width, Height - RB2, angle(), Diamond2);
for(auto ob : {obstacle1, obstacle2, obstacle3, obstacle4})
{
obBatch->addObject(ob, Diamond1);
this->world.AddObstacle(ob);
this->gs->obstacles.push_back(ob);
}
for(auto ob : {obstacle5, obstacle6, obstacle7, obstacle8, obstacle9, obstacle10, obstacle11, obstacle12})
{
obBatch->addObject(ob, Diamond2);
this->world.AddObstacle(ob);
this->gs->obstacles.push_back(ob);
}
//Grid
//#define GraphCellDebug
//#define GraphEdgeDebug
{
std::queue<GridCellBuild*> cells;
int intersections = 0;
GridCellBuild grid[Y][X];
std::pair<int, int> directions[8] =
{
{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}
};
b2Vec2 points[4] = {{-.5f, 0.f}, {0.f, -.5f}, {0.5f, 0.f}, {0.f, .5f}};
b2Vec2 diags[4];
for(size_t i = 0; i < 4u; ++i)
diags[i] = b2Mul(b2Rot(0.5f * b2_pi), points[i]);
for(size_t x = 0u; x < X; ++x)
for(size_t y = 0u; y < Y; ++y)
{
grid[y][x].x = x;
grid[y][x].y = y;
}
grid[0][0].state = GridCellBuild::Queued;
cells.push(&grid[0][0]);
while(!cells.empty())
{
GridCellBuild& currentCell = *cells.front();
cells.pop();
intersections = 0;
b2Vec2 pos = b2Vec2{0.5f + currentCell.x, 0.5f + currentCell.y};
std::vector<SGE::Object*> obstacles = std::move(this->world.getObstacles(pos, 1.5f));
for(SGE::Object* o : obstacles)
{
QuadObstacle* qo = dynamic_cast<QuadObstacle*>(o);
if(!qo) continue;
for(Edge edge : qo->getEdges())
{
b2Vec2 radius = -edge.Normal();
radius *= 0.5f;
float dist = b2DistanceSquared(pos, edge.From());
if(dist <= 0.25f)
{
currentCell.state = GridCellBuild::Invalid;
break;
}
b2Vec2 intersection;
if(LineIntersection(pos, pos + radius, edge.From(), edge.To(), dist, intersection))
{
currentCell.state = GridCellBuild::Invalid;
break;
}
if(LineIntersection(pos, pos + b2Vec2{100.f, 0.f}, edge.From(), edge.To(), dist, intersection))
{
++intersections;
}
}
if(currentCell.state == GridCellBuild::Invalid || 1 == intersections % 2)
break;
}
if(currentCell.state != GridCellBuild::Invalid && 0 == intersections % 2)
{
currentCell.state = GridCellBuild::Accepted;
currentCell.vertex = new GridVertex(CellLabel(pos));
this->gs->graph.AddVertex(currentCell.vertex);
#ifdef GraphCellDebug
currentCell.dummy = new GraphCellDummy(currentCell.x, currentCell.y);
graphTestBatch->addObject(currentCell.dummy);
#endif
for(size_t i = 0u; i < 8u; ++i)
{
auto dir = directions[i];
size_t x = currentCell.x + dir.first;
size_t y = currentCell.y + dir.second;
b2Vec2 edgeVec = b2Vec2{float(dir.first), float(dir.second)};
if(x < X && y < Y)
{
GridCellBuild& otherCell = grid[y][x];
switch(otherCell.state)
{
case GridCellBuild::Accepted:
{
bool intersected = false;
for(SGE::Object* o : obstacles)
{
QuadObstacle* qo = dynamic_cast<QuadObstacle*>(o);
if(!qo) continue;
for(Edge edge : qo->getEdges())
{
b2Vec2(&pts)[4] = i % 2 ? diags : points;
for(b2Vec2 offset : pts)
{
b2Vec2 from = pos + offset;
b2Vec2 to = from + edgeVec;
float dist;
b2Vec2 inters;
intersected = LineIntersection(from, to, edge.From(), edge.To(), dist, inters);
if(intersected)
{
break;
}
}
if(intersected) break;
}
if(intersected) break;
}
if(!intersected)
{
this->gs->graph.AddEdge(currentCell.vertex, otherCell.vertex, edgeVec.Length());
#ifdef GraphEdgeDebug
auto edgeOb = new GraphEdgeDummy(pos + 0.5f * edgeVec);
edgeOb->setOrientation(edgeVec.Orientation());
edgeOb->setLayer(.5f);
edgeOb->setShape(SGE::Shape::Rectangle(edgeVec.Length(), 0.05f, true));
graphEdgeTestBatch->addObject(edgeOb);
#endif
}
break;
}
case GridCellBuild::Queued: break;
case GridCellBuild::Untested:
{
otherCell.state = GridCellBuild::Queued;
cells.push(&otherCell);
break;
}
case GridCellBuild::Invalid: break;
default: break;
}
}
}
}
}
for(size_t x = 0u; x < X; ++x)
{
for(size_t y = 0u; y < Y; ++y)
{
if(grid[y][x].state == GridCellBuild::Accepted)
{
auto& cell = this->gs->cells[y][x];
cell.state = GridCell::Valid;
cell.vertex = grid[y][x].vertex;
}
}
}
this->gs->InitRandomEngine();
//#define ASTARDEBUG
#ifdef ASTARDEBUG
//Test
GridVertex* begin = this->gs->cells[0][0].vertex;
GridVertex* end = this->gs->cells[Y-1u][X - 1u].vertex;
this->gs->graph.AStar(begin, end, DiagonalDistance{});
while(end != begin)
{
graphTestBatch->addObject(new GraphCellDummy(end->Label().position));
end = end->Parent();
}
for(auto v : this->gs->graph)
{
if(v->State() != CTL::VertexState::White)
{
graphTestBatch->addObject(new GraphCellDummy1(v->Label().position));
}
}
#endif
} //!Grid
//Players
{
this->gs->bots.reserve(Bots);
this->gs->lua.create_table("Bots");
for(int i = 0; i < Bots; ++i)
{
this->gs->bots.emplace_back(this->gs->GetRandomVertex()->Label().position, getCircle(), &this->world);
DemoBot* bot = &this->gs->bots.back();
this->gs->lua["Bots"][i+1] = bot;
botBatch->addObject(bot);
this->world.AddMover(bot);
bot->RailgunTrace = new RGTrace();
this->gs->railBatch->addObject(bot->RailgunTrace);
}
}
//Items
{
this->gs->GenerateItems<HealthPack>(Bots, healthBatch);
this->gs->GenerateItems<ArmorPack>(Bots, armorBatch);
this->gs->GenerateItems<RocketAmmo>(Bots, rlammoBatch);
this->gs->GenerateItems<RailgunAmmo>(Bots, rgammoBatch);
}
//Logics
this->addLogic(new SteeringBehavioursUpdate(&this->gs->bots));
this->addLogic(new SeparateBots(&this->world, &this->gs->bots));
this->addLogic(new MoveAwayFromObstacle(&this->world, this->gs->obstacles));
this->addLogic(new MoveAwayFromWall(&this->world, this->gs->bots));
this->addLogic(new BotLogic(&this->world, this->gs));
this->addLogic(new ItemLogic(&this->world, this->gs));
this->addLogic(new RocketLogic(this->gs, &this->world));
//
game->mapAction(SGE::InputBinder(new InputLua(this->gs->lua), SGE::Key::Return));
}
void DemoScene::unloadScene()
{
this->finalize();
}
DemoScene::~DemoScene()
{
std::cout << "~MainScene" << std::endl;
}
template<typename Vec>
void vec_clear(Vec& vec)
{
for (auto h : vec)
{
delete h;
}
vec.clear();
}
void DemoScene::finalize()
{
game->getRenderer()->deleteSceneBatch(this);
delete this->gs;
this->level.clear();
this->world.clear();
vec_clear(this->getLogics());
vec_clear(this->getActions());
this->getObjects().clear();
game->unmapAll();
}
void DemoScene::onDraw()
{}