r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
intro.cpp
Go to the documentation of this file.
1#include <cmath>
2
4#include "ECS/Component.hpp"
6#include "Utils/Common.hpp"
7
8cli::Intro::Intro(const eng::id assignedId, const std::shared_ptr<eng::IRenderer> &renderer,
9 const std::shared_ptr<eng::IAudio> &audio)
10 : AScene(assignedId), m_renderer(renderer), m_audio(audio)
11{
12 auto &registry = AScene::getRegistry();
13
14 registry.onComponentAdded(
15 [&renderer, &registry, &audio](const ecs::Entity e, const std::type_info &type)
16 {
17 const auto *audioComp = registry.getComponent<ecs::Audio>(e);
18 const auto *colorComp = registry.getComponent<ecs::Color>(e);
19 const auto *scaleComp = registry.getComponent<ecs::Scale>(e);
20 const auto *textureComp = registry.getComponent<ecs::Texture>(e);
21 const auto *transform = registry.getComponent<ecs::Transform>(e);
22 const auto *rectComp = registry.getComponent<ecs::Rect>(e);
23
24 if (type == typeid(ecs::Texture))
25 {
26 const float scale_x = scaleComp ? scaleComp->x : 1.F;
27 const float scale_y = scaleComp ? scaleComp->y : 1.F;
28
29 renderer->createTexture(textureComp->id, textureComp->path);
30
31 if (transform && textureComp)
32 {
33 if (rectComp)
34 {
35 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
36 transform->y, scale_x, scale_y, static_cast<int>(rectComp->pos_x),
37 static_cast<int>(rectComp->pos_y), rectComp->size_x, rectComp->size_y);
38 }
39 else
40 {
41 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
42 transform->y);
43 }
44 }
45 }
46 else if (type == typeid(ecs::Audio))
47 {
48 if (audioComp)
49 {
50 audio->createAudio(audioComp->path, audioComp->volume, audioComp->loop,
51 audioComp->id + std::to_string(e));
52 }
53 }
54 });
55
57 registry.createEntity().with<ecs::Audio>("game_begin", "assets/audio/elephant.mp3", 20.0F, false, true).build();
58
59 auto [width, height] = renderer->getWindowSize();
60 m_logoEntity = registry.createEntity()
61 .with<ecs::Transform>("logo_transform", width * 0.37F, height * 0.2F)
62 .with<ecs::Scale>("logo_scale", 1.5F, 1.5F)
65 .with<ecs::Texture>("logo_text", utl::Path::Icons::ICON_APP)
66 .build();
67}
68
69void cli::Intro::update(const float dt, const eng::WindowSize &size)
70{
71 auto &reg = getRegistry();
72
73 auto *color = reg.getComponent<ecs::Color>(m_logoEntity);
74 m_elapsedTime += dt;
75
76 if (color == nullptr)
77 {
78 return;
79 }
80
81 if (m_elapsedTime < 2.0F)
82 {
83 color->a = static_cast<uint8_t>(std::min(255.0F, (m_elapsedTime / 2.0F) * 255.0F));
84 }
85 else if (m_elapsedTime < 6.0F)
86 {
87 color->a = 255;
88 }
89 else if (m_elapsedTime < 8.0f)
90 {
91 const float t = (m_elapsedTime - 6.0F) / 2.0F;
92 color->a = static_cast<uint8_t>(255.0F * (1.0F - t));
93 }
94 else
95 {
96 onLeave();
97 }
98}
99
100void cli::Intro::event(const eng::Event &event) {}
This file contains the component definitions.
This file contains the Audio interface.
This file contains the intro scene.
Intro(eng::id assignedId, const std::shared_ptr< eng::IRenderer > &renderer, const std::shared_ptr< eng::IAudio > &audio)
Definition intro.cpp:8
ecs::Entity m_logoEntity
Definition Intro.hpp:42
void event(const eng::Event &event) override
Definition intro.cpp:100
ecs::Entity m_soundEntity
Definition Intro.hpp:43
void update(float dt, const eng::WindowSize &size) override
Definition intro.cpp:69
This file contains common definitions and constants.
std::uint32_t Entity
Definition Entity.hpp:13
unsigned int id
Definition IScene.hpp:20
static constexpr eng::Color WHITE_LOW
Definition Common.hpp:65
constexpr auto ICON_APP
Definition Common.hpp:80
unsigned char a
Definition Component.hpp:29
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