-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientNetwork.cpp
More file actions
218 lines (192 loc) · 9.46 KB
/
ClientNetwork.cpp
File metadata and controls
218 lines (192 loc) · 9.46 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
#include "ClientNetwork.hpp"
#include <algorithm>
#include "ECSCustomTypes.hpp"
#include "Json.hpp"
#include "NitworkClient.hpp"
#include "Registry.hpp"
#include "SceneManager.hpp"
#include "SystemManagersDirector.hpp"
#include "Systems.hpp"
namespace Systems {
void receiveLifeUpdate(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
auto msg = std::any_cast<struct msgLifeUpdate_s>(any);
Registry ®istry = Registry::getInstance();
Registry::components<struct health_s> arrHealth = registry.getComponents<struct health_s>();
std::vector<std::size_t> ids = Registry::getInstance().getEntitiesByComponents(
{typeid(struct health_s), typeid(Types::Player)});
if (ids.empty()) {
return;
}
struct health_s &life = arrHealth[ids[0]];
if (life.hp != msg.life.hp) {
life.hp = msg.life.hp;
}
}
void receiveEnemyDeath(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto enemyDeath = std::any_cast<struct msgEnemyDeath_s>(any);
Registry::components<Types::Enemy> enemies = Registry::getInstance().getComponents<Types::Enemy>();
std::vector<std::size_t> ids = enemies.getExistingsId();
for (auto id : ids) {
if (enemies[id].getConstId().id == enemyDeath.enemyId.id) {
Logger::debug("ROLLBACK REMOVE ENEMY !!!!!");
Registry::getInstance().removeEntity(id);
return;
}
}
}
void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto wave = std::any_cast<struct msgStartWave_s>(any);
Types::Enemy::setEnemyNb(wave.enemyNb);
SystemManagersDirector::getInstance()
.getSystemManager(static_cast<std::size_t>(Scene::SystemManagers::GAME))
.addSystem(initWave);
Logger::info("Wave started");
}
void receivePlayerInit(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto playerInit = std::any_cast<struct msgPlayerInit_s>(any);
Logger::info("Your player id is: " + std::to_string(playerInit.playerId));
initPlayer(JsonType::DEFAULT_PLAYER, playerInit.playerId);
}
void receiveNewEnemy(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto newEnemy = std::any_cast<struct msgNewEnemy_s>(any);
Logger::debug("ROLLBACK RECREATE ENEMY !!!!!");
initEnemy(JsonType::DEFAULT_ENEMY, true, newEnemy.enemyInfos.id);
Types::Position pos = {
static_cast<float>(newEnemy.enemyInfos.pos.x),
static_cast<float>(newEnemy.enemyInfos.pos.y)};
struct health_s hp = newEnemy.enemyInfos.life;
Registry::getInstance().getComponents<Types::Position>().insertBack(pos);
Registry::getInstance().getComponents<struct health_s>().insertBack(hp);
}
void receiveNewAllie(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto newAllie = std::any_cast<struct msgNewAllie_s>(any);
Logger::fatal("NEW ALLIE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n\n\n\n\n\n\n\n");
initPlayer(JsonType::DEFAULT_PLAYER, newAllie.playerId, true);
}
void receiveRelativePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const struct msgPositionRelativeBroadcast_s &msg = std::any_cast<struct msgPositionRelativeBroadcast_s>(any);
Types::Position position = {
static_cast<float>(msg.pos.x),
static_cast<float>(msg.pos.y)};
auto &arrPos = Registry::getInstance().getComponents<Types::Position>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto ids = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::OtherPlayer)});
auto otherPlayer = std::find_if(ids.begin(), ids.end(), [&arrOtherPlayers, &msg](std::size_t id) {
return arrOtherPlayers[id].constId == msg.playerId;
});
if (otherPlayer == ids.end()) {
return;
}
arrPos[*otherPlayer] += position;
}
void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
constexpr std::size_t delay = 10;
static auto clockId = Registry::getInstance().getClock().create();
static std::pair<std::size_t, Types::Position> positionPlayerCached;
Registry ®istry = Registry::getInstance();
if (registry.getClock().elapsedMillisecondsSince(clockId) < delay) {
return;
}
auto ids = registry.getEntitiesByComponents({typeid(Types::Position), typeid(Types::Player)});
auto arrPosition = registry.getComponents<Types::Position>();
if (ids.empty()) {
return;
}
if (positionPlayerCached.first != ids[0]) {
positionPlayerCached.first = ids[0];
positionPlayerCached.second = arrPosition[ids[0]];
}
auto &posCached = positionPlayerCached.second;
const auto &pos = arrPosition[positionPlayerCached.first];
if (pos.x != posCached.x || pos.y != posCached.y) {
registry.getClock().decreaseMilliseconds(clockId, delay);
struct position_relative_s msg = {
.x = static_cast<char>(static_cast<int>(pos.x - posCached.x)),
.y = static_cast<char>(static_cast<int>(pos.y - posCached.y)),
};
Logger::info("send pos relative\n\n" + std::to_string(msg.x) + " " + std::to_string(msg.y) + "\n\n");
posCached.x = pos.x;
posCached.y = pos.y;
Nitwork::NitworkClient::getInstance().addPositionRelativeMsg(msg);
}
}
void sendPositionAbsolute(std::size_t /* unused */, std::size_t /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
constexpr std::size_t delay = 1;
static auto clockId = Registry::getInstance().getClock().create();
Registry ®istry = Registry::getInstance();
static struct position_absolute_s position = {0, 0};
if (registry.getClock().elapsedSecondsSince(clockId) < delay) {
return;
}
auto ids = registry.getEntitiesByComponents({typeid(Types::Position), typeid(Types::Player)});
auto arrPosition = registry.getComponents<Types::Position>();
if (ids.empty()) {
return;
}
struct position_absolute_s msg = {
.x = static_cast<int>(arrPosition[ids[0]].x),
.y = static_cast<int>(arrPosition[ids[0]].y),
};
if (position.x == msg.x && position.y == msg.y) {
return;
}
registry.getClock().decreaseSeconds(clockId, delay);
position = msg;
Nitwork::NitworkClient::getInstance().addPositionAbsoluteMsg(msg);
}
void receiveNewBullet(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const struct msgNewBullet_s &msgNewBullet = std::any_cast<struct msgNewBullet_s>(any);
struct Types::Position position = {
static_cast<float>(msgNewBullet.pos.x),
static_cast<float>(msgNewBullet.pos.y),
};
struct Types::Missiles missileType = {static_cast<missileTypes_e>(msgNewBullet.missileType)};
Systems::createMissile(position, missileType);
}
void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const struct msgPositionAbsoluteBroadcast_s &msg =
std::any_cast<struct msgPositionAbsoluteBroadcast_s>(any);
struct Types::Position position = {
static_cast<float>(msg.pos.x),
static_cast<float>(msg.pos.y),
};
auto &arrPos = Registry::getInstance().getComponents<Types::Position>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto ids = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::OtherPlayer)});
auto otherPlayer = std::find_if(ids.begin(), ids.end(), [&arrOtherPlayers, &msg](std::size_t id) {
return arrOtherPlayers[id].constId == msg.playerId;
});
if (otherPlayer == ids.end()) {
return;
}
arrPos[*otherPlayer] = position;
}
std::vector<std::function<void(std::size_t, std::size_t)>> getNetworkSystems()
{
return {sendPositionRelative, sendPositionAbsolute};
}
} // namespace Systems