Skip to content

Commit dc96705

Browse files
authored
Merge pull request #119 from maelbecel/Merge/main_server
Merge/main server
2 parents 4a5264b + 34cd320 commit dc96705

File tree

31 files changed

+580
-345
lines changed

31 files changed

+580
-345
lines changed

Client/src/Layer/RTypeLayer.cpp

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace RType {
5858
ApplicationCommandLineArgs commandLine = Application::Get().GetSpecification().CommandLineArgs;
5959

6060
// TODO: Temp port ./r-type_client {port} {ip} {port}
61-
int port = 8083; // Default port
61+
int port = 8082; // Default port
6262
if (commandLine.Count > 3) {
6363
port = std::stoi(commandLine[3]);
6464

@@ -71,11 +71,9 @@ namespace RType {
7171
}
7272

7373
void RTypeLayer::ConnectToServer(int port, std::string ip, int serverPort) {
74-
_Network = CreateScope<Network::Network>(_WorldNetwork, _IOContextManager, port);
75-
74+
(void)port;
7675
_Network->Loop();
7776
_Network->SendAskConnect(ip, (short)serverPort);
78-
// TODO: change ip and port when the server is on a different machine
7977
}
8078

8179
void RTypeLayer::OnAttach() {
@@ -84,52 +82,26 @@ namespace RType {
8482
int port = GetPort();
8583
std::string ip = GetIp();
8684
int serverPort = GetServerPort();
85+
_Network = CreateScope<Network::Network>(_WorldNetwork, _IOContextManager, port);
8786

8887
if (port == -1)
8988
return;
9089

91-
ConnectToServer(port, ip, serverPort);
92-
9390
// Create world
9491
CurrentScene = GAME;
9592

96-
// Scenes[MENU] = CreateRef<Scene>();
97-
// Scenes[MENU]->RegisterSystem(new AnimationSystem());
98-
// Scenes[MENU]->RegisterSystem(new MovingSystem(1.5f));
99-
// Scenes[MENU]->OnViewportResize(Application::Get().GetWindow().GetWidth(),
100-
// Application::Get().GetWindow().GetHeight());
101-
102-
// RType::EntityEventSubscriber *subscribe = new RType::EntityEventSubscriber(_Network);
10393
CollisionSystem *collisionSystem = new CollisionSystem();
10494

10595
Scenes[GAME] = CreateRef<Scene>();
10696
Scenes[GAME]->RegisterSystem(new AnimationSystem());
10797
Scenes[GAME]->RegisterSystem(new MovingSystem(1.5f));
10898
Scenes[GAME]->RegisterSystem(collisionSystem);
109-
// Scenes[GAME]->Subscribe<Events::OnEntityCreated>(subscribe);
110-
// Scenes[GAME]->Subscribe<Events::OnEntityDestroyed>(subscribe);
99+
Scenes[GAME]->RegisterSystem(new ClockSystem());
100+
111101
Scenes[GAME]->Subscribe<Exodia::Events::OnCollisionEntered>(collisionSystem);
112102
Scenes[GAME]->OnViewportResize(Application::Get().GetWindow().GetWidth(),
113103
Application::Get().GetWindow().GetHeight());
114104

115-
// Entity *music = Scenes[GAME]->CreateEntity("Background");
116-
117-
// music->AddComponent<MusicComponent>(124013371145915, 0.5f, true);
118-
119-
// Entity *text = Scenes[GAME]->CreateEntity("Text");
120-
121-
// auto txt = text->AddComponent<TextRendererComponent>("\"From the dark regions of space they\n came...Waging
122-
// war upon us.\n One saviour stood his ground while all\n others were crushed under the alien\n assaul t...\n\n
123-
// The Prototype Markl .\"");
124-
125-
// txt.Get().Font = UUID(4521854574125);
126-
// txt.Get().Font = UUID(45121874124124);
127-
128-
// auto transform = text->GetComponent<TransformComponent>();
129-
130-
// transform.Get().Translation = {-10.0f, 3.0f, 0.0f};
131-
// transform.Get().Scale = {0.50f, 0.50f, 1.0f};
132-
133105
_Network->SetWorld(Scenes[CurrentScene]->GetWorldPtr());
134106

135107
// TODO: Temp code
@@ -143,41 +115,22 @@ namespace RType {
143115
camera.Camera.SetViewportSize(Application::Get().GetWindow().GetWidth(),
144116
Application::Get().GetWindow().GetHeight());
145117

146-
/*RType::EntityEventSubscriber *subscribe = new RType::EntityEventSubscriber(*_Network);
147-
148-
Scenes[GAME]->Subscribe<Events::OnEntityCreated>(subscribe);
149-
Scenes[GAME]->Subscribe<Events::OnEntityDestroyed>(subscribe);*/
150-
151-
/* Removing rigid body for static camera
152-
auto body_camera = cameraEntity->AddComponent<RigidBody2DComponent>();
153-
body_camera.Get().Type = RigidBody2DComponent::BodyType::Dynamic;
154-
body_camera.Get().Mass = 0.0f;
155-
body_camera.Get().GravityScale = 0.0f;
156-
body_camera.Get().Velocity = glm::vec2{ 1.5f, 0.0f };
157-
*/
158-
159-
// Create the entities
160-
// TODO: Ask server for playerID
161-
// int playerID = 0;
162-
// Entity *entity = Scenes[GAME]->CreateEntity("Player_" + std::to_string(playerID));
163-
// entity->AddComponent<ScriptComponent>().Get().Bind("Player");
164-
165-
// Create pata-pata
166-
// Entity *patata = Scenes[GAME]->CreateEntity("Pata-pata");
167-
// patata->AddComponent<ScriptComponent>().Get().Bind("PataPata");
168-
169-
// Create stars
170-
// CreateStars(Scenes);
171118
for (int i = 0; i < 60; i++) {
172119
Entity *star = Scenes[GAME]->CreateEntity("Star" + std::to_string(i));
173120
star->AddComponent<ScriptComponent>().Get().Bind("Star");
174121
}
175122

176123
// Create the camera
177124
Scenes[CurrentScene]->OnRuntimeStart();
125+
126+
ConnectToServer(port, ip, serverPort);
178127
}
179128

180-
void RTypeLayer::OnDetach() { EXODIA_PROFILE_FUNCTION(); }
129+
void RTypeLayer::OnDetach() {
130+
EXODIA_PROFILE_FUNCTION();
131+
_Network->SendDisconnect();
132+
EXODIA_CORE_ERROR("RTypeLayer::OnDetach()");
133+
}
181134

182135
void RTypeLayer::OnUpdate(Exodia::Timestep ts) {
183136
EXODIA_PROFILE_FUNCTION();
@@ -193,6 +146,11 @@ namespace RType {
193146
if (CurrentScene == GAME) {
194147
};
195148

149+
_packetTimer += ts;
150+
if (_packetTimer >= 1) {
151+
_Network->SendPacketInfo();
152+
_packetTimer = 0;
153+
}
196154
// Update the world
197155
Scenes[CurrentScene]->OnUpdateRuntime(ts);
198156
}
@@ -218,16 +176,27 @@ namespace RType {
218176
TagComponent &tc = tag.Get();
219177

220178
std::ostringstream oss;
221-
oss << _Network->GetId();
179+
oss << _Network->GetIdPlayer();
222180
std::string player = "Player_" + oss.str();
223181

224182
if ((tc.Tag.compare(player) == 0) && sc.Instance != nullptr) {
225-
// sc.Instance->OnKeyPressed(key);
183+
sc.Instance->OnKeyPressed(key);
226184

227185
_Network->SendEvent(false, key, true);
228186
}
229187
(void)entity;
230188
});
189+
if (key == Key::ESCAPE) {
190+
NetworkInfo info = _Network->GetNetworkInfo();
191+
EXODIA_CORE_ERROR("PACKET info");
192+
EXODIA_CORE_ERROR("Send packet: {0}", info.sendPacket);
193+
EXODIA_CORE_ERROR("Received packet: {0}", info.receivedPacket);
194+
EXODIA_CORE_ERROR("KiloByte sent: {0}", info.kiloByteSent);
195+
EXODIA_CORE_ERROR("KiloByte received: {0}", info.kiloByteReceived);
196+
EXODIA_CORE_ERROR("Packet loss sent: {0}", info.sendPacketLost);
197+
EXODIA_CORE_ERROR("Packet loss received: {0}", info.receivePacketLost);
198+
EXODIA_CORE_ERROR("PING sent: {0}", info.ping);
199+
}
231200
Scenes[CurrentScene]->GetWorld().UnlockMutex();
232201

233202
return true;
@@ -243,7 +212,7 @@ namespace RType {
243212
TagComponent &tc = tag.Get();
244213

245214
std::ostringstream oss;
246-
oss << _Network->GetId();
215+
oss << _Network->GetIdPlayer();
247216
std::string player = "Player_" + oss.str();
248217

249218
if ((tc.Tag.compare(player) == 0) && sc.Instance != nullptr) {
@@ -264,4 +233,4 @@ namespace RType {
264233

265234
return true;
266235
}
267-
}; // namespace RType
236+
}; // namespace RType

Client/src/Layer/RTypeLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace RType {
5555
private:
5656
// TODO: WARNING: This is a temporary solution
5757
World *_WorldNetwork;
58-
58+
float _packetTimer = 0;
5959
Network::IOContextManager _IOContextManager;
6060
Scope<Network::Network> _Network;
6161
};

GameEngine/src/Exodia/ECS/Component/DefaultComponents/RendererComponents.hpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,36 +99,27 @@ namespace Exodia {
9999
virtual Buffer SerializeData() override {
100100
try {
101101
Buffer buffer(sizeof(Color) + sizeof(TilingFactor) + sizeof(bool));
102-
size_t offset = 0;
103-
104-
std::memcpy(buffer.Data, &Color, sizeof(Color));
105-
offset += sizeof(Color);
106-
std::memcpy(buffer.Data + offset, &TilingFactor, sizeof(TilingFactor));
107-
offset += sizeof(TilingFactor);
108102

103+
buffer.Write(&Color, sizeof(Color));
104+
buffer.Write(&TilingFactor, sizeof(TilingFactor));
109105
bool hasTexture = (Texture != nullptr);
110-
111-
std::memcpy(buffer.Data, &hasTexture, sizeof(bool));
112-
offset += sizeof(bool);
106+
buffer.Write(&hasTexture, sizeof(bool));
113107

114108
if (Texture) {
115109
buffer.Resize(buffer.Size + sizeof(Texture->GetAssetHandle()) + sizeof(Texture->GetCoords()) +
116110
sizeof(Texture->GetTextureCellSize()) + sizeof(Texture->GetTextureSpriteSize()));
117111

118112
Exodia::AssetHandle assetHandle = Texture->GetAssetHandle();
119113

120-
std::memcpy(buffer.Data + offset, &assetHandle, sizeof(Texture->GetAssetHandle()));
121-
offset += sizeof(Texture->GetAssetHandle());
122-
std::memcpy(buffer.Data + offset, &(Texture->GetCoords()), sizeof(Texture->GetCoords()));
123-
offset += sizeof(Texture->GetCoords());
124-
std::memcpy(buffer.Data + offset, &Texture->GetTextureCellSize(),
125-
sizeof(Texture->GetTextureCellSize()));
126-
offset += sizeof(Texture->GetTextureCellSize());
127-
std::memcpy(buffer.Data + offset, &Texture->GetTextureSpriteSize(),
128-
sizeof(Texture->GetTextureSpriteSize()));
129-
offset += sizeof(Texture->GetTextureSpriteSize());
114+
buffer.Write(&assetHandle, sizeof(assetHandle));
115+
buffer.Write(&Texture->GetCoords(), sizeof(Texture->GetCoords()));
116+
buffer.Write(&Texture->GetTextureCellSize(), sizeof(Texture->GetTextureCellSize()));
117+
buffer.Write(&Texture->GetTextureSpriteSize(), sizeof(Texture->GetTextureSpriteSize()));
130118
}
131-
119+
for (size_t i = 0; i < buffer.Size; i++) {
120+
std::cout << (int)buffer.Data[i] << " ";
121+
}
122+
std::cout << std::endl;
132123
return buffer;
133124
} catch (std::exception &e) {
134125
EXODIA_CORE_WARN("SpriteRendererComponent serialization failed: {0}", e.what());
@@ -149,11 +140,19 @@ namespace Exodia {
149140
Memcopy(&TilingFactor, data.Data + offset, sizeof(TilingFactor));
150141
offset += sizeof(TilingFactor);
151142

152-
std::memcpy(&hasTexture, data.Data, sizeof(bool));
143+
for (size_t i = offset; i < offset + sizeof(bool); i++) {
144+
std::cout << (int)data.Data[i] << " ";
145+
}
146+
std::cout << std::endl;
147+
if (data.Data[offset] == 1)
148+
hasTexture = true;
149+
else
150+
hasTexture = false;
153151
offset += sizeof(bool);
154152

155-
if (!hasTexture)
153+
if (!hasTexture) {
156154
return;
155+
}
157156
Exodia::AssetHandle assetHandle;
158157
glm::vec2 coords;
159158
glm::vec2 cellSize;
@@ -172,6 +171,7 @@ namespace Exodia {
172171
offset += sizeof(spriteSize);
173172

174173
Texture = SubTexture2D::CreateFromCoords(assetHandle, coords, cellSize, spriteSize);
174+
175175
} catch (std::exception &e) {
176176
EXODIA_CORE_WARN("SpriteRendererComponent deserialization failed: {0}", e.what());
177177
}

GameEngine/src/Exodia/Renderer/Renderer/Renderer2D.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ namespace Exodia {
113113
delete[] _Data->QuadVertexBufferBase;
114114
_Data->QuadVertexBufferBase = nullptr;
115115
}
116+
if (_Data && _Data->CircleVertexBufferBase != nullptr) {
117+
delete[] _Data->CircleVertexBufferBase;
118+
_Data->CircleVertexBufferBase = nullptr;
119+
}
120+
if (_Data && _Data->LineVertexBufferBase != nullptr) {
121+
delete[] _Data->LineVertexBufferBase;
122+
_Data->LineVertexBufferBase = nullptr;
123+
}
116124

117125
_Data = nullptr;
118126
}

GameEngine/src/Exodia/Scene/Scene/Scene.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,44 @@ namespace Exodia {
289289
}
290290
});
291291
_World->UnlockMutex();
292+
293+
// _World->LockMutex();
294+
// _World->ForEach<TransformComponent, BoxCollider2DComponent>([&](Entity *entity, auto transform, auto
295+
// collider) {
296+
// auto &tc = transform.Get();
297+
// auto &cc = collider.Get();
298+
299+
// Renderer2D::DrawRect(tc.GetTransform(), glm::vec4(1.0f, 0.5f, 0.0f, 1.0f));
300+
301+
// (void)cc;
302+
// (void)entity;
303+
// });
304+
// _World->UnlockMutex();
305+
306+
// _World->LockMutex();
307+
// _World->ForEach<TransformComponent, ParentComponent>([&](Entity *entity, auto transform, auto script) {
308+
// auto &tc = transform.Get();
309+
// auto &cc = script.Get();
310+
311+
// Renderer2D::DrawCircle(tc.GetTransform(), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f), 0.1f, 0.005f);
312+
313+
// (void)cc;
314+
// (void)entity;
315+
// });
316+
// _World->UnlockMutex();
317+
318+
// _World->LockMutex();
319+
// _World->ForEach<TransformComponent, CircleCollider2DComponent>([&](Entity *entity, auto transform, auto
320+
// collider) {
321+
// auto &tc = transform.Get();
322+
// auto &cc = collider.Get();
323+
324+
// Renderer2D::DrawCircle(tc.GetTransform(), glm::vec4(1.0f, 0.5f, 0.0f, 1.0f), 0.1f, 0.005f);
325+
326+
// (void)cc;
327+
// (void)entity;
328+
// });
329+
// _World->UnlockMutex();
292330
}
293331

294332
///////////////////////

0 commit comments

Comments
 (0)