Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
RoomWaitingScene.cpp
Go to the documentation of this file.
1
13
14namespace rtp::client {
15 namespace scenes {
16
18 // Public API
20
22 Settings& settings,
23 TranslationManager& translationManager,
24 NetworkSyncSystem& network,
25 graphics::UiFactory& uiFactory,
26 std::function<void(GameState)> changeState)
27 : _uiRegistry(UiRegistry),
28 _settings(settings),
29 _translationManager(translationManager),
30 _network(network),
31 _uiFactory(uiFactory),
32 _changeState(changeState)
33 {
34 }
35
37 {
38 log::info("Entering RoomWaitingScene");
39
42 {30.0f, 20.0f},
43 {900.0f, 40.0f},
44 "",
45 nullptr
46 );
47 if (auto buttonsOpt = _uiRegistry.get<ecs::components::ui::Button>()) {
48 auto &buttons = buttonsOpt.value().get();
49 if (buttons.has(_chatCompactPanel)) {
50 auto &panel = buttons[_chatCompactPanel];
51 panel.idleColor[0] = 20; panel.idleColor[1] = 20; panel.idleColor[2] = 20;
52 panel.hoverColor[0] = 20; panel.hoverColor[1] = 20; panel.hoverColor[2] = 20;
53 panel.pressedColor[0] = 20; panel.pressedColor[1] = 20; panel.pressedColor[2] = 20;
54 }
55 }
56
59 {45.0f, 28.0f},
60 "",
61 "assets/fonts/main.ttf",
62 20,
63 10,
64 {220, 220, 220}
65 );
66
69 {940.0f, 20.0f},
70 {300.0f, 40.0f},
71 "CHAT",
72 [this]() {
73 if (_chatOpen) {
74 closeChat();
75 } else {
76 openChat();
77 }
78 }
79 );
80
83 {390.0f, 140.0f},
84 "Waiting for players...",
85 "assets/fonts/title.ttf",
86 56,
87 10,
88 {255, 200, 100}
89 );
90
93 {420.0f, 280.0f},
94 {420.0f, 60.0f},
95 "Set Ready",
96 [this]() {
98 std::string buttonText = _uiReady ?
99 "Not Ready" :
100 "Ready";
101
102 log::info("Player is now {}", _uiReady ? "ready" : "not ready");
103
105 }
106 );
107
110 {420.0f, 360.0f},
111 {420.0f, 60.0f},
112 "Leave Room",
113 [this]() {
116 }
117 );
118 }
119
121 {
122 closeChat();
123 }
124
125 void RoomWaitingScene::handleEvent(const sf::Event& event)
126 {
127 (void)event;
128 }
129
131 {
132 (void)dt;
133 if (_network.consumeKicked()) {
135 return;
136 }
137 if (_network.isInGame()) {
139 }
140
141 auto textsOpt = _uiRegistry.get<ecs::components::ui::Text>();
142 if (textsOpt) {
143 auto &texts = textsOpt.value().get();
144 if (texts.has(_chatCompactText)) {
146 }
147 }
148
149 if (_chatOpen) {
151 }
152 }
153
155 {
156 if (_chatOpen)
157 return;
158
159 _chatOpen = true;
160
163 {30.0f, 70.0f},
164 {1220.0f, 220.0f},
165 "",
166 nullptr
167 );
168 if (auto buttonsOpt = _uiRegistry.get<ecs::components::ui::Button>()) {
169 auto &buttons = buttonsOpt.value().get();
170 if (buttons.has(_chatPanel)) {
171 auto &panel = buttons[_chatPanel];
172 panel.idleColor[0] = 20; panel.idleColor[1] = 20; panel.idleColor[2] = 20;
173 panel.hoverColor[0] = 20; panel.hoverColor[1] = 20; panel.hoverColor[2] = 20;
174 panel.pressedColor[0] = 20; panel.pressedColor[1] = 20; panel.pressedColor[2] = 20;
175 }
176 }
177
180 {45.0f, 80.0f},
181 "",
182 "assets/fonts/main.ttf",
183 18,
184 10,
185 {230, 230, 230}
186 );
187
190 {45.0f, 250.0f},
191 {1180.0f, 30.0f},
192 "assets/fonts/main.ttf",
193 18,
194 120,
195 "Type message...",
196 nullptr,
197 nullptr
198 );
199 if (auto inputsOpt = _uiRegistry.get<ecs::components::ui::TextInput>()) {
200 auto &inputs = inputsOpt.value().get();
201 if (inputs.has(_chatInput)) {
202 inputs[_chatInput].onSubmit = [this](const std::string&) {
204 };
205 inputs[_chatInput].isFocused = true;
206 inputs[_chatInput].showCursor = true;
207 }
208 }
209
211 }
212
214 {
215 if (!_chatOpen)
216 return;
217
218 _chatOpen = false;
222 _chatPanel = {};
223 _chatHistoryText = {};
224 _chatInput = {};
225 }
226
228 {
229 auto textsOpt = _uiRegistry.get<ecs::components::ui::Text>();
230 if (!textsOpt)
231 return;
232 auto &texts = textsOpt.value().get();
233 if (!texts.has(_chatHistoryText))
234 return;
235
236 const auto &history = _network.getChatHistory();
237 std::string combined;
238 for (const auto &line : history) {
239 if (!combined.empty())
240 combined += "\n";
241 combined += line;
242 }
243 texts[_chatHistoryText].content = combined;
244 }
245
247 {
249 if (!inputsOpt)
250 return;
251
252 auto &inputs = inputsOpt.value().get();
253 if (!inputs.has(_chatInput))
254 return;
255
256 auto &input = inputs[_chatInput];
257 const std::string message = input.value;
258 if (message.empty())
259 return;
260
261 _network.trySendMessage(message);
262 input.value.clear();
263 }
264 } // namespace scenes
265} // namespace rtp::client
System to handle network-related operations on the client side.
bool isInGame(void) const
Check if the client is in game.
void trySetReady(bool isReady)
Send a request to set the client's ready status in the current room.
void tryLeaveRoom(void)
Send a request to leave the current room on the server.
void trySendMessage(const std::string &message) const
Send a chat message to the server.
std::string getLastChatMessage(void) const
Get the last received room chat message.
const std::deque< std::string > & getChatHistory(void) const
Get chat history buffer (most recent last)
Manages game settings and preferences.
Definition Settings.hpp:67
Manages game translations for all UI elements.
Factory class for creating UI components in the ECS registry.
Definition UiFactory.hpp:72
static ecs::Entity createTextInput(ecs::Registry &registry, const position &position, const size &size, const std::string &fontPath, unsigned int fontSize, const int maxLength=64, const std::string &placeholder="", std::function< void(const std::string &)> onSubmit=nullptr, std::function< void(const std::string &)> onChange=nullptr)
static ecs::Entity createText(ecs::Registry &registry, const position &position, const std::string &content, const std::string &fontPath, unsigned int fontSize, const std::uint8_t zIndex=0, const color &textColor={255, 255, 255})
Definition UiFactory.cpp:43
static ecs::Entity createButton(ecs::Registry &registry, const position &position, const size &size, const std::string &label, std::function< void()> onClick=nullptr)
Create a button UI component.
Definition UiFactory.cpp:17
ChangeStateFn _changeState
Function to change the game state.
NetworkSyncSystem & _network
Reference to the client network.
graphics::UiFactory & _uiFactory
UI Factory for creating UI components.
ecs::Entity _chatCompactPanel
Compact chat background panel.
void update(float dt) override
Update the scene state.
void onExit(void) override
Called when the scene is exited.
ecs::Entity _chatToggleButton
Toggle button for chat.
ecs::Registry & _uiRegistry
Reference to the ECS registry.
void onEnter(void) override
Called when the scene is entered.
RoomWaitingScene(ecs::Registry &UiRegistry, Settings &settings, TranslationManager &translationManager, NetworkSyncSystem &network, graphics::UiFactory &uiFactory, std::function< void(GameState)> changeState)
Constructor for RoomWaitingScene.
bool _uiReady
Player ready status in the UI.
ecs::Entity _chatInput
Text input entity for chat.
void handleEvent(const sf::Event &event) override
Handle an incoming event.
bool _chatOpen
Whether expanded chat is open.
ecs::Entity _chatPanel
Expanded chat panel.
ecs::Entity _chatHistoryText
Text entity for chat history.
ecs::Entity _chatCompactText
Text entity for last chat message.
constexpr bool isNull(void) const noexcept
void kill(Entity entity)
Definition Registry.cpp:73
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
R-Type client namespace.
@ Menu
Main menu state.
@ Playing
In-game playing state.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
Component representing a clickable button.
Definition Button.hpp:30
Component representing text to render.
Definition Text.hpp:19
std::string content
Text content.
Definition Text.hpp:20