-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystems.cpp
More file actions
464 lines (417 loc) · 19.4 KB
/
Systems.cpp
File metadata and controls
464 lines (417 loc) · 19.4 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
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
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** Systems implementation
*/
#include "Systems.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include <sstream>
#include "ECSCustomTypes.hpp"
#include "Registry.hpp"
#include "SystemManagersDirector.hpp"
#ifdef CLIENT
#include "CustomTypes.hpp"
#include "NitworkClient.hpp"
#include "Raylib.hpp"
#else
#include "NitworkServer.hpp"
#endif
namespace Systems {
void windowCollision(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry ®istry = Registry::getInstance();
Registry::components<Types::Position> arrPosition = registry.getComponents<Types::Position>();
Registry::components<Types::CollisionRect> arrCollisionRect =
registry.getComponents<Types::CollisionRect>();
std::vector<std::size_t> ids = registry.getEntitiesByComponents(
{typeid(Types::Player), typeid(Types::Position), typeid(Types::CollisionRect)});
const float maxPercent = 100.0F;
for (std::size_t id : ids) {
if (arrPosition[id].x < 0) {
arrPosition[id].x = 0;
}
if (arrPosition[id].y < 0) {
arrPosition[id].y = 0;
}
if (arrPosition[id].x + arrCollisionRect[id].width > maxPercent) {
arrPosition[id].x = maxPercent - arrCollisionRect[id].width;
}
if (arrPosition[id].y + arrCollisionRect[id].height > maxPercent) {
arrPosition[id].y = maxPercent - arrCollisionRect[id].height;
}
}
}
static bool checkAllies(std::size_t fstId, std::size_t scdId)
{
Registry ®istry = Registry::getInstance();
Registry::components<Types::Player> players = registry.getComponents<Types::Player>();
Registry::components<Types::Enemy> enemies = registry.getComponents<Types::Enemy>();
Registry::components<Types::PlayerAllies> playerAllies =
registry.getComponents<Types::PlayerAllies>();
Registry::components<Types::EnemyAllies> enemyAllies = registry.getComponents<Types::EnemyAllies>();
if ((playerAllies.exist(fstId) && players.exist(scdId))
|| (playerAllies.exist(scdId) && players.exist(fstId))
|| (playerAllies.exist(fstId) && playerAllies.exist(scdId))
|| (players.exist(scdId) && players.exist(fstId))) {
return true;
}
if ((enemyAllies.exist(fstId) && enemies.exist(scdId))
|| (enemyAllies.exist(scdId) && enemies.exist(fstId))
|| (enemyAllies.exist(fstId) && enemyAllies.exist(scdId))
|| (enemies.exist(scdId) && enemies.exist(fstId))) {
return true;
}
return false;
}
#ifdef CLIENT
static void sendLifeUpdateToServer(std::size_t id, Registry::components<struct health_s> &arrHealth)
{
Registry::components<Types::Player> arrPlayer =
Registry::getInstance().getComponents<Types::Player>();
if (arrPlayer.exist(id)) {
Nitwork::NitworkClient::getInstance().addLifeUpdateMsg(arrPlayer[id].constId, arrHealth[id]);
}
}
#endif
static void giveDamages(std::size_t firstEntity, std::size_t secondEntity)
{
Registry::components<Types::Damage> arrDamage =
Registry::getInstance().getComponents<Types::Damage>();
Registry::components<struct health_s> arrHealth =
Registry::getInstance().getComponents<struct health_s>();
if (arrDamage.exist(firstEntity) && arrDamage[firstEntity].damage > 0) {
if (arrHealth.exist(secondEntity)) {
arrHealth[secondEntity].hp -= arrDamage[firstEntity].damage;
#ifdef CLIENT
sendLifeUpdateToServer(secondEntity, arrHealth);
#endif
}
}
}
static void checkSide(std::size_t firstEntity, std::size_t secondEntity)
{
if (checkAllies(firstEntity, secondEntity)) {
return;
}
giveDamages(firstEntity, secondEntity);
giveDamages(secondEntity, firstEntity);
}
static void checkCollisionEntity(
std::vector<size_t>::iterator itIds,
std::vector<std::size_t> &ids,
Registry::components<Types::Position> arrPosition,
Registry::components<Types::CollisionRect> arrCollisionRect)
{
std::size_t id = *itIds;
Types::Position entityPos = arrPosition[id];
Types::CollisionRect entityColl = arrCollisionRect[id];
itIds++;
while (itIds != ids.end()) {
if (arrCollisionRect.exist(*itIds)) {
Types::CollisionRect sndEntityRect = arrCollisionRect[*itIds];
Types::Position sndEntityPos = arrPosition[*itIds];
if (entityPos.x < sndEntityPos.x + sndEntityRect.width
&& entityPos.x + entityColl.width > sndEntityPos.x
&& entityPos.y < sndEntityPos.y + sndEntityRect.height
&& entityPos.y + entityColl.height > sndEntityPos.y) {
checkSide(id, *itIds);
}
}
itIds++;
}
}
void entitiesCollision(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry ®istry = Registry::getInstance();
Registry::components<Types::Position> arrPosition = registry.getComponents<Types::Position>();
Registry::components<Types::CollisionRect> arrCollisionRect =
registry.getComponents<Types::CollisionRect>();
std::vector<std::size_t> ids =
registry.getEntitiesByComponents({typeid(Types::CollisionRect), typeid(Types::Position)});
for (auto itIds = ids.begin(); itIds != ids.end(); itIds++) {
checkCollisionEntity(itIds, ids, arrPosition, arrCollisionRect);
}
}
const std::size_t moveTime = 20;
void moveEntities(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
Registry::components<Types::Velocity> arrVelocity =
Registry::getInstance().getComponents<Types::Velocity>();
Clock &clock = Registry::getInstance().getClock();
static std::size_t clockId = clock.create();
std::vector<std::size_t> ids = arrPosition.getExistingsId();
if (clock.elapsedMillisecondsSince(clockId) < moveTime) {
return;
}
for (auto &id : ids) {
if (arrVelocity.exist(id)) {
arrPosition[id].x += arrVelocity[id].speedX;
arrPosition[id].y += arrVelocity[id].speedY;
}
}
clock.restart(clockId);
}
void initEnemy(JsonType enemyType, bool setId, struct ::enemy_id_s enemyId)
{
std::vector<nlohmann::basic_json<>> enemyData =
Json::getInstance().getDataByJsonType("enemy", enemyType);
for (auto &elem : enemyData) {
#ifdef CLIENT
std::size_t id = Registry::getInstance().addEntity();
Types::Rect rect = {Types::Rect(Json::getInstance().getDataFromJson(elem, "rect"))};
Types::SpriteDatas enemy = {
Json::getInstance().getDataFromJson(elem, "spritePath"),
Json::getInstance().getDataFromJson(elem, "width"),
Json::getInstance().getDataFromJson(elem, "height"),
id,
LayerType::DEFAULTLAYER,
0
};
nlohmann::basic_json<> animRectData = Json::getInstance().getDataFromJson(elem, "animRect");
Types::AnimRect animRect = {
rect,
Json::getInstance().getDataFromJson(animRectData, "move").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "attack").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "dead").get<std::vector<Types::Rect>>()};
#else
Registry::getInstance().addEntity();
#endif
Types::Enemy enemyComp = (setId ? Types::Enemy {enemyId} : Types::Enemy {});
Types::Position position = {
Types::Position(Json::getInstance().getDataFromJson(elem, "position"))};
Types::CollisionRect collisionRect = {
Types::CollisionRect(Json::getInstance().getDataFromJson(elem, "collisionRect"))};
struct health_s healthComp = {Json::getInstance().getDataFromJson(elem, "health")};
Types::Damage damageComp = {Json::getInstance().getDataFromJson(elem, "damage")};
Types::Velocity velocity = {
Types::Velocity(Json::getInstance().getDataFromJson(elem, "velocity"))};
#ifdef CLIENT
Registry::getInstance().setToFrontLayers(id);
Registry::getInstance().getComponents<Types::Rect>().insertBack((rect));
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
Registry::getInstance().getComponents<Types::SpriteDatas>().insertBack(enemy);
#endif
Registry::getInstance().getComponents<Types::Position>().insertBack(position);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Velocity>().insertBack(velocity);
Registry::getInstance().getComponents<struct health_s>().insertBack(healthComp);
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<Types::Enemy>().insertBack(enemyComp);
}
}
void initWave(std::size_t managerId, std::size_t systemId)
{
static std::size_t enemyNumber = 5;
const std::size_t spawnDelay = 2;
Clock &clock = Registry::getInstance().getClock();
static std::size_t clockId = clock.create(true);
if (clock.elapsedSecondsSince(clockId) > spawnDelay) {
initEnemy(JsonType::DEFAULT_ENEMY);
enemyNumber--;
clock.restart(clockId);
}
if (enemyNumber <= 0) {
SystemManagersDirector::getInstance().getSystemManager(managerId).removeSystem(systemId);
}
}
void checkDestroyAfterDeathCallBack(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry ®istry = Registry::getInstance();
auto deadList = registry.getComponents<Types::Dead>();
auto deadIdList = deadList.getExistingsId();
Clock &clock = registry.getClock();
std::size_t decrease = 0;
for (auto id : deadIdList) {
auto tmpId = id - decrease;
Types::Dead &dead = deadList[tmpId];
if (static_cast<int>(dead.clockId) > -1
&& clock.elapsedMillisecondsSince(dead.clockId) > dead.timeToWait) {
registry.removeEntity(tmpId);
decrease++;
}
}
}
static void
executeDeathFunction(std::size_t id, Registry::components<Types::Dead> arrDead, std::size_t &decrease)
{
if (arrDead.exist(id) && arrDead[id].deathFunction != std::nullopt) {
Types::Dead &deadComp = arrDead[id];
if (!deadComp.launched) {
deadComp.deathFunction.value()(id);
deadComp.clockId = Registry::getInstance().getClock().create();
deadComp.launched = true;
}
} else {
Registry::getInstance().removeEntity(id);
decrease++;
}
}
static void sendEnemyDeath(std::size_t arrId)
{
auto &arrEnemies = Registry::getInstance().getComponents<Types::Enemy>();
if (!arrEnemies.exist(arrId)) {
return;
}
#ifdef CLIENT
Nitwork::NitworkClient::getInstance().addEnemyDeathMsg(arrEnemies[arrId].getConstId().id);
#else
Nitwork::NitworkServer::getInstance().addEnemyDeathMessage(arrEnemies[arrId].getConstId().id);
#endif
}
void deathChecker(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<struct health_s> arrHealth =
Registry::getInstance().getComponents<struct health_s>();
Registry::components<Types::Dead> arrDead = Registry::getInstance().getComponents<Types::Dead>();
std::size_t decrease = 0;
std::vector<std::size_t> ids = arrHealth.getExistingsId();
for (auto &id : ids) {
auto tmpId = id - decrease;
if (arrHealth.exist(tmpId) && arrHealth[tmpId].hp <= 0) {
sendEnemyDeath(tmpId);
executeDeathFunction(tmpId, arrDead, decrease);
}
}
}
constexpr float outsideWindowTopLeft = -25.0F;
constexpr float outsideWindowBotRigth = 125.0F;
static bool isOutsideWindow(const Types::Position &pos)
{
if (pos.x < outsideWindowTopLeft || pos.x > outsideWindowBotRigth || pos.y < outsideWindowTopLeft
|| pos.y > outsideWindowBotRigth) {
return (true);
}
return (false);
}
void destroyOutsideWindow(std::size_t /*unused*/, std::size_t /*unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
Registry::components<Types::Position> arrPosition =
Registry::getInstance().getComponents<Types::Position>();
#ifdef CLIENT
Registry::components<Types::Parallax> arrParallax =
Registry::getInstance().getComponents<Types::Parallax>();
#endif
std::vector<std::size_t> ids =
Registry::getInstance().getEntitiesByComponents({typeid(Types::Position)});
std::size_t decrease = 0;
for (auto &id : ids) {
auto tmpId = id - decrease;
#ifdef CLIENT
bool isNotParallax = !arrParallax.exist(tmpId);
#else
bool isNotParallax = true;
#endif
if (isNotParallax && isOutsideWindow(arrPosition[tmpId])) {
Registry::getInstance().removeEntity(tmpId);
decrease++;
}
}
}
void initPlayer(JsonType playerType)
{
#ifdef CLIENT
std::size_t id = Registry::getInstance().addEntity();
#else
Registry::getInstance().addEntity();
#endif
Types::Player playerComp = {};
Types::Dead deadComp = {Json::getInstance().getDataByVector({"player", "deadTime"}, playerType)};
struct health_s healthComp = {
Json::getInstance().getDataByVector({"player", "health"}, playerType)};
Types::Damage damageComp = {Json::getInstance().getDataByVector({"player", "damage"}, playerType)};
#ifdef CLIENT
Types::SpriteDatas playerDatas(
Json::getInstance().getDataByVector({"player", "spritePath"}, playerType),
Json::getInstance().getDataByVector({"player", "width"}, playerType),
Json::getInstance().getDataByVector({"player", "height"}, playerType),
id,
FRONTLAYER,
static_cast<std::size_t>(FRONT));
Types::Rect rect = {
Types::Rect(Json::getInstance().getDataByVector({"player", "rect"}, playerType))};
nlohmann::basic_json<> animRectData =
Json::getInstance().getDataByVector({"player", "animRect"}, playerType);
Types::AnimRect animRect = {
rect,
Json::getInstance().getDataFromJson(animRectData, "move").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "attack").get<std::vector<Types::Rect>>(),
Json::getInstance().getDataFromJson(animRectData, "dead").get<std::vector<Types::Rect>>()};
#endif
Types::Position position = {
Types::Position(Json::getInstance().getDataByVector({"player", "position"}, playerType))};
Types::CollisionRect collisionRect = {Types::CollisionRect(
Json::getInstance().getDataByVector({"player", "collisionRect"}, playerType))};
#ifdef CLIENT
Registry::getInstance().getComponents<Types::Rect>().insertBack(rect);
Registry::getInstance().getComponents<Types::AnimRect>().insertBack(animRect);
Registry::getInstance().getComponents<Types::SpriteDatas>().insertBack(playerDatas);
#endif
Registry::getInstance().getComponents<Types::Position>().insertBack(position);
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Player>().insertBack(playerComp);
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<struct health_s>().insertBack(healthComp);
Registry::getInstance().getComponents<Types::Dead>().insertBack(deadComp);
}
void createMissile(Types::Position &pos, Types::Missiles &typeOfMissile)
{
#ifdef CLIENT
std::size_t entityId = Registry::getInstance().addEntity();
#else
Registry::getInstance().addEntity();
#endif
constexpr float bulletWidth = 5.0F;
constexpr float bulletHeight = 5.0F;
Types::CollisionRect collisionRect = {bulletWidth, bulletHeight};
Types::Velocity velocity = {0.7F, 0.0F};
Types::Missiles missileType = typeOfMissile;
Types::Dead deadComp = {};
Types::PlayerAllies playerAlliesComp = {};
Types::Position position = {pos.x, pos.y};
#ifdef CLIENT
const std::string bulletPath = "assets/R-TypeSheet/r-typesheet1.gif";
Types::Rect spriteRect = {200, 121, 32, 10};
Types::SpriteDatas bulletDatas(
bulletPath,
bulletWidth,
bulletHeight,
entityId,
FRONTLAYER,
static_cast<std::size_t>(FRONT));
#endif
struct health_s healthComp = {1};
Types::Damage damageComp = {10};
Registry::getInstance().getComponents<Types::Position>().insertBack(position);
#ifdef CLIENT
Registry::getInstance().getComponents<Types::SpriteDatas>().insertBack(bulletDatas);
Registry::getInstance().getComponents<Types::Rect>().insertBack(spriteRect);
#endif
Registry::getInstance().getComponents<Types::CollisionRect>().insertBack(collisionRect);
Registry::getInstance().getComponents<Types::Missiles>().insertBack(missileType);
Registry::getInstance().getComponents<Types::PlayerAllies>().insertBack(playerAlliesComp);
Registry::getInstance().getComponents<Types::Velocity>().insertBack(velocity);
Registry::getInstance().getComponents<struct health_s>().insertBack(healthComp);
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<Types::Dead>().insertBack(deadComp);
}
std::vector<std::function<void(std::size_t, std::size_t)>> getECSSystems()
{
return {
windowCollision,
checkDestroyAfterDeathCallBack,
entitiesCollision,
destroyOutsideWindow,
deathChecker,
moveEntities};
}
} // namespace Systems