vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
particle_system.h
1#pragma once
2
3#include <utility>
4#include <vector>
5
6#include <glm/glm.hpp>
7
8#include "core/math/random.h"
9#include "core/system.h"
10#include "ecs/component/render/particle_emitter.h"
11#include "system/particle/live_particles.h"
12#include "system/physics/query/query.h"
13
14namespace Vkm::Engine {
15
16class Scene;
17
26EntityId triggeringEmitter(const Scene& scene, EntityId child);
27
38class ParticleSystem : public System {
39 public:
40 static constexpr float SWIRL_REACH = 1.5f;
41
42 public:
43 ParticleSystem() = default;
44 ~ParticleSystem() override = default;
45
46 ParticleSystem(const ParticleSystem& other) = delete;
47 ParticleSystem& operator=(const ParticleSystem& other) = delete;
48
49 ParticleSystem(ParticleSystem && other) = delete;
50 ParticleSystem& operator=(ParticleSystem && other) = delete;
51
52 public:
53 void update(FrameContext& ctx) override;
54
64 static glm::vec3 swirlAt(const glm::vec3& position, float time);
65
66 private:
67 static constexpr uint64_t SEED = 0x9E3779B97F4A7C15ULL;
68
70 struct TriggeredSpawn {
71 EntityId emitter;
72 glm::vec3 position;
73 glm::vec3 velocity;
74 uint32_t count;
75 };
76
78 struct ChildLink {
79 uint32_t parentSlot;
80 EntityId child;
81 ParticleTrigger trigger;
82 float inheritVelocity;
83 };
84
86 struct Origin {
87 glm::vec3 position;
88 glm::mat3 frame;
89 glm::vec3 velocity;
90 };
91
92 private:
102 bool spawn(
103 LiveParticles::Pool& pool,
104 const ParticleEmitter& emitter,
105 const Origin& origin,
106 float age
107 );
108
116 void trigger(EntityId emitter, ParticleEvent event, const Particle& particle);
117
124 std::pair<const ChildLink*, const ChildLink*> childrenOf(uint32_t slot) const;
125
132 bool quiet(uint32_t slot) const;
133
143 void step(
144 FrameContext& ctx,
145 EntityId id,
146 const ParticleEmitter& emitter,
147 LiveParticles::Pool& pool,
148 float dt
149 );
150
151 private:
159 Math::Rng m_rng{SEED};
160
161 LiveParticles m_live;
162 uint64_t m_step = 0;
163 uint64_t m_epoch = 0;
164 float m_time = 0.0f;
165
166 std::vector<EntityId> m_spent;
167
169 std::vector<ChildLink> m_links;
170 std::vector<TriggeredSpawn> m_triggered;
171 QueryParts m_struck;
172};
173
174} // namespace Vkm::Engine
void update(FrameContext &ctx) override
Execute this system for the current frame.
static constexpr float SWIRL_REACH
The most any axis of swirlAt reaches.
Definition particle_system.h:40
static glm::vec3 swirlAt(const glm::vec3 &position, float time)
The swirl the turbulence pushes along, at position and time.
Central registry managing entities and an open set of component types.
Definition scene.h:29
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
Per-frame state bundle passed to every system.
Definition system.h:57
Makes a child entity's ParticleEmitter a sub-emitter of its parent's.
Definition particle_emitter.h:165