r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Spawn.hpp
Go to the documentation of this file.
1///
2/// @file Spawn.hpp
3/// @brief This file contains the spawn system definitions
4/// @namespace gme
5///
6
7#pragma once
8
9#include "ECS/Component.hpp"
11#include "ECS/Registry.hpp"
13#include "Utils/Common.hpp"
14#include "Utils/HitboxUtils.hpp"
16
17namespace gme
18{
19
20 class SpawnSystem final : public ecs::ASystem
21 {
22 public:
23 explicit SpawnSystem(const std::shared_ptr<eng::IRenderer> &renderer) : m_renderer(renderer) {}
24 ~SpawnSystem() override = default;
25
26 SpawnSystem(const SpawnSystem &) = delete;
27 SpawnSystem &operator=(const SpawnSystem &) = delete;
30
31 bool isEnable() override { return true; }
32 void setEnable(const bool enable) override { (void)enable; }
33
34 void update(ecs::Registry &registry, float dt) override
35 {
37 m_waveTimer += dt;
38
40 {
41 spawnEnemy(registry);
42 m_enemySpawnTimer = 0.0f;
43 }
44
45 if (m_waveTimer >= 10.0f)
46 {
47 spawnWave(registry);
48 m_waveTimer = 0.0f;
49 }
50 }
51
52 private:
53 const std::shared_ptr<eng::IRenderer> &m_renderer;
54 float m_enemySpawnTimer = 0.0F;
55 float m_waveTimer = 0.0F;
56
57 static void spawnEnemy(ecs::Registry &registry)
58 {
60 float y =
62 (std::rand() % static_cast<int>(utl::GameConfig::Screen::MAX_Y - utl::GameConfig::Screen::MIN_Y));
63
64 auto [offsetX, offsetY] = utl::calculateHitboxOffsetsRelative(
67
68 registry.createEntity()
69 .with<ecs::Transform>("enemy_transform", x, y, 0.0f)
70 .with<ecs::Velocity>("enemy_velocity", -utl::GameConfig::Enemy::Easy::SPEED, 0.0f)
71 .with<ecs::Rect>("enemy_rect", 0.0f, 0.0f,
74 .with<ecs::Scale>("enemy_scale", utl::GameConfig::Enemy::Easy::SCALE,
77 .with<ecs::Animation>("enemy_animation", 0, utl::GameConfig::Enemy::Easy::ANIMATION_FRAMES,
82 .with<ecs::Enemy>("enemy", utl::GameConfig::Enemy::Easy::HEALTH,
86 .with<ecs::Hitbox>("enemy_hitbox", utl::GameConfig::Hitbox::ENEMY_RADIUS, offsetX, offsetY)
87 .build();
88 }
89
90 static void spawnWave(ecs::Registry &registry)
91 {
92 const int waveSize = 5 + (std::rand() % 4);
93
94 for (int i = 0; i < waveSize; ++i)
95 {
96 float x = utl::GameConfig::Screen::SPAWN_X + (i * 100.0f);
98 (std::rand() %
100 auto [offsetX, offsetY] = utl::calculateHitboxOffsetsRelative(
103
104 registry.createEntity()
105 .with<ecs::Transform>("enemy_wave_transform", x, y, 0.0f)
106 .with<ecs::Velocity>("enemy_wave_velocity", -utl::GameConfig::Enemy::Easy::SPEED, 0.0f)
107 .with<ecs::Rect>("enemy_wave_rect", 0.0f, 0.0f,
110 .with<ecs::Scale>("enemy_wave_scale", utl::GameConfig::Enemy::Easy::SCALE,
112 .with<ecs::Texture>("enemy_wave_texture", utl::Path::Texture::TEXTURE_ENEMY_EASY)
113 .with<ecs::Animation>("enemy_wave_animation", 0,
123 .with<ecs::Hitbox>("enemy_wave_hitbox", utl::GameConfig::Hitbox::ENEMY_RADIUS, offsetX, offsetY)
124 .build();
125 }
126 }
127 }; // class SpawnSystem
128} // namespace gme
This file contains the component definitions.
Configuration constants for the multiplayer game.
Utility functions for hitbox calculations.
This file contains the IRenderer class declaration.
This file contains the interface for systems.
This file contains the Registry class declaration.
Abstract class for system.
Definition ISystems.hpp:34
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
static void spawnWave(ecs::Registry &registry)
Definition Spawn.hpp:90
SpawnSystem & operator=(const SpawnSystem &)=delete
void update(ecs::Registry &registry, float dt) override
Definition Spawn.hpp:34
void setEnable(const bool enable) override
Definition Spawn.hpp:32
SpawnSystem(const std::shared_ptr< eng::IRenderer > &renderer)
Definition Spawn.hpp:23
static void spawnEnemy(ecs::Registry &registry)
Definition Spawn.hpp:57
float m_waveTimer
Definition Spawn.hpp:55
~SpawnSystem() override=default
float m_enemySpawnTimer
Definition Spawn.hpp:54
SpawnSystem(SpawnSystem &&)=delete
SpawnSystem & operator=(SpawnSystem &&)=delete
const std::shared_ptr< eng::IRenderer > & m_renderer
Definition Spawn.hpp:53
SpawnSystem(const SpawnSystem &)=delete
bool isEnable() override
Definition Spawn.hpp:31
This file contains common definitions and constants.
constexpr float SPRITE_HEIGHT
constexpr float ANIMATION_DURATION
constexpr float SHOOT_COOLDOWN
constexpr float ENEMY_RADIUS
constexpr float MAX_Y
constexpr float MIN_Y
constexpr float SPAWN_X
constexpr auto TEXTURE_ENEMY_EASY
Definition Common.hpp:118
std::pair< float, float > calculateHitboxOffsetsRelative(const float x, const float y, const float spriteWidth, const float spriteHeight, const float scale)
Calculate hitbox offsets relative to sprite position.