Skip to content

Commit c3670c9

Browse files
authored
Merge pull request #92 from G-Epitech/rtp-110-update-room-selection
RTP-110 - Update room selection
2 parents f83ecc9 + 66f7cf7 commit c3670c9

File tree

18 files changed

+369
-96
lines changed

18 files changed

+369
-96
lines changed

client/src/builder/services.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ using namespace rtype::client::builder;
1414
using namespace rtype::client::services;
1515

1616
void Services::RegisterServices(mew::game::GameBuilder *builder) {
17-
builder->WithService<ServerConnectionService>("127.0.0.1", 4848);
17+
builder->WithService<ServerConnectionService>("rtype.thismath.com", 4848);
1818
builder->WithService<RoomsService>();
1919
}

client/src/scenes/game.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ void SceneGame::OnCreate() {
8383
InitFade();
8484
}
8585

86+
void SceneGame::OnActivate() {
87+
ResetFade();
88+
}
89+
8690
void SceneGame::Update(DeltaTime delta_time) {
8791
deltaTime_ = delta_time;
8892
registry_->RunSystems();
@@ -155,3 +159,26 @@ void SceneGame::InitFade() {
155159

156160
registry_->AddComponent<Tags>(rectangle, Tags({"end_fade"}));
157161
}
162+
163+
void SceneGame::ResetFade() {
164+
auto drawables = registry_->GetComponents<Drawable>();
165+
auto tags = registry_->GetComponents<Tags>();
166+
std::size_t fade_id = 0;
167+
std::size_t i = 0;
168+
169+
for (auto &tag : *tags) {
170+
if (!tag) {
171+
i += 1;
172+
continue;
173+
} else if ((*tag) & "end_fade") {
174+
fade_id = i;
175+
}
176+
i += 1;
177+
}
178+
auto &fade = (*drawables)[fade_id];
179+
if (!fade) {
180+
return;
181+
}
182+
auto &rectangle = std::get<drawable::Rectangle>((*fade).drawable);
183+
rectangle.fillColor.a = 0;
184+
}

client/src/scenes/game.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class SceneGame final : public SceneBase {
3030

3131
void Update(DeltaTime delta_time) override;
3232

33+
void OnActivate() override;
34+
3335
protected:
3436
void OnCreate() override;
3537

@@ -60,6 +62,11 @@ class SceneGame final : public SceneBase {
6062
*/
6163
void InitFade();
6264

65+
/**
66+
* @brief Reset fade
67+
*/
68+
void ResetFade();
69+
6370
/// @brief Server connection service
6471
services::ServerConnectionService::Ptr serverConnectionService_;
6572

client/src/scenes/lobby.cpp

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "lobby.hpp"
99

1010
#include "./game.hpp"
11+
#include "libs/game/src/api/client/client.hpp"
1112
#include "libs/mew/src/sets/drawable/drawable.hpp"
1213

1314
using namespace rtype::client::scenes;
@@ -18,6 +19,7 @@ using namespace porygon;
1819
using namespace mew::sets::drawable;
1920
using namespace mew::scenes;
2021
using namespace mew::managers;
22+
using namespace rtype::sdk::game::api;
2123

2224
SceneLobby::SceneLobby(DependenciesHandler::Ptr services) : SceneBase(std::move(services)) {
2325
serverConnectionService_ = services_->GetOrThrow<ServerConnectionService>();
@@ -36,10 +38,7 @@ void SceneLobby::OnCreate() {
3638
}
3739

3840
void SceneLobby::OnActivate() {
39-
if (status_ == LobbyStatus::kIn) {
40-
return managers_.scenes->GoToScene<SceneGame>();
41-
}
42-
JoinLobbyAsync();
41+
WaitAsync();
4342
}
4443

4544
void SceneLobby::Update(std::chrono::nanoseconds delta_time) {
@@ -48,10 +47,6 @@ void SceneLobby::Update(std::chrono::nanoseconds delta_time) {
4847
if (status_ == LobbyStatus::kIn) {
4948
managers_.scenes->GoToScene<SceneGame>();
5049
}
51-
52-
if (status_ == LobbyStatus::kWaitingPlayers) {
53-
WaitGameStart();
54-
}
5550
}
5651

5752
void SceneLobby::CreateStatusText() {
@@ -83,46 +78,29 @@ void SceneLobby::CreateGEpitechLogo() {
8378
{Texture{.name = "g-epitech-logo", .scale = 0.35, .rect = rect}, WindowManager::View::HUD});
8479
}
8580

86-
void SceneLobby::JoinLobbyAsync() {
81+
void SceneLobby::WaitAsync() {
8782
if (joiningThread_)
8883
return;
89-
joiningThread_ = std::thread(&SceneLobby::JoinLobby, this);
84+
joiningThread_ = std::thread(&SceneLobby::Wait, this);
9085
}
9186

92-
void SceneLobby::JoinLobby() {
93-
if (status_ == LobbyStatus::kJoining) {
94-
return;
95-
}
96-
mainMessage_ = "Creating the room";
97-
secondaryMessage_ = "Please wait...";
98-
auto res = serverConnectionService_->client()->CreateRoom({
99-
.name = "MyPtitRoom",
100-
.nbPlayers = 2,
101-
.difficulty = 0,
102-
});
103-
if (!res) {
104-
mainMessage_ = "Creating failed";
105-
secondaryMessage_ = "Unable to create the lobby";
106-
status_ = LobbyStatus::kFailed;
107-
return;
108-
}
109-
std::this_thread::sleep_for(std::chrono::milliseconds(1200));
110-
status_ = LobbyStatus::kRegistered;
111-
mainMessage_ = "Joining the room";
112-
secondaryMessage_ = "Please wait...";
113-
res = serverConnectionService_->client()->JoinRoom({
114-
.nodeId = 0,
115-
.roomId = 0,
116-
});
117-
if (!res) {
118-
mainMessage_ = "Joining failed";
119-
secondaryMessage_ = "Unable to join the lobby";
120-
status_ = LobbyStatus::kFailed;
121-
return;
87+
void SceneLobby::Wait() {
88+
mainMessage_ = "Starting...";
89+
secondaryMessage_ = "You are now in the lobby. Waiting for other players";
90+
status_ = LobbyStatus::kWaiting;
91+
92+
while (status_ == LobbyStatus::kWaiting) {
93+
auto queue = serverConnectionService_->client()->ExtractQueue();
94+
95+
while (!queue.empty()) {
96+
auto message = queue.front();
97+
if (message.messageType == kMsgTypeMTCGameStarted) {
98+
status_ = LobbyStatus::kIn;
99+
break;
100+
}
101+
queue.pop();
102+
}
122103
}
123-
mainMessage_ = "Waiting for players";
124-
secondaryMessage_ = "Please wait until all players are ready";
125-
status_ = LobbyStatus::kWaitingPlayers;
126104
}
127105

128106
void SceneLobby::UpdateStatusText() {
@@ -157,17 +135,12 @@ void SceneLobby::UpdateStatusText() {
157135
return;
158136
}
159137
}
160-
161-
void SceneLobby::WaitGameStart() {
162-
auto queue = serverConnectionService_->client()->ExtractQueue();
163-
164-
while (!queue.empty()) {
165-
auto &message = queue.front();
166-
if (message.messageType == api::MasterToClientMsgType::kMsgTypeMTCGameStarted) {
167-
mainMessage_ = "Starting...";
168-
secondaryMessage_ = "The game is starting, please wait";
169-
status_ = LobbyStatus::kIn;
138+
void SceneLobby::OnDeactivate() {
139+
if (joiningThread_ && joiningThread_->joinable()) {
140+
joiningThread_->join();
170141
}
171-
queue.pop();
172-
}
142+
joiningThread_.reset();
143+
status_ = LobbyStatus::kNone;
144+
mainMessage_ = "";
145+
secondaryMessage_ = "";
173146
}

client/src/scenes/lobby.hpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ class SceneLobby final : public SceneBase {
3232

3333
void OnActivate() override;
3434

35+
void OnDeactivate() override;
36+
3537
private:
3638
/**
3739
* @brief Create the title
3840
*/
3941
enum class LobbyStatus {
40-
kNone, ///< No status
41-
kFailed, ///< Failed to join the lobby
42-
kRegistrationInProgress, ///< Registration in progress
43-
kRegistered, ///< Registered in on the server
44-
kJoining, ///< Joining the lobby
45-
kWaitingPlayers, ///< Waiting for players
46-
kIn, ///< In the lobby
42+
kNone, ///< No status
43+
kWaiting, ///< Waiting for the lobby
44+
kIn, ///< In the lobby
4745
};
4846

4947
/**
@@ -59,23 +57,18 @@ class SceneLobby final : public SceneBase {
5957
/**
6058
* @brief Join the lobby
6159
*/
62-
void JoinLobby();
60+
void Wait();
6361

6462
/**
6563
* @brief Join the lobby asynchronously
6664
*/
67-
void JoinLobbyAsync();
65+
void WaitAsync();
6866

6967
/**
7068
* @brief Update the status text
7169
*/
7270
void UpdateStatusText();
7371

74-
/**
75-
* @brief Wait for the game to start
76-
*/
77-
void WaitGameStart();
78-
7972
/// @brief The current lobby status
8073
LobbyStatus status_ = LobbyStatus::kNone;
8174

client/src/scenes/start/create_room.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void SceneCreateRoom::CreateCreateButton() const {
106106
return;
107107
}
108108
api::payload::CreateRoom payload{};
109-
payload.difficulty = difficulty_ - 1;
109+
payload.difficulty = difficulty_ - 2;
110110
payload.nbPlayers = nbPlayers_ - 1;
111111
char usernameChar[20] = {0};
112112
strncpy(usernameChar, text.text.c_str(), sizeof(usernameChar) - 1);

client/src/scenes/start/entities/text_entity.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,24 @@ void TextEntity::OnSpawn(const TextEntity::Properties &props) {
2828
auto drawable = Text{.text = props.text,
2929
.fontName = props.fontName,
3030
.characterSize = props.characterSize,
31+
.style = props.style,
3132
.color = props.color};
3233

33-
registry_->AddComponent<Drawable>(*this, {drawable, WindowManager::HUD});
34+
registry_->AddComponent<Drawable>(
35+
*this, {.drawable = drawable, .view = WindowManager::HUD, .visible = props.visible});
3436
registry_->AddComponent<Position>(*this, {
3537
props.position,
3638
props.alignment,
3739
});
40+
auto tags = Tags({});
41+
for (const auto &tag : props.tags) {
42+
tags.AddTag(tag);
43+
}
44+
registry_->AddComponent<Tags>(*this, tags);
45+
}
46+
47+
void TextEntity::RegisterDependencies(const Registry::Ptr &registry) {
48+
registry->RegisterComponent<Drawable>();
49+
registry->RegisterComponent<Position>();
50+
registry->RegisterComponent<Tags>();
3851
}

client/src/scenes/start/entities/text_entity.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ class TextEntity : public Entity {
3333
sf::Color color;
3434
/// @brief Layer of the text
3535
zygarde::core::components::Alignment alignment;
36+
/// @brief Style of the text*
37+
sf::Text::Style style = sf::Text::Style::Regular;
38+
/// @brief Tags list
39+
std::vector<std::string> tags;
40+
/// @brief View of the text
41+
bool visible = true;
3642
};
3743

44+
/**
45+
* @brief Register the dependencies
46+
* @param registry Registry to register the dependencies
47+
*/
48+
static void RegisterDependencies(const zygarde::Registry::Ptr &registry);
49+
3850
explicit TextEntity(const Entity &other);
3951

4052
protected:

0 commit comments

Comments
 (0)