Skip to content

Commit

Permalink
Merge pull request #119 from maelbecel/Merge/main_server
Browse files Browse the repository at this point in the history
Merge/main server
  • Loading branch information
PharaEthan authored Jan 14, 2024
2 parents 4a5264b + 34cd320 commit dc96705
Show file tree
Hide file tree
Showing 31 changed files with 580 additions and 345 deletions.
95 changes: 32 additions & 63 deletions Client/src/Layer/RTypeLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace RType {
ApplicationCommandLineArgs commandLine = Application::Get().GetSpecification().CommandLineArgs;

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

Expand All @@ -71,11 +71,9 @@ namespace RType {
}

void RTypeLayer::ConnectToServer(int port, std::string ip, int serverPort) {
_Network = CreateScope<Network::Network>(_WorldNetwork, _IOContextManager, port);

(void)port;
_Network->Loop();
_Network->SendAskConnect(ip, (short)serverPort);
// TODO: change ip and port when the server is on a different machine
}

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

if (port == -1)
return;

ConnectToServer(port, ip, serverPort);

// Create world
CurrentScene = GAME;

// Scenes[MENU] = CreateRef<Scene>();
// Scenes[MENU]->RegisterSystem(new AnimationSystem());
// Scenes[MENU]->RegisterSystem(new MovingSystem(1.5f));
// Scenes[MENU]->OnViewportResize(Application::Get().GetWindow().GetWidth(),
// Application::Get().GetWindow().GetHeight());

// RType::EntityEventSubscriber *subscribe = new RType::EntityEventSubscriber(_Network);
CollisionSystem *collisionSystem = new CollisionSystem();

Scenes[GAME] = CreateRef<Scene>();
Scenes[GAME]->RegisterSystem(new AnimationSystem());
Scenes[GAME]->RegisterSystem(new MovingSystem(1.5f));
Scenes[GAME]->RegisterSystem(collisionSystem);
// Scenes[GAME]->Subscribe<Events::OnEntityCreated>(subscribe);
// Scenes[GAME]->Subscribe<Events::OnEntityDestroyed>(subscribe);
Scenes[GAME]->RegisterSystem(new ClockSystem());

Scenes[GAME]->Subscribe<Exodia::Events::OnCollisionEntered>(collisionSystem);
Scenes[GAME]->OnViewportResize(Application::Get().GetWindow().GetWidth(),
Application::Get().GetWindow().GetHeight());

// Entity *music = Scenes[GAME]->CreateEntity("Background");

// music->AddComponent<MusicComponent>(124013371145915, 0.5f, true);

// Entity *text = Scenes[GAME]->CreateEntity("Text");

// auto txt = text->AddComponent<TextRendererComponent>("\"From the dark regions of space they\n came...Waging
// war upon us.\n One saviour stood his ground while all\n others were crushed under the alien\n assaul t...\n\n
// The Prototype Markl .\"");

// txt.Get().Font = UUID(4521854574125);
// txt.Get().Font = UUID(45121874124124);

// auto transform = text->GetComponent<TransformComponent>();

// transform.Get().Translation = {-10.0f, 3.0f, 0.0f};
// transform.Get().Scale = {0.50f, 0.50f, 1.0f};

_Network->SetWorld(Scenes[CurrentScene]->GetWorldPtr());

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

/*RType::EntityEventSubscriber *subscribe = new RType::EntityEventSubscriber(*_Network);
Scenes[GAME]->Subscribe<Events::OnEntityCreated>(subscribe);
Scenes[GAME]->Subscribe<Events::OnEntityDestroyed>(subscribe);*/

/* Removing rigid body for static camera
auto body_camera = cameraEntity->AddComponent<RigidBody2DComponent>();
body_camera.Get().Type = RigidBody2DComponent::BodyType::Dynamic;
body_camera.Get().Mass = 0.0f;
body_camera.Get().GravityScale = 0.0f;
body_camera.Get().Velocity = glm::vec2{ 1.5f, 0.0f };
*/

// Create the entities
// TODO: Ask server for playerID
// int playerID = 0;
// Entity *entity = Scenes[GAME]->CreateEntity("Player_" + std::to_string(playerID));
// entity->AddComponent<ScriptComponent>().Get().Bind("Player");

// Create pata-pata
// Entity *patata = Scenes[GAME]->CreateEntity("Pata-pata");
// patata->AddComponent<ScriptComponent>().Get().Bind("PataPata");

// Create stars
// CreateStars(Scenes);
for (int i = 0; i < 60; i++) {
Entity *star = Scenes[GAME]->CreateEntity("Star" + std::to_string(i));
star->AddComponent<ScriptComponent>().Get().Bind("Star");
}

// Create the camera
Scenes[CurrentScene]->OnRuntimeStart();

ConnectToServer(port, ip, serverPort);
}

