r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
gameSolo.cpp
Go to the documentation of this file.
2#include "Client/Common.hpp"
4#include "ECS/Component.hpp"
6
7static constexpr eng::Color WHITE = {.r = 255U, .g = 255U, .b = 255U, .a = 255U};
8static constexpr eng::Color WHITE_TRANS = {.r = 255U, .g = 255U, .b = 255U, .a = 100U};
9static constexpr eng::Color BLUE = {.r = 200U, .g = 200U, .b = 255U, .a = 150U};
10static constexpr eng::Color BLUE_SECOND = {.r = 50U, .g = 100U, .b = 200U, .a = 60U};
11static constexpr eng::Color YELLOW = {.r = 255U, .g = 255U, .b = 200U, .a = 200U};
12static constexpr eng::Color PURPLE = {.r = 100U, .g = 50U, .b = 150U, .a = 80U};
13static constexpr eng::Color GREEN = {.r = 200U, .g = 255U, .b = 200U, .a = 180U};
14
15cli::GameSolo::GameSolo(const std::shared_ptr<eng::IRenderer> &renderer, const std::shared_ptr<eng::IAudio> &audio)
16 : m_audio(audio)
17{
18 auto &registry = AScene::getRegistry();
19
20 registry.onComponentAdded(
21 [&renderer, &audio, &registry](const ecs::Entity e, const std::type_info &type)
22 {
23 const auto *audioComp = registry.getComponent<ecs::Audio>(e);
24 const auto *colorComp = registry.getComponent<ecs::Color>(e);
25 const auto *fontComp = registry.getComponent<ecs::Font>(e);
26 const auto *rectComp = registry.getComponent<ecs::Rect>(e);
27 const auto *scaleComp = registry.getComponent<ecs::Scale>(e);
28 const auto *textComp = registry.getComponent<ecs::Text>(e);
29 const auto *textureComp = registry.getComponent<ecs::Texture>(e);
30 const auto *transform = registry.getComponent<ecs::Transform>(e);
31
32 if (type == typeid(ecs::Text))
33 {
34 if (textComp && transform && fontComp)
35 {
36 renderer->createFont(fontComp->id, fontComp->path);
37 renderer->createText(
38 {.font_name = fontComp->id,
39 .color = {.r = colorComp->r, .g = colorComp->g, .b = colorComp->b, .a = colorComp->a},
40 .content = textComp->content,
41 .size = textComp->font_size,
42 .x = transform->x,
43 .y = transform->y,
44 .name = textComp->id});
45 }
46 }
47 else if (type == typeid(ecs::Texture))
48 {
49 const float scale_x = scaleComp ? scaleComp->x : 1.F;
50 const float scale_y = scaleComp ? scaleComp->y : 1.F;
51
52 renderer->createTexture(textureComp->id, textureComp->path);
53
54 if (transform && textureComp)
55 {
56 if (rectComp)
57 {
58 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
59 transform->y, scale_x, scale_y, static_cast<int>(rectComp->pos_x),
60 static_cast<int>(rectComp->pos_y), rectComp->size_x, rectComp->size_y);
61 }
62 else
63 {
64 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
65 transform->y);
66 }
67 }
68 }
69 else if (type == typeid(ecs::Audio))
70 {
71 if (audioComp)
72 {
73 audio->createAudio(audioComp->path, audioComp->volume, audioComp->loop,
74 audioComp->id + std::to_string(e));
75 }
76 }
77 });
78
79 registry.createEntity().with<ecs::Audio>("id_audio", Path::Audio::AUDIO_TITLE, 5.F, true, true).build();
80 registry.createEntity()
81 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
82 .with<ecs::Transform>("transform_title", 10.F, 10.F, 0.F)
83 .with<ecs::Color>("color_title", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
84 .with<ecs::Text>("id", std::string("RType Client"), 50U)
85 .build();
86 m_fpsEntity = registry.createEntity()
87 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
88 .with<ecs::Transform>("transform_fps", 10.F, 70.F, 0.F)
89 .with<ecs::Color>("color_fps", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
90 .with<ecs::Text>("id_text", std::string("FPS: 0"), 20U)
91 .build();
92
93 // Compteur d'ennemis
94 m_enemyCounterEntity = registry.createEntity()
95 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
96 .with<ecs::Transform>("transform_enemy_counter", 10.F, 100.F, 0.F)
97 .with<ecs::Color>("color_enemy_counter", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
98 .with<ecs::Text>("id_enemy_counter", std::string("Enemies: 0"), 20U)
99 .build();
100
101 // Compteur d'astéroïdes
102 m_asteroidCounterEntity = registry.createEntity()
103 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
104 .with<ecs::Transform>("transform_asteroid_counter", 10.F, 130.F, 0.F)
105 .with<ecs::Color>("color_asteroid_counter", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
106 .with<ecs::Text>("id_asteroid_counter", std::string("Asteroids: 0"), 20U)
107 .build();
108
109 m_playerEntity = registry.createEntity()
110 .with<ecs::Transform>("player_transform", 200.F, 100.F, 0.F)
111 .with<ecs::Velocity>("player_velocity", 0.F, 0.F)
112 .with<ecs::Rect>("player_rect", 0.F, 0.F, static_cast<int>(GameConfig::Player::SPRITE_WIDTH),
113 static_cast<int>(GameConfig::Player::SPRITE_HEIGHT))
114 .with<ecs::Scale>("player_scale", GameConfig::Player::SCALE, GameConfig::Player::SCALE)
115 .with<ecs::Texture>("player_texture", Path::Texture::TEXTURE_PLAYER)
116 .with<ecs::Player>("player", true)
117 .with<ecs::BeamCharge>("beam_charge", 0.0f, GameConfig::Beam::MAX_CHARGE)
118 .with<ecs::Hitbox>("player_hitbox", GameConfig::Hitbox::PLAYER_RADIUS)
119 .build();
120
121 // La barre de Beam sera affichée directement au-dessus du joueur
122 // Pas besoin d'une entité séparée
123 // Créer des étoiles pour l'effet de parallax simple
124 const int screenWidth = 1920;
125 const int screenHeight = 1080;
126
127 // Étoiles lointaines (lentes)
128 for (int i = 0; i < 50; ++i)
129 {
130 registry.createEntity()
131 .with<ecs::Pixel>("star_far")
132 .with<ecs::Transform>("star_far_transform", static_cast<float>(std::rand() % screenWidth),
133 static_cast<float>(std::rand() % screenHeight), 0.0f)
134 .with<ecs::Color>("star_far_color", WHITE_TRANS.r, WHITE_TRANS.g, WHITE_TRANS.b, WHITE_TRANS.a)
135 .with<ecs::Velocity>("star_far_vel", -20.0f, 0.0f)
136 .build();
137 }
138
139 // Étoiles moyennes
140 for (int i = 0; i < 30; ++i)
141 {
142 registry.createEntity()
143 .with<ecs::Pixel>("star_mid")
144 .with<ecs::Transform>("star_mid_transform", static_cast<float>(std::rand() % screenWidth),
145 static_cast<float>(std::rand() % screenHeight), 0.0f)
146 .with<ecs::Color>("star_mid_color", BLUE.r, BLUE.g, BLUE.b, BLUE.a)
147 .with<ecs::Velocity>("star_mid_vel", -40.0f, 0.0f)
148 .build();
149 }
150
151 // Étoiles proches (rapides)
152 for (int i = 0; i < 20; ++i)
153 {
154 registry.createEntity()
155 .with<ecs::Pixel>("star_near")
156 .with<ecs::Transform>("star_near_transform", static_cast<float>(std::rand() % screenWidth),
157 static_cast<float>(std::rand() % screenHeight), 0.0f)
158 .with<ecs::Color>("star_near_color", YELLOW.r, YELLOW.g, YELLOW.b, YELLOW.a)
159 .with<ecs::Velocity>("star_near_vel", -80.0f, 0.0f)
160 .build();
161 }
162
163 // Étoiles filantes
164 for (int i = 0; i < 10; ++i)
165 {
166 registry.createEntity()
167 .with<ecs::Pixel>("star_shooting")
168 .with<ecs::Transform>("star_shooting_transform", static_cast<float>(std::rand() % screenWidth),
169 static_cast<float>(std::rand() % screenHeight), 0.0f)
170 .with<ecs::Color>("star_shooting_color", GREEN.r, GREEN.g, GREEN.b, GREEN.a)
171 .with<ecs::Velocity>("star_shooting_vel", -120.0f, static_cast<float>((std::rand() % 20) - 10))
172 .build();
173 }
174
175 // Planètes lointaines (très lentes)
176 for (int i = 0; i < 5; ++i)
177 {
178 registry.createEntity()
179 .with<ecs::Pixel>("planet_far")
180 .with<ecs::Transform>("planet_far_transform", static_cast<float>(std::rand() % screenWidth),
181 static_cast<float>(std::rand() % screenHeight), 0.0f)
182 .with<ecs::Color>("planet_far_color", PURPLE.r, PURPLE.g, PURPLE.b, PURPLE.a)
183 .with<ecs::Velocity>("planet_far_vel", -5.0f, 0.0f)
184 .build();
185 }
186
187 // Nébuleuses (très lentes, grandes)
188 for (int i = 0; i < 3; ++i)
189 {
190 registry.createEntity()
191 .with<ecs::Pixel>("nebula")
192 .with<ecs::Transform>("nebula_transform", static_cast<float>(std::rand() % screenWidth),
193 static_cast<float>(std::rand() % screenHeight), 0.0f)
194 .with<ecs::Color>("nebula_color", BLUE_SECOND.r, BLUE_SECOND.g, BLUE_SECOND.b, BLUE_SECOND.a)
195 .with<ecs::Velocity>("nebula_vel", -8.0f, 0.0f)
196 .build();
197 }
198
199 // Comètes (mouvement diagonal)
200 for (int i = 0; i < 8; ++i)
201 {
202 registry.createEntity()
203 .with<ecs::Pixel>("comet")
204 .with<ecs::Transform>("comet_transform", static_cast<float>(std::rand() % screenWidth),
205 static_cast<float>(std::rand() % screenHeight), 0.0f)
206 .with<ecs::Color>("comet_color", GREEN.r, GREEN.g, GREEN.b, GREEN.a)
207 .with<ecs::Velocity>("comet_vel", -60.0f, static_cast<float>((std::rand() % 40) - 20))
208 .build();
209 }
210}
211
212void cli::GameSolo::update(const float dt, const eng::WindowSize &size)
213{
214 auto &reg = getRegistry();
215 auto *playerTransform = reg.getComponent<ecs::Transform>(m_playerEntity);
216 auto *playerVelocity = reg.getComponent<ecs::Velocity>(m_playerEntity);
217 auto &audios = reg.getAll<ecs::Audio>();
218
219 for (auto &audio : audios)
220 {
221 if (!audio.second.play && (m_audio->isPlaying(audio.second.id) == eng::Status::Playing))
222 {
223 m_audio->stopAudio(audio.second.id);
224 }
225 }
226 // if (m_keysPressed[eng::Key::Space])
227 // m_weaponSystem.update(reg, dt);
228 // m_weaponSystem.update(reg, dt, m_keysPressed[eng::Key::Space]); TODO(bobis33): tofix
229 // Mise à jour des étoiles simples
230 for (auto &[entity, pixel] : reg.getAll<ecs::Pixel>())
231 {
232 if (auto *transform = reg.getComponent<ecs::Transform>(entity))
233 {
234 if (auto *velocity = reg.getComponent<ecs::Velocity>(entity))
235 {
236 // Mise à jour de la position
237 transform->x += velocity->x * dt;
238 transform->y += velocity->y * dt;
239
240 // Réinitialiser si l'étoile sort de l'écran
241 if (transform->x < -10.0f || transform->x > size.width + 10.0f || transform->y < -10.0f ||
242 transform->y > size.height + 10.0f)
243 {
244 transform->x = static_cast<float>(size.width + std::rand() % 200);
245 transform->y = static_cast<float>(std::rand() % size.height);
246 }
247 }
248 }
249 }
250 if (auto *fpsText = reg.getComponent<ecs::Text>(m_fpsEntity))
251 {
252 fpsText->content = "FPS: " + std::to_string(static_cast<int>(1 / dt));
253 }
254
255 // Mettre à jour le compteur d'ennemis
256 if (auto *enemyCounterText = reg.getComponent<ecs::Text>(m_enemyCounterEntity))
257 {
258 int enemyCount = 0;
259 for (auto &[entity, enemy] : reg.getAll<ecs::Enemy>())
260 {
261 enemyCount++;
262 }
263 enemyCounterText->content = "Enemies: " + std::to_string(enemyCount);
264 }
265
266 // Mettre à jour le compteur d'astéroïdes
267 if (auto *asteroidCounterText = reg.getComponent<ecs::Text>(m_asteroidCounterEntity))
268 {
269 int asteroidCount = 0;
270 for (auto &[entity, asteroid] : reg.getAll<ecs::Asteroid>())
271 {
272 asteroidCount++;
273 }
274 asteroidCounterText->content = "Asteroids: " + std::to_string(asteroidCount);
275 }
276 float speed = GameConfig::Player::SPEED;
277 float diagonal_speed = speed * GameConfig::Player::DIAGONAL_SPEED_MULTIPLIER;
278
279 playerVelocity->x = 0.0f;
280 playerVelocity->y = 0.0f;
281
282 bool up = m_keysPressed[eng::Key::Up];
283 bool down = m_keysPressed[eng::Key::Down];
284 bool left = m_keysPressed[eng::Key::Left];
285 bool right = m_keysPressed[eng::Key::Right];
286
287 if (up && right)
288 {
289 playerVelocity->x = diagonal_speed;
290 playerVelocity->y = -diagonal_speed;
291 }
292 else if (up && left)
293 {
294 playerVelocity->x = -diagonal_speed;
295 playerVelocity->y = -diagonal_speed;
296 }
297 else if (down && right)
298 {
299 playerVelocity->x = diagonal_speed;
300 playerVelocity->y = diagonal_speed;
301 }
302 else if (down && left)
303 {
304 playerVelocity->x = -diagonal_speed;
305 playerVelocity->y = diagonal_speed;
306 }
307 else
308 {
309 if (up)
310 playerVelocity->y = -speed;
311 if (down)
312 playerVelocity->y = speed;
313 if (left)
314 playerVelocity->x = -speed;
315 if (right)
316 playerVelocity->x = speed;
317 }
318
319 playerTransform->x += playerVelocity->x * dt;
320 playerTransform->y += playerVelocity->y * dt;
321 playerTransform->x = std::max(playerTransform->x, 0.F);
322 playerTransform->y = std::max(playerTransform->y, 0.F);
323 playerTransform->x = std::min(playerTransform->x, static_cast<float>(size.width) -
325 playerTransform->y =
326 std::min(playerTransform->y,
328}
329
331{
332 switch (event.type)
333 {
335 if (event.key == eng::Key::Up)
336 m_keysPressed[eng::Key::Up] = true;
337 if (event.key == eng::Key::Down)
338 m_keysPressed[eng::Key::Down] = true;
339 if (event.key == eng::Key::Left)
340 m_keysPressed[eng::Key::Left] = true;
341 if (event.key == eng::Key::Right)
342 m_keysPressed[eng::Key::Right] = true;
343 if (event.key == eng::Key::Space)
344 m_keysPressed[eng::Key::Space] = true;
345 break;
346
348 if (event.key == eng::Key::Up)
349 m_keysPressed[eng::Key::Up] = false;
350 if (event.key == eng::Key::Down)
351 m_keysPressed[eng::Key::Down] = false;
352 if (event.key == eng::Key::Left)
353 m_keysPressed[eng::Key::Left] = false;
354 if (event.key == eng::Key::Right)
355 m_keysPressed[eng::Key::Right] = false;
356 if (event.key == eng::Key::Space)
357 m_keysPressed[eng::Key::Space] = false;
358 break;
359
360 default:
361 break;
362 }
363}
This file contains the component definitions.
Configuration constants for the game.
This file contains the solo Game scene.
This file contains the Audio interface.
void event(const eng::Event &event) override
Definition gameSolo.cpp:330
GameSolo(const std::shared_ptr< eng::IRenderer > &renderer, const std::shared_ptr< eng::IAudio > &audio)
Definition gameSolo.cpp:15
ecs::Entity m_enemyCounterEntity
Definition GameSolo.hpp:41
void update(float dt, const eng::WindowSize &size) override
Definition gameSolo.cpp:212
ecs::Entity m_playerEntity
Definition GameSolo.hpp:39
ecs::Entity m_asteroidCounterEntity
Definition GameSolo.hpp:42
ecs::Entity m_fpsEntity
Definition GameSolo.hpp:40
This file contains common definitions and constants.
static constexpr eng::Color WHITE
static constexpr eng::Color WHITE_TRANS
Definition gameSolo.cpp:8
static constexpr eng::Color BLUE
Definition gameSolo.cpp:9
static constexpr eng::Color YELLOW
Definition gameSolo.cpp:11
static constexpr eng::Color GREEN
Definition gameSolo.cpp:13
static constexpr eng::Color BLUE_SECOND
Definition gameSolo.cpp:10
static constexpr eng::Color PURPLE
Definition gameSolo.cpp:12
static constexpr eng::Color WHITE
Definition gameSolo.cpp:7
constexpr float MAX_CHARGE
constexpr float PLAYER_RADIUS
constexpr float DIAGONAL_SPEED_MULTIPLIER
constexpr float SCALE
constexpr float SPRITE_WIDTH
constexpr float SPRITE_HEIGHT
constexpr float SPEED
constexpr auto AUDIO_TITLE
Definition Common.hpp:44
constexpr auto FONTS_RTYPE
Definition Common.hpp:50
constexpr auto TEXTURE_PLAYER
Definition Common.hpp:63
std::uint32_t Entity
Definition Entity.hpp:13
std::string id
Definition Component.hpp:15
unsigned char r
Definition IRenderer.hpp:17
unsigned char a
Definition IRenderer.hpp:20
unsigned char g
Definition IRenderer.hpp:18
unsigned char b
Definition IRenderer.hpp:19
EventType type
Definition IRenderer.hpp:90
unsigned int width
Definition IRenderer.hpp:96
unsigned int height
Definition IRenderer.hpp:97