81 lines
1.9 KiB
C
81 lines
1.9 KiB
C
#include "particle.h"
|
|
#include "conf.h"
|
|
#include "level.h"
|
|
#include "lzy.h"
|
|
#include "tools.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static struct Particle_table particle_table;
|
|
|
|
static void particle_table_free(void);
|
|
static void particle_update(struct Particle *particle);
|
|
static void particle_draw(struct Particle *particle, int timer);
|
|
|
|
void particle_table_init(void) {
|
|
particle_table_free();
|
|
particle_table.particles = malloc(MAX_PARTICLES * sizeof(struct Particle));
|
|
particle_table.n = 0;
|
|
|
|
for (int i = 0; i < MAX_PARTICLES; ++i) {
|
|
particle_table.particles[i].active = 0;
|
|
}
|
|
}
|
|
|
|
void particle_table_free(void) {
|
|
if (particle_table.particles != NULL) {
|
|
particle_table.particles = NULL;
|
|
}
|
|
};
|
|
|
|
void particle_new(int x, int y, int id, int life) {
|
|
for (int i = 0; i < MAX_PARTICLES; ++i) {
|
|
if (!particle_table.particles[i].active) {
|
|
particle_table.particles[i] = (struct Particle){
|
|
.pos = (struct Vec2){.x = x - TILE_S / 2, .y = y - TILE_S / 2},
|
|
.id = id,
|
|
.life = life,
|
|
.active = 1};
|
|
break;
|
|
}
|
|
}
|
|
++particle_table.n;
|
|
}
|
|
|
|
void particle_table_update(void) {
|
|
for (int i = 0; i < MAX_PARTICLES; ++i) {
|
|
if (particle_table.particles[i].active) {
|
|
particle_update(&particle_table.particles[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void particle_update(struct Particle *particle) {
|
|
particle->life -= 1;
|
|
/* boom */
|
|
const struct Vec2 level_dim = level_get_dim();
|
|
if (particle->life < 0) {
|
|
particle_destroy(particle);
|
|
}
|
|
}
|
|
|
|
void particle_table_draw(int timer) {
|
|
for (int i = 0; i < MAX_PARTICLES; ++i) {
|
|
if (particle_table.particles[i].active) {
|
|
particle_draw(&particle_table.particles[i], timer);
|
|
}
|
|
}
|
|
}
|
|
|
|
void particle_draw(struct Particle *particle, int timer) {
|
|
LZY_DrawTile(particle->id + 1, particle->pos.x, particle->pos.y);
|
|
}
|
|
|
|
void particle_destroy(struct Particle *particle) {
|
|
particle->active = 0;
|
|
--particle_table.n;
|
|
}
|
|
|
|
int particle_nb(void) { return particle_table.n; }
|