void RTypeLayer::OnDetach() { EXODIA_PROFILE_FUNCTION(); }
void RTypeLayer::OnDetach() {
EXODIA_PROFILE_FUNCTION();
_Network->SendDisconnect();
EXODIA_CORE_ERROR("RTypeLayer::OnDetach()");
}

void RTypeLayer::OnUpdate(Exodia::Timestep ts) {
EXODIA_PROFILE_FUNCTION();
Expand All @@ -193,6 +146,11 @@ namespace RType {
if (CurrentScene == GAME) {
};

_packetTimer += ts;
if (_packetTimer >= 1) {
_Network->SendPacketInfo();
_packetTimer = 0;
}
// Update the world
Scenes[CurrentScene]->OnUpdateRuntime(ts);
}
Expand All @@ -218,16 +176,27 @@ namespace RType {
TagComponent &tc = tag.Get();

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

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

_Network->SendEvent(false, key, true);
}
(void)entity;
});
if (key == Key::ESCAPE) {
NetworkInfo info = _Network->GetNetworkInfo();
EXODIA_CORE_ERROR("PACKET info");
EXODIA_CORE_ERROR("Send packet: {0}", info.sendPacket);
EXODIA_CORE_ERROR("Received packet: {0}", info.receivedPacket);
EXODIA_CORE_ERROR("KiloByte sent: {0}", info.kiloByteSent);
EXODIA_CORE_ERROR("KiloByte received: {0}", info.kiloByteReceived);
EXODIA_CORE_ERROR("Packet loss sent: {0}", info.sendPacketLost);
EXODIA_CORE_ERROR("Packet loss received: {0}", info.receivePacketLost);
EXODIA_CORE_ERROR("PING sent: {0}", info.ping);
}
Scenes[CurrentScene]->GetWorld().UnlockMutex();

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

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

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

