18 auto ®istry = AScene::getRegistry();
20 registry.onComponentAdded(
21 [&renderer, &audio, ®istry](
const ecs::Entity e,
const std::type_info &type)
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);
34 if (textComp && transform && fontComp)
36 renderer->createFont(fontComp->
id, fontComp->path);
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,
44 .name = textComp->
id});
49 const float scale_x = scaleComp ? scaleComp->x : 1.F;
50 const float scale_y = scaleComp ? scaleComp->y : 1.F;
52 renderer->createTexture(textureComp->
id, textureComp->path);
54 if (transform && textureComp)
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);
64 renderer->createSprite(textureComp->
id + std::to_string(e), textureComp->
id, transform->x,
73 audio->createAudio(audioComp->path, audioComp->volume, audioComp->loop,
74 audioComp->id + std::to_string(e));
80 registry.createEntity()
82 .with<ecs::Transform>(
"transform_title", 10.F, 10.F, 0.F)
84 .with<ecs::Text>(
"id", std::string(
"RType Client"), 50U)
88 .with<ecs::Transform>(
"transform_fps", 10.F, 70.F, 0.F)
90 .with<ecs::Text>(
"id_text", std::string(
"FPS: 0"), 20U)
96 .with<ecs::Transform>(
"transform_enemy_counter", 10.F, 100.F, 0.F)
98 .with<ecs::Text>(
"id_enemy_counter", std::string(
"Enemies: 0"), 20U)
104 .with<ecs::Transform>(
"transform_asteroid_counter", 10.F, 130.F, 0.F)
106 .with<ecs::Text>(
"id_asteroid_counter", std::string(
"Asteroids: 0"), 20U)
111 .with<ecs::Velocity>(
"player_velocity", 0.F, 0.F)
116 .with<ecs::Player>(
"player",
true)
124 const int screenWidth = 1920;
125 const int screenHeight = 1080;
128 for (
int i = 0; i < 50; ++i)
130 registry.createEntity()
132 .with<ecs::Transform>(
"star_far_transform",
static_cast<float>(std::rand() % screenWidth),
133 static_cast<float>(std::rand() % screenHeight), 0.0f)
135 .with<ecs::Velocity>(
"star_far_vel", -20.0f, 0.0f)
140 for (
int i = 0; i < 30; ++i)
142 registry.createEntity()
144 .with<ecs::Transform>(
"star_mid_transform",
static_cast<float>(std::rand() % screenWidth),
145 static_cast<float>(std::rand() % screenHeight), 0.0f)
147 .with<ecs::Velocity>(
"star_mid_vel", -40.0f, 0.0f)
152 for (
int i = 0; i < 20; ++i)
154 registry.createEntity()
156 .with<ecs::Transform>(
"star_near_transform",
static_cast<float>(std::rand() % screenWidth),
157 static_cast<float>(std::rand() % screenHeight), 0.0f)
159 .with<ecs::Velocity>(
"star_near_vel", -80.0f, 0.0f)
164 for (
int i = 0; i < 10; ++i)
166 registry.createEntity()
168 .with<ecs::Transform>(
"star_shooting_transform",
static_cast<float>(std::rand() % screenWidth),
169 static_cast<float>(std::rand() % screenHeight), 0.0f)
171 .with<ecs::Velocity>(
"star_shooting_vel", -120.0f,
static_cast<float>((std::rand() % 20) - 10))
176 for (
int i = 0; i < 5; ++i)
178 registry.createEntity()
180 .with<ecs::Transform>(
"planet_far_transform",
static_cast<float>(std::rand() % screenWidth),
181 static_cast<float>(std::rand() % screenHeight), 0.0f)
183 .with<ecs::Velocity>(
"planet_far_vel", -5.0f, 0.0f)
188 for (
int i = 0; i < 3; ++i)
190 registry.createEntity()
192 .with<ecs::Transform>(
"nebula_transform",
static_cast<float>(std::rand() % screenWidth),
193 static_cast<float>(std::rand() % screenHeight), 0.0f)
195 .with<ecs::Velocity>(
"nebula_vel", -8.0f, 0.0f)
200 for (
int i = 0; i < 8; ++i)
202 registry.createEntity()
204 .with<ecs::Transform>(
"comet_transform",
static_cast<float>(std::rand() % screenWidth),
205 static_cast<float>(std::rand() % screenHeight), 0.0f)
207 .with<ecs::Velocity>(
"comet_vel", -60.0f,
static_cast<float>((std::rand() % 40) - 20))
214 auto ® = getRegistry();
215 auto *playerTransform = reg.getComponent<
ecs::Transform>(m_playerEntity);
216 auto *playerVelocity = reg.getComponent<
ecs::Velocity>(m_playerEntity);
219 for (
auto &audio : audios)
223 m_audio->stopAudio(audio.second.id);
230 for (
auto &[entity, pixel] : reg.getAll<
ecs::Pixel>())
234 if (
auto *velocity = reg.getComponent<
ecs::Velocity>(entity))
237 transform->x += velocity->x * dt;
238 transform->y += velocity->y * dt;
241 if (transform->x < -10.0f || transform->x > size.
width + 10.0f || transform->y < -10.0f ||
242 transform->y > size.
height + 10.0f)
244 transform->x =
static_cast<float>(size.
width + std::rand() % 200);
245 transform->y =
static_cast<float>(std::rand() % size.
height);
250 if (
auto *fpsText = reg.getComponent<
ecs::Text>(m_fpsEntity))
252 fpsText->content =
"FPS: " + std::to_string(
static_cast<int>(1 / dt));
256 if (
auto *enemyCounterText = reg.getComponent<
ecs::Text>(m_enemyCounterEntity))
259 for (
auto &[entity, enemy] : reg.getAll<
ecs::Enemy>())
263 enemyCounterText->content =
"Enemies: " + std::to_string(enemyCount);
267 if (
auto *asteroidCounterText = reg.getComponent<
ecs::Text>(m_asteroidCounterEntity))
269 int asteroidCount = 0;
274 asteroidCounterText->content =
"Asteroids: " + std::to_string(asteroidCount);
279 playerVelocity->
x = 0.0f;
280 playerVelocity->
y = 0.0f;
289 playerVelocity->
x = diagonal_speed;
290 playerVelocity->
y = -diagonal_speed;
294 playerVelocity->
x = -diagonal_speed;
295 playerVelocity->
y = -diagonal_speed;
297 else if (down && right)
299 playerVelocity->
x = diagonal_speed;
300 playerVelocity->
y = diagonal_speed;
302 else if (down && left)
304 playerVelocity->
x = -diagonal_speed;
305 playerVelocity->
y = diagonal_speed;
310 playerVelocity->
y = -speed;
312 playerVelocity->
y = speed;
314 playerVelocity->
x = -speed;
316 playerVelocity->
x = speed;
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) -
326 std::min(playerTransform->y,
constexpr auto TEXTURE_PLAYER