From 8d2ba1419c060078c0aeeb33ef4e84da482b78f5 Mon Sep 17 00:00:00 2001 From: quou Date: Wed, 2 Oct 2024 21:23:04 +1000 Subject: particle effects --- Makefile | 1 + effect.c | 40 ++++++++++++++++++++++++++++++++++++++++ obj.h | 13 +++++++++++++ world.c | 20 ++++++++++++++++++++ world.h | 12 +++++++++++- 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 effect.c diff --git a/Makefile b/Makefile index 06f7573..65f4b5d 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,7 @@ sources = \ animation.c \ asset.c \ deathzone.c \ + effect.c \ enemy.c \ hud.c \ map.c \ diff --git a/effect.c b/effect.c new file mode 100644 index 0000000..9db1608 --- /dev/null +++ b/effect.c @@ -0,0 +1,40 @@ +#include "config.h" +#include "maths.h" +#include "obj.h" +#include "random.h" +#include "render.h" + +void init_effect(Effect* e, int x, int y, int vx, int vy, int c) { + int i; + e->c = c; + for (i = 0; i < c; i++) { + const int a = get_r() & 0xff; + const int sa = sin_table[a]; + const int ca = cos_table[a]; + e->x[i] = x; + e->y[i] = y; + e->vx[i] = vx + ca - sa + get_rf(); + e->vy[i] = vy + sa + ca + get_rf(); + } + e->life = 100; +} + +int update_effect(Effect* e) { + int i; + for (i = 0; i < e->c; i++) { + e->vy[i] += main_gravity; + e->x[i] += e->vx[i]; + e->y[i] += e->vy[i]; + } + return !(e->life--); +} + +void ren_effect(const Effect* e, Renderer* r) { + Rect re = { 0, 0, 1, 1 }; + int i; + for (i = 0; i < e->c; i++) { + re.x = e->x[i] >> fbits; + re.y = e->y[i] >> fbits; + ren_rect(r, &re); + } +} diff --git a/obj.h b/obj.h index ad7098c..f29d5a4 100644 --- a/obj.h +++ b/obj.h @@ -65,6 +65,19 @@ void init_particle(Particle* p, int x, int y, int anim, int bmp); int update_particle(Particle* p); void ren_particle(const Particle* p, struct Renderer* r); +#define effect_max 32 +typedef struct { + int x[effect_max]; + int y[effect_max]; + int vx[effect_max]; + int vy[effect_max]; + int c, life; +} Effect; + +void init_effect(Effect* e, int x, int y, int vx, int vy, int c); +int update_effect(Effect* e); +void ren_effect(const Effect* e, struct Renderer* r); + typedef enum { enemy_demon } Enemy_Type; diff --git a/world.c b/world.c index b1f73ec..1881df2 100644 --- a/world.c +++ b/world.c @@ -54,6 +54,21 @@ Deathzone* inst_deathzone( return d; } +Effect* inst_effect( + World* w, + int x, + int y, + int vx, + int vy, + int c +) { + Effect* e; + assert(w->effect_count < max_effects); + e = &w->effects[w->effect_count++]; + init_effect(e, x, y, vx, vy, c); + return e; +} + void update_world(World* w, const App* a) { int i; if (w->laser.life) @@ -69,6 +84,9 @@ void update_world(World* w, const App* a) { for (i = w->particle_count - 1; i >= 0; i--) if (update_particle(&w->particles[i])) w->particles[i] = w->particles[--w->particle_count]; + for (i = w->effect_count - 1; i >= 0; i--) + if (update_effect(&w->effects[i])) + w->effects[i] = w->effects[--w->effect_count]; for (i = w->enemy_count - 1; i >= 0; i--) if (update_enemy(&w->enemies[i], w)) w->enemies[i] = w->enemies[--w->enemy_count]; @@ -81,6 +99,8 @@ void ren_world(const World* w, struct Renderer* r) { render_map(&w->map, r); for (i = 0; i < w->particle_count; i++) ren_particle(&w->particles[i], r); + for (i = 0; i < w->effect_count; i++) + ren_effect(&w->effects[i], r); for (i = 0; i < w->enemy_count; i++) ren_enemy(&w->enemies[i], r); if (w->laser.life) diff --git a/world.h b/world.h index 05372ae..81feb6f 100644 --- a/world.h +++ b/world.h @@ -7,6 +7,7 @@ #define max_particles 32 #define max_enemies 16 #define max_deathzones 16 +#define max_effects 16 struct Renderer; @@ -14,7 +15,8 @@ typedef struct World { Particle particles[max_particles]; Enemy enemies[max_enemies]; Deathzone deathzones[max_deathzones]; - int particle_count, enemy_count; + Effect effects[max_effects]; + int particle_count, enemy_count, effect_count; int deathzone_count; Player player; Laser laser; @@ -45,6 +47,14 @@ Deathzone* inst_deathzone( int life, int friendly ); +Effect* inst_effect( + World* w, + int x, + int y, + int vx, + int vy, + int c +); void update_world(World* w, const struct App* a); void ren_world(const World* w, struct Renderer* r); -- cgit v1.2.3-54-g00ecf