Add emission to ParticleEmitter
This commit is contained in:
@@ -64,6 +64,11 @@ typedef struct ParticleEmitter {
|
||||
Vector2 pos;
|
||||
|
||||
struct ParticleEmitterData {
|
||||
// emission
|
||||
f32 emmitRate;
|
||||
f32 emmitVariance;
|
||||
f32 emmitVarianceMin, emmitVarianceMax;
|
||||
|
||||
// offset
|
||||
Vector2 minOffset, maxOffset;
|
||||
// startVel
|
||||
|
||||
24
game/main.c
24
game/main.c
@@ -567,6 +567,30 @@ static void renderGame(Game *game, float dt) {
|
||||
drawPlayerInputUI();
|
||||
|
||||
EndMode2D();
|
||||
|
||||
InputState *input = ecs_singleton_get_mut(ECS, InputState);
|
||||
if (IsKeyReleased(KEY_SPACE)) {
|
||||
ParticleEmitter emitter = {
|
||||
.emitterLifetime = 2.0f,
|
||||
.targetParticles = ecs_id(ParticleLayer1),
|
||||
.data.emmitRate = 1.2f,
|
||||
.data.emmitVariance = 1.0f,
|
||||
.data.emmitVarianceMin = 0.0f,
|
||||
.data.emmitVarianceMax = 1.0f,
|
||||
.pos = input->mouseWorld,
|
||||
.data.startColor = { 210, 0, 0, 210 },
|
||||
.data.endColor = { 110, 10, 10, 110 },
|
||||
.data.minLifetime = 1.0f,
|
||||
.data.maxLifetime = 2.2f,
|
||||
.data.minStartSize = 4.0f,
|
||||
.data.maxStartSize = 6.0f,
|
||||
.data.tileID = getParticleTypeTile(PARTICLE_CIRCLE),
|
||||
.data.blend = BLEND_ADDITIVE
|
||||
};
|
||||
ecs_entity_t e = entityCreateEmpty();
|
||||
ecs_set_ptr(ECS, e, ParticleEmitter, &emitter);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void render(float dt, void *userData) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "../game_state.h"
|
||||
#include "../utils.h"
|
||||
#include "../entity_factory.h"
|
||||
|
||||
#include <raymath.h>
|
||||
#include <stdlib.h>
|
||||
@@ -57,13 +58,21 @@ void updateParticleEmitter(ecs_iter_t *it) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
if (ecs_has(ECS, entity, EmitterAttachment)) {
|
||||
EmitterAttachment attachment = *ecs_get(ECS, entity, EmitterAttachment);
|
||||
if (!ecs_is_alive(ECS, attachment.baseEntity)) {
|
||||
ecs_delete(ECS, entity);
|
||||
continue;
|
||||
}
|
||||
if (ecs_is_alive(ECS, attachment.baseEntity)) {
|
||||
Vector2 pos = *ecs_get(ECS, entity, Position);
|
||||
pos = Vector2Add(pos, attachment.offset);
|
||||
emitter->pos = pos;
|
||||
emitter[i].pos = pos;
|
||||
}
|
||||
}
|
||||
|
||||
const struct ParticleEmitterData data = emitter[i].data;
|
||||
|
||||
i32 emmit = data.emmitRate + data.emmitVariance * randFloatRange(data.emmitVarianceMin, data.emmitVarianceMax);
|
||||
for (i32 pIdx = 0; pIdx < emmit; pIdx++) {
|
||||
ecs_entity_t pEntity = entityCreateEmpty();
|
||||
ecs_set_ptr(ECS, pEntity, ParticleLayer0, NULL);
|
||||
Particle particle = spawnParticle(&emitter[i]);
|
||||
ecs_set_id(ECS, pEntity, emitter[i].targetParticles, sizeof(particle), &particle);
|
||||
}
|
||||
|
||||
emitter[i].emitterElapsed += dt;
|
||||
|
||||
@@ -192,6 +192,8 @@ void setupSystems() {
|
||||
ECS_SYSTEM(ECS, renderHealthBar, EcsOnUpdate, Position, HitBox, Health);
|
||||
ECS_SYSTEM(ECS, renderFloatyTextParticle, EcsOnUpdate, FloatyTextParticle);
|
||||
|
||||
ECS_SYSTEM(ECS, updateParticleEmitter, EcsOnUpdate, ParticleEmitter);
|
||||
|
||||
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
|
||||
|
||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, HitBox);
|
||||
|
||||
Reference in New Issue
Block a user