R-Type 0.1
Loading...
Searching...
No Matches
gameScene.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <optional>
6#include <vector>
7
8#include "EngineContext.hpp"
9#include "graphical/renderWindow.hpp"
10#include "sceneManager/sceneConcept.hpp"
11
12#include "components/AABBCollider.hpp"
13#include "components/Controllable.hpp"
14#include "components/Transform.hpp"
15#include "components/Velocity.hpp"
16
17#include "systems/AnimationSystem.hpp"
18#include "systems/AnimatorSystem.hpp"
19#include "systems/RenderConcept.hpp"
20
21#include "sceneType.hpp"
22
23namespace rtype::client {
32class GameScene {
33public:
38 explicit GameScene(rtk::RenderWindow &window);
39 ~GameScene() = default;
40
42 static constexpr SceneType Type = SceneType::Game;
43
48 void onEnter(rtk::EngineContext &context);
49
57 std::optional<SceneType> onUpdate(rtk::EngineContext &context,
58 const rtk::InputState &inputState,
59 float dt);
60
65 void onExit(rtk::EngineContext &context);
66
67private:
68 void createParallaxBackground(rtk::EngineContext &context,
69 const std::string &bp) {
70 const float scaledWidth = 720.0f;
71
72 for (int i = 0; i < 3; ++i) {
73 auto bg =
74 context._entityFactory.createFromBlueprint(context.registry, bp);
75
76 auto *transform =
77 context.registry.get_components<component::Transform>().get(bg);
78 if (transform) {
79 transform->position.x = i * scaledWidth;
80 }
81
82 _entities.push_back(bg);
83 }
84 }
85
86 float createGate(rtk::EngineContext &context, float startX, bool symmetric) {
87 auto gateEntity =
88 context._entityFactory.createFromBlueprint(context.registry, "gate");
89
90 context.registry.get_components<component::Transform>()
91 .get(gateEntity)
92 ->position.x = startX;
93 _entities.push_back(gateEntity);
94
95 if (symmetric) {
96 auto ceilingEntity =
97 context._entityFactory.createFromBlueprint(context.registry, "gate");
98 auto *transform =
99 context.registry.get_components<component::Transform>().get(
100 ceilingEntity);
101
102 transform->position.x = startX;
103 transform->position.y = 0.0f;
104 context.registry.get_components<rtk::ecs::concepts::SpriteData>()
105 .get(ceilingEntity)
106 ->flags = 1 << 1;
107 _entities.push_back(ceilingEntity);
108 }
109 return startX + (192.0f * 2.0f);
110 }
111
112 float createRamp(rtk::EngineContext &context, float startX, bool symmetric) {
113 auto rampEntity =
114 context._entityFactory.createFromBlueprint(context.registry, "ramp");
115 context.registry.get_components<component::Transform>()
116 .get(rampEntity)
117 ->position.x = startX;
118 _entities.push_back(rampEntity);
119
120 if (symmetric) {
121 auto ceilingEntity =
122 context._entityFactory.createFromBlueprint(context.registry, "ramp");
123 auto *transform =
124 context.registry.get_components<component::Transform>().get(
125 ceilingEntity);
126 transform->position.x = startX;
127 transform->position.y = 0.0f;
128 context.registry.get_components<rtk::ecs::concepts::SpriteData>()
129 .get(ceilingEntity)
130 ->flags = 1 << 1;
131 _entities.push_back(ceilingEntity);
132 }
133 return startX + (192.0f * 2.0f);
134 }
135
136 float createReactor(rtk::EngineContext &context, float startX,
137 bool symmetric) {
138 auto reactorEntity =
139 context._entityFactory.createFromBlueprint(context.registry, "reactor");
140 context.registry.get_components<component::Transform>()
141 .get(reactorEntity)
142 ->position.x = startX;
143 _entities.push_back(reactorEntity);
144
145 if (symmetric) {
146 auto ceilingEntity = context._entityFactory.createFromBlueprint(
147 context.registry, "reactor");
148 auto *transform =
149 context.registry.get_components<component::Transform>().get(
150 ceilingEntity);
151 transform->position.x = startX;
152 transform->position.y = 0.0f;
153 context.registry.get_components<rtk::ecs::concepts::SpriteData>()
154 .get(ceilingEntity)
155 ->flags = 1 << 1;
156 _entities.push_back(ceilingEntity);
157 }
158 return startX + (113.0f * 2.0f);
159 }
160
161 float createHighGround(rtk::EngineContext &context, float startX,
162 bool symmetric) {
163 auto highGroundEntity = context._entityFactory.createFromBlueprint(
164 context.registry, "high_ground");
165 context.registry.get_components<component::Transform>()
166 .get(highGroundEntity)
167 ->position.x = startX;
168 _entities.push_back(highGroundEntity);
169
170 if (symmetric) {
171 auto ceilingEntity = context._entityFactory.createFromBlueprint(
172 context.registry, "high_ground");
173 auto *transform =
174 context.registry.get_components<component::Transform>().get(
175 ceilingEntity);
176 transform->position.x = startX;
177 transform->position.y = 0.0f;
178 context.registry.get_components<rtk::ecs::concepts::SpriteData>()
179 .get(ceilingEntity)
180 ->flags = 1 << 1;
181 _entities.push_back(ceilingEntity);
182 }
183 return startX + (64.0f * 2.0f);
184 }
185
186 float createLowGround(rtk::EngineContext &context, float startX,
187 bool symmetric) {
188 auto lowGroundEntity = context._entityFactory.createFromBlueprint(
189 context.registry, "low_ground");
190 context.registry.get_components<component::Transform>()
191 .get(lowGroundEntity)
192 ->position.x = startX;
193 _entities.push_back(lowGroundEntity);
194
195 if (symmetric) {
196 auto ceilingEntity = context._entityFactory.createFromBlueprint(
197 context.registry, "low_ground");
198 auto *transform =
199 context.registry.get_components<component::Transform>().get(
200 ceilingEntity);
201 transform->position.x = startX;
202 transform->position.y = 0.0f;
203 context.registry.get_components<rtk::ecs::concepts::SpriteData>()
204 .get(ceilingEntity)
205 ->flags = 1 << 1;
206 _entities.push_back(ceilingEntity);
207 }
208 return startX + (256.0f * 2.0f);
209 }
210
211 float createMediumGround(rtk::EngineContext &context, float startX,
212 bool symmetric) {
213 auto groundEntity = context._entityFactory.createFromBlueprint(
214 context.registry, "medium_ground");
215 context.registry.get_components<component::Transform>()
216 .get(groundEntity)
217 ->position.x = startX;
218 _entities.push_back(groundEntity);
219
220 if (symmetric) {
221 auto ceilingEntity = context._entityFactory.createFromBlueprint(
222 context.registry, "medium_ground");
223 auto *transform =
224 context.registry.get_components<component::Transform>().get(
225 ceilingEntity);
226 transform->position.x = startX;
227 transform->position.y = 0.0f;
228 context.registry.get_components<rtk::ecs::concepts::SpriteData>()
229 .get(ceilingEntity)
230 ->flags = 1 << 1;
231 _entities.push_back(ceilingEntity);
232 }
233 return startX + (64.0f * 2.0f);
234 }
235
236 void createBackground(rtk::EngineContext &context) {
237 float currentX = 0.0f;
238 currentX = createMediumGround((context), currentX, false);
239 currentX = createLowGround((context), currentX, false);
240 currentX = createHighGround((context), currentX, false);
241 currentX = createMediumGround((context), currentX, false);
242 currentX = createLowGround((context), currentX, false);
243 currentX = createHighGround((context), currentX, false);
244 currentX = createHighGround((context), currentX, false);
245 currentX = createMediumGround((context), currentX, false);
246 currentX = createLowGround((context), currentX, false);
247 currentX = createMediumGround((context), currentX, false);
248 currentX = createReactor(context, currentX, true);
249 currentX = createLowGround((context), currentX, true);
250 currentX = createMediumGround((context), currentX, true);
251 currentX = createHighGround((context), currentX, true);
252 currentX = createLowGround((context), currentX, true);
253 currentX = createRamp((context), currentX, true);
254 currentX = createMediumGround((context), currentX, true);
255 currentX = createMediumGround((context), currentX, true);
256 currentX = createMediumGround((context), currentX, true);
257 currentX = createHighGround((context), currentX, true);
258 currentX = createGate((context), currentX, true);
259 currentX = createLowGround((context), currentX, true);
260 currentX = createMediumGround((context), currentX, true);
261 currentX = createHighGround((context), currentX, true);
262 }
263
264 void createPlayer(rtk::EngineContext &context) {
265 // auto entity =
266 // context._entityFactory.createFromBlueprint(context.registry, "player");
267
268 //_entities.push_back(entity);
269 //_playerEntity.emplace(entity);
270 }
271 rtk::RenderWindow &_window;
272
273 std::vector<std::size_t> _entities;
274 std::optional<std::size_t> _playerEntity;
275
276 std::vector<std::size_t> bullets;
277
278 std::uint32_t _textureId = 0;
279 std::uint32_t _backgroundTextureId = 0;
280
281 std::uint16_t _bulletPressedByTick = 0;
282};
283
285} // namespace rtype::client
std::size_t createFromBlueprint(rtk::ecs::Registry &reg, const std::string &blueprintName)
Spawns an entity from a loaded blueprint.
Definition factoryDataDriven.hpp:195
In game scene, which shows the scrolling starfield.
Definition gameScene.hpp:32
void onEnter(rtk::EngineContext &context)
Creates the two parallax starfield layers.
Definition gameScene.cpp:16
void onExit(rtk::EngineContext &context)
Destroys the entities created by the scene.
Definition gameScene.cpp:28
std::optional< SceneType > onUpdate(rtk::EngineContext &context, const rtk::InputState &inputState, float dt)
Does nothing, the game stays in this scene.
Definition gameScene.cpp:21
GameScene(rtk::RenderWindow &window)
Creates the scene.
Definition gameScene.cpp:14
static constexpr SceneType Type
Identifier of this scene.
Definition gameScene.hpp:42
SceneType
Identifiers of the client scenes.
Definition sceneType.hpp:12
Position, rotation and scale of an entity in world space.
Definition Transform.hpp:22
utils::vec2 position
Top left corner, in pixels.
Definition Transform.hpp:24
Engine services handed to the game and its scenes.
Definition EngineContext.hpp:20
rtk::factory::EntityFactory _entityFactory
Blueprint based entity factory.
Definition EngineContext.hpp:25
ecs::Registry & registry
Entities and components.
Definition EngineContext.hpp:21
Player input sampled during the current frame.
Definition InputAction.hpp:14
Everything the renderer needs to draw one sprite.
Definition RenderConcept.hpp:338
uint32_t flags
Bit 0 flips horizontally, bit 1 vertically.
Definition RenderConcept.hpp:350
float x
Horizontal component.
Definition vec2.hpp:13