return true;
}
}; // namespace RType
}; // namespace RType
2 changes: 1 addition & 1 deletion Client/src/Layer/RTypeLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace RType {
private:
// TODO: WARNING: This is a temporary solution
World *_WorldNetwork;

float _packetTimer = 0;
Network::IOContextManager _IOContextManager;
Scope<Network::Network> _Network;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,27 @@ namespace Exodia {
virtual Buffer SerializeData() override {
try {
Buffer buffer(sizeof(Color) + sizeof(TilingFactor) + sizeof(bool));
size_t offset = 0;

std::memcpy(buffer.Data, &Color, sizeof(Color));
offset += sizeof(Color);
std::memcpy(buffer.Data + offset, &TilingFactor, sizeof(TilingFactor));
offset += sizeof(TilingFactor);

buffer.Write(&Color, sizeof(Color));
buffer.Write(&TilingFactor, sizeof(TilingFactor));
bool hasTexture = (Texture != nullptr);

std::memcpy(buffer.Data, &hasTexture, sizeof(bool));
offset += sizeof(bool);
buffer.Write(&hasTexture, sizeof(bool));

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

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

std::memcpy(buffer.Data + offset, &assetHandle, sizeof(Texture->GetAssetHandle()));
offset += sizeof(Texture->GetAssetHandle());
std::memcpy(buffer.Data + offset, &(Texture->GetCoords()), sizeof(Texture->GetCoords()));
offset += sizeof(Texture->GetCoords());
std::memcpy(buffer.Data + offset, &Texture->GetTextureCellSize(),
sizeof(Texture->GetTextureCellSize()));
offset += sizeof(Texture->GetTextureCellSize());
std::memcpy(buffer.Data + offset, &Texture->GetTextureSpriteSize(),
sizeof(Texture->GetTextureSpriteSize()));
offset += sizeof(Texture->GetTextureSpriteSize());
buffer.Write(&assetHandle, sizeof(assetHandle));
buffer.Write(&Texture->GetCoords(), sizeof(Texture->GetCoords()));
buffer.Write(&Texture->GetTextureCellSize(), sizeof(Texture->GetTextureCellSize()));
buffer.Write(&Texture->GetTextureSpriteSize(), sizeof(Texture->GetTextureSpriteSize()));
}

for (size_t i = 0; i < buffer.Size; i++) {
std::cout << (int)buffer.Data[i] << " ";
}
std::cout << std::endl;
return buffer;
} catch (std::exception &e) {
EXODIA_CORE_WARN("SpriteRendererComponent serialization failed: {0}", e.what());
Expand All @@ -149,11 +140,19 @@ namespace Exodia {
Memcopy(&TilingFactor, data.Data + offset, sizeof(TilingFactor));
offset += sizeof(TilingFactor);

std::memcpy(&hasTexture, data.Data, sizeof(bool));
for (size_t i = offset; i < offset + sizeof(bool); i++) {
std::cout << (int)data.Data[i] << " ";
}
std::cout << std::endl;
if (data.Data[offset] == 1)
hasTexture = true;
else
hasTexture = false;
offset += sizeof(bool);

if (!hasTexture)
if (!hasTexture) {
return;
}
Exodia::AssetHandle assetHandle;
glm::vec2 coords;
glm::vec2 cellSize;
Expand All @@ -172,6 +171,7 @@ namespace Exodia {
offset += sizeof(spriteSize);

Texture = SubTexture2D::CreateFromCoords(assetHandle, coords, cellSize, spriteSize);

} catch (std::exception &e) {
EXODIA_CORE_WARN("SpriteRendererComponent deserialization failed: {0}", e.what());
}
Expand Down
8 changes: 8 additions & 0 deletions GameEngine/src/Exodia/Renderer/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ namespace Exodia {
delete[] _Data->QuadVertexBufferBase;
_Data->QuadVertexBufferBase = nullptr;
}
if (_Data && _Data->CircleVertexBufferBase != nullptr) {
delete[] _Data->CircleVertexBufferBase;
_Data->CircleVertexBufferBase = nullptr;
}
if (_Data && _Data->LineVertexBufferBase != nullptr) {
delete[] _Data->LineVertexBufferBase;
_Data->LineVertexBufferBase = nullptr;
}

_Data = nullptr;
}
Expand Down
38 changes: 38 additions & 0 deletions GameEngine/src/Exodia/Scene/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,44 @@ namespace Exodia {
}
});
_World->UnlockMutex();

// _World->LockMutex();
// _World->ForEach<TransformComponent, BoxCollider2DComponent>([&](Entity *entity, auto transform, auto
// collider) {
// auto &tc = transform.Get();
// auto &cc = collider.Get();

// Renderer2D::DrawRect(tc.GetTransform(), glm::vec4(1.0f, 0.5f, 0.0f, 1.0f));

// (void)cc;
// (void)entity;
// });
// _World->UnlockMutex();

// _World->LockMutex();
// _World->ForEach<TransformComponent, ParentComponent>([&](Entity *entity, auto transform, auto script) {
// auto &tc = transform.Get();
// auto &cc = script.Get();

// Renderer2D::DrawCircle(tc.GetTransform(), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f), 0.1f, 0.005f);

// (void)cc;
// (void)entity;
// });
// _World->UnlockMutex();

// _World->LockMutex();
// _World->ForEach<TransformComponent, CircleCollider2DComponent>([&](Entity *entity, auto transform, auto
// collider) {
// auto &tc = transform.Get();
// auto &cc = collider.Get();

// Renderer2D::DrawCircle(tc.GetTransform(), glm::vec4(1.0f, 0.5f, 0.0f, 1.0f), 0.1f, 0.005f);

// (void)cc;
// (void)entity;
// });
// _World->UnlockMutex();
}

///////////////////////
Expand Down
Loading

0 comments on commit dc96705

Please sign in to comment.