r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Spawn.hpp
Go to the documentation of this file.
1///
2/// @file Systems.hpp
3/// @brief This file contains the system definitions
4/// @namespace cli
5///
6
7#pragma once
8
10#include "ECS/Component.hpp"
11#include "ECS/Registry.hpp"
13
14namespace cli
15{
16
17 class SpawnSystem final : public eng::ISystem
18 {
19 public:
20 explicit SpawnSystem(eng::IRenderer &renderer) : m_renderer(renderer) {}
21 ~SpawnSystem() override = default;
22
23 SpawnSystem(const SpawnSystem &) = delete;
24 SpawnSystem &operator=(const SpawnSystem &) = delete;
27
28 bool isEnable() override { return true; }
29 void setEnable(bool enable) override { (void)enable; }
30
31 void update(ecs::Registry &registry, float dt) override
32 {
35 m_waveTimer += dt;
36
38 {
39 spawnEnemy(registry);
40 m_enemySpawnTimer = 0.0f;
41 }
42
43 if (m_waveTimer >= 10.0f)
44 {
45 spawnWave(registry);
46 m_waveTimer = 0.0f;
47 }
48
50 {
53 }
54 }
55
56 private:
58 float m_enemySpawnTimer = 0.0f;
60 float m_waveTimer = 0.0f;
61
62 void spawnEnemy(ecs::Registry &registry)
63 {
65 float y = static_cast<float>(
67 (std::rand() % static_cast<int>(GameConfig::Screen::MAX_Y - GameConfig::Screen::MIN_Y)));
68
69 registry.createEntity()
70 .with<ecs::Transform>("enemy_transform", x, y, 0.0f)
71 .with<ecs::Velocity>("enemy_velocity", -GameConfig::Enemy::Easy::SPEED, 0.0f)
72 .with<ecs::Rect>("enemy_rect", 0.0f, 0.0f, static_cast<int>(GameConfig::Enemy::Easy::SPRITE_WIDTH),
74 .with<ecs::Scale>("enemy_scale", GameConfig::Enemy::Easy::SCALE, GameConfig::Enemy::Easy::SCALE)
75 .with<ecs::Texture>("enemy_texture", Path::Texture::TEXTURE_ENEMY_EASY)
76 .with<ecs::Animation>("enemy_animation", 0, GameConfig::Enemy::Easy::ANIMATION_FRAMES,
84 .with<ecs::Hitbox>("enemy_hitbox", GameConfig::Hitbox::ENEMY_RADIUS)
85 .build();
86 }
87
89 {
91 float y = static_cast<float>(
93 (std::rand() % static_cast<int>(GameConfig::Screen::MAX_Y - GameConfig::Screen::MIN_Y)));
94
95 registry.createEntity()
96 .with<ecs::Transform>("asteroid_transform", x, y, 0.0f)
97 .with<ecs::Velocity>("asteroid_velocity", -GameConfig::Asteroid::Small::SPEED, 0.0f)
98 .with<ecs::Rect>("asteroid_rect", 0.0f, 0.0f,
101 .with<ecs::Scale>("asteroid_scale", GameConfig::Asteroid::Small::SCALE,
103 .with<ecs::Texture>("asteroid_texture", Path::Texture::TEXTURE_ASTEROID)
104 .with<ecs::Animation>("asteroid_animation", 0, GameConfig::Asteroid::Small::ANIMATION_FRAMES,
109 .with<ecs::Asteroid>("asteroid", size, GameConfig::Asteroid::Small::ROTATION_SPEED,
112 .build();
113 }
114
115 void spawnWave(ecs::Registry &registry)
116 {
117 int waveSize = 5 + (std::rand() % 4);
118
119 for (int i = 0; i < waveSize; ++i)
120 {
121 float x = GameConfig::Screen::SPAWN_X + (i * 100.0f);
122 float y = static_cast<float>(
124 (std::rand() % static_cast<int>(GameConfig::Screen::MAX_Y - GameConfig::Screen::MIN_Y)));
125
126 registry.createEntity()
127 .with<ecs::Transform>("enemy_wave_transform", x, y, 0.0f)
128 .with<ecs::Velocity>("enemy_wave_velocity", -GameConfig::Enemy::Easy::SPEED, 0.0f)
129 .with<ecs::Rect>("enemy_wave_rect", 0.0f, 0.0f,
132 .with<ecs::Scale>("enemy_wave_scale", GameConfig::Enemy::Easy::SCALE,
134 .with<ecs::Texture>("enemy_wave_texture", Path::Texture::TEXTURE_ENEMY_EASY)
135 .with<ecs::Animation>("enemy_wave_animation", 0,
141 .with<ecs::Enemy>("enemy_wave", GameConfig::Enemy::Easy::HEALTH,
144 .with<ecs::Hitbox>("enemy_wave_hitbox", GameConfig::Hitbox::ENEMY_RADIUS)
145 .build();
146 }
147 }
148 }; // class SpawnSystem
149
150} // namespace cli
This file contains the component definitions.
Configuration constants for the game.
This file contains the IRenderer class declaration.
This file contains the Registry class declaration.
SpawnSystem(SpawnSystem &&)=delete
float m_enemySpawnTimer
Definition Spawn.hpp:58
bool isEnable() override
Definition Spawn.hpp:28
SpawnSystem & operator=(const SpawnSystem &)=delete
~SpawnSystem() override=default
void update(ecs::Registry &registry, float dt) override
Definition Spawn.hpp:31
void spawnWave(ecs::Registry &registry)
Definition Spawn.hpp:115
float m_waveTimer
Definition Spawn.hpp:60
void spawnAsteroid(ecs::Registry &registry, ecs::Asteroid::Size size)
Definition Spawn.hpp:88
SpawnSystem(const SpawnSystem &)=delete
SpawnSystem(eng::IRenderer &renderer)
Definition Spawn.hpp:20
SpawnSystem & operator=(SpawnSystem &&)=delete
void spawnEnemy(ecs::Registry &registry)
Definition Spawn.hpp:62
void setEnable(bool enable) override
Definition Spawn.hpp:29
float m_asteroidSpawnTimer
Definition Spawn.hpp:59
eng::IRenderer & m_renderer
Definition Spawn.hpp:57
EntityBuilder & with(Args &&...args)
Definition Registry.hpp:40
Class for managing entities and their components.
Definition Registry.hpp:25
EntityBuilder createEntity()
Definition Registry.hpp:53
Interface for the renderer.
constexpr float SHOOT_COOLDOWN
constexpr float SPRITE_HEIGHT
constexpr float ANIMATION_DURATION
constexpr float ASTEROID_SMALL_RADIUS
constexpr float ENEMY_RADIUS
constexpr float MIN_Y
constexpr float MAX_Y
constexpr float SPAWN_X
constexpr auto TEXTURE_ASTEROID
Definition Common.hpp:68
constexpr auto TEXTURE_ENEMY_EASY
Definition Common.hpp:67