3#include "Logger/Logger.hpp"
4#include "NetworkIdGenerator.hpp"
5#include "components/Transform.hpp"
6#include "factoryDataDriven/factoryDataDriven.hpp"
12namespace rtype::events {
30namespace rtk::systems {
44 std::function<bool()> _canSpawn;
46 float _spawnTimer{0.0f};
47 float _spawnInterval{1.5f};
50 std::uniform_real_distribution<float> _distX;
51 std::uniform_real_distribution<float> _distY;
61 std::function<
bool()> canSpawn =
nullptr)
62 : _factory(factory), _eventBus(eventBus), _canSpawn(std::move(canSpawn)),
63 _rng(std::random_device{}()), _distX(1920.0f, 2920.0f),
64 _distY(200.0f, 400.0f) {}
69 void onStart(rtk::ecs::Registry &)
override {
70 LOG_INFO(
"Load Enemy Spawner System (Server Side)");
78 void update(rtk::ecs::Registry ®,
float dt)
override {
79 if (_canSpawn && !_canSpawn())
84 if (_spawnTimer >= _spawnInterval) {
90 auto enemyEntity = _factory.
createNetwork(reg,
"enemyBidos", netId);
95 transform->position.x = _distX(_rng);
96 transform->position.y = _distY(_rng);
99 netId, transform->position.x, transform->position.y});
107 void onStop(rtk::ecs::Registry &)
override {}
Type indexed publish and subscribe event bus.
Definition busEvents.hpp:20
void publish(const E &event)
Calls every handler registered for the event type.
Definition busEvents.hpp:52
Builds entities from JSON blueprints and registered component builders.
Definition factoryDataDriven.hpp:54
std::size_t createNetwork(rtk::ecs::Registry ®, ArchetypeID archetypeId, std::uint32_t networkId)
Spawns a replicated entity and attaches its NetworkEntity.
Definition factoryDataDriven.hpp:228
Spawns a Bydos enemy at a random position right of the screen every 1.5 seconds.
Definition EnemySpawnerSystem.hpp:40
EnemySpawnerSystem(rtk::factory::EntityFactory &factory, busEvent &eventBus, std::function< bool()> canSpawn=nullptr)
Creates the spawner.
Definition EnemySpawnerSystem.hpp:60
void onStart(rtk::ecs::Registry &) override
Logs that the system is loaded.
Definition EnemySpawnerSystem.hpp:69
void update(rtk::ecs::Registry ®, float dt) override
Advances the spawn timer and spawns an enemy when it expires.
Definition EnemySpawnerSystem.hpp:78
void onStop(rtk::ecs::Registry &) override
Does nothing.
Definition EnemySpawnerSystem.hpp:107
static NetworkIdGenerator & getInstance()
Returns the single generator instance.
Definition NetworkIdGenerator.hpp:26
std::uint32_t getNextId()
Returns a new id, thread safe.
Definition NetworkIdGenerator.hpp:38
Published by the server when a new enemy is spawned.
Definition EnemySpawnerSystem.hpp:21
float x
Spawn position on the x axis, in pixels.
Definition EnemySpawnerSystem.hpp:23
std::uint32_t networkId
Network id of the enemy.
Definition EnemySpawnerSystem.hpp:22
float y
Spawn position on the y axis, in pixels.
Definition EnemySpawnerSystem.hpp:24