VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
UtilitySystem.hpp
2#include "components/GameComponents/UtilityComponents.hpp"
3#include <cmath>
4
5namespace vex {
6
13void ProcessParticles(vex::Registry& registry, float deltaTime) {
15 particleView.each([&](vex::Entity entity, TransformComponent& trans, ParticleEmitterComponent& emit) {
16 trans.recalculateMatrix();
17
18 emit.spawnTimer += deltaTime;
19 static int frame = 0;
20 if (frame++ % 60 == 0) vex::log(LogLevel::INFO, "ProcessParticles running... CPU count: %d, spawnTimer: %f", (int)emit.cpuParticles.size(), emit.spawnTimer);
21 if (emit.spawnRate <= 0.001f) emit.spawnRate = 0.001f;
22 while (emit.spawnTimer >= emit.spawnRate) {
23 emit.spawnTimer -= emit.spawnRate;
24 if(!emit.active) break;
25 Particle p;
26 p.position = trans.getWorldPosition();
27
28 float rx = ((rand() % 200) / 100.0f) - 1.0f;
29 float ry = ((rand() % 200) / 100.0f) - 1.0f;
30 float rz = ((rand() % 200) / 100.0f) - 1.0f;
31
32 p.velocity = emit.initialVelocity + (emit.velocityVariation * glm::vec3(rx, ry, rz));
33
34 float rLife = ((rand() % 200) / 100.0f) - 1.0f;
35 p.life = p.startingLife = emit.particleLife + (emit.particleLifeVariation * rLife);
36
37 p.startSize = emit.startSize;
38 p.endSize = emit.endSize;
39 p.startColor = emit.startColor;
40 p.endColor = emit.endColor;
41
42 emit.cpuParticles.push_back(p);
43 }
44
45 emit.activeParticles.clear();
46 for (auto it = emit.cpuParticles.begin(); it != emit.cpuParticles.end(); ) {
47 it->life -= deltaTime;
48 if (it->life <= 0.0f) {
49 it = emit.cpuParticles.erase(it);
50 } else {
51 it->velocity += emit.gravity * deltaTime;
52 it->position += it->velocity * deltaTime;
53
54 float t = 1.0f - (it->life / it->startingLife);
55 float currentSize = glm::mix(it->startSize, it->endSize, t);
56
57 glm::vec4 sColor(it->startColor.r, it->startColor.g, it->startColor.b, it->startColor.a);
58 glm::vec4 eColor(it->endColor.r, it->endColor.g, it->endColor.b, it->endColor.a);
59 glm::vec4 currentColor = glm::mix(sColor, eColor, t);
60
61 ParticleGPUData gpuData{};
62 gpuData.position = glm::vec4(it->position, 1.0f);
63 gpuData.scaleX = currentSize;
64 gpuData.scaleY = currentSize;
65 gpuData.color = currentColor;
66 gpuData.isUnlit = emit.isUnlit ? 1 : 0;
67
68 emit.activeParticles.push_back(gpuData);
69 ++it;
70 }
71 }
72 });
73}
74
75
76void ProcessUtilityComponents(vex::Registry& registry, float deltaTime, Engine& engine) {
78 oscView.each([&](vex::Entity entity, OscillatorComponent& osc, TransformComponent& transform) {
79 if (!osc.initialized) {
80 osc.startPosition = transform.getLocalPosition();
81 osc.initialized = true;
82 }
83
84 osc.currentTime += deltaTime;
85 float wave = std::sin((osc.currentTime + osc.timeOffset) * osc.frequency) * osc.amplitude;
86
87 transform.setLocalPosition(osc.startPosition + (osc.axis * wave));
88 });
89
90 vex::View<TweenComponent, TransformComponent> tweenView(registry);
91 tweenView.each([&](vex::Entity entity, TweenComponent& tween, TransformComponent& transform) {
92 if (!tween.initialized) {
93 tween.startPosition = transform.getLocalPosition();
94 tween.initialized = true;
95 }
96
97 tween.timeElapsed += deltaTime;
98 float t = std::clamp(tween.timeElapsed / tween.duration, 0.0f, 1.0f);
99
100 float ease = t * t * (3.0f - 2.0f * t);
101
102 glm::vec3 currentTarget = tween.returning ? tween.startPosition : (tween.startPosition + tween.targetLocalOffset);
103 glm::vec3 currentStart = tween.returning ? (tween.startPosition + tween.targetLocalOffset) : tween.startPosition;
104
105 transform.setLocalPosition(glm::mix(currentStart, currentTarget, ease));
106
107 if (t >= 1.0f) {
108 if (tween.pingPong) {
109 tween.returning = !tween.returning;
110 tween.timeElapsed = 0.0f;
111 }
112 }
113 });
114
115 vex::View<LifetimeComponent> lifetimeView(registry);
116 lifetimeView.each([&](vex::Entity entity, LifetimeComponent& lifetime) {
117 lifetime.timeElapsed += deltaTime;
118
119 if (lifetime.timeElapsed >= lifetime.lifespan) {
120 for (const auto& sceneName : engine.getSceneManager()->GetAllSceneNames()) {
121 Scene* scene = engine.getSceneManager()->GetScene(sceneName);
122 if (scene) {
123 GameObject* obj = scene->GetGameObjectByEntity(entity);
124 if (obj) {
125 scene->DestroyGameObject(obj);
126 break;
127 }
128 }
129 }
130 }
131 });
132}
133
134} // namespace vex
Particle emitter component.
Its base class for all game objects, eg. Player, Enemy, Weapon. Your Class needs to inherit from it.
Central registry for managing entities and their components in the ECS system.
Definition Registry.hpp:29
Scene class implements scene functionality like loading and holding game objects.
Definition Scene.hpp:22
Provides iteration over entities that have all specified component types.
Definition View.hpp:17
void each(Func func)
Iterates over all entities with the specified component types.
Definition View.hpp:40
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
Definition Types.hpp:11
void VEX_EXPORT log(const char *fmt,...)
Logs a formatted message.
void ProcessParticles(vex::Registry &registry, float deltaTime)
Processes CPU-side particle simulation and prepares GPU data.
Destroys the entity after a set amount of time. Useful for temporary effects.
Component that emits and manages a system of particles.
bool active
Indicates if the emitter is active and emitting particles.
vex::rgba startColor
Color of particles at spawn.
float particleLifeVariation
Random variation range applied to lifetime.
float particleLife
Base lifetime for new particles.
glm::vec3 velocityVariation
Random variation range applied to initial velocity.
std::vector< ParticleGPUData > activeParticles
GPU data buffer built every frame for rendering.
float spawnTimer
Internal timer to track time since last spawn.
float startSize
Base size for new particles.
std::vector< Particle > cpuParticles
Internal list of active CPU particles.
vex::rgba endColor
Color of particles at the end of their life.
bool isUnlit
Indicates if the particles should ignore lighting.
float endSize
Size of particles at the end of their life.
glm::vec3 initialVelocity
Base starting velocity for new particles.
glm::vec3 gravity
Gravity applied to particles.
float spawnRate
Time in seconds between particle spawns.
Structure passed to the GPU containing per-particle rendering data.
uint32_t isUnlit
Flag determining if the particle is unlit (1) or affected by lighting (0).
float scaleY
Particle vertical scale.
float scaleX
Particle horizontal scale.
glm::vec4 color
Particle color (rgba).
glm::vec4 position
Particle position (xyz) and unused (w).
CPU-side representation of a single particle during its lifetime.
vex::rgba startColor
Color at spawn.
float startingLife
Total lifetime in seconds.
vex::rgba endColor
Color at end of life.
glm::vec3 velocity
Current velocity.
float startSize
Size at spawn.
float endSize
Size at end of life.
float life
Remaining lifetime in seconds.
glm::vec3 position
Current world position.
Struct containing transform data and methods.
glm::vec3 getWorldPosition()
Method to get world position, needed when object is parented as position parameter stores local posit...
Smoothly interpolates an entity's local position over time.