61 lines
1016 B
C
61 lines
1016 B
C
#include "self_destruct.h"
|
|
|
|
#include "entity.h"
|
|
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
#define MAX 256U
|
|
|
|
typedef struct {
|
|
unsigned entity_id;
|
|
unsigned component_id;
|
|
unsigned rem;
|
|
} cmp_t;
|
|
|
|
static unsigned count;
|
|
static cmp_t cmps[MAX];
|
|
|
|
static void update(unsigned new_entity_id, void *ref)
|
|
{
|
|
cmp_t *c = (cmp_t *)ref;
|
|
c->entity_id = new_entity_id;
|
|
}
|
|
|
|
static void remove(void *ref)
|
|
{
|
|
cmp_t *c = (cmp_t *)ref;
|
|
const cmp_t *last = cmps + (count - 1);
|
|
if (c < last) {
|
|
memcpy(c, last, sizeof(cmp_t));
|
|
entity_update_component(c->entity_id, c->component_id, c);
|
|
}
|
|
--count;
|
|
}
|
|
|
|
void self_destruct_clear()
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
void self_destruct_update()
|
|
{
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
if (cmps[i].rem == 0)
|
|
entity_mark(cmps[i].entity_id);
|
|
else
|
|
--cmps[i].rem;
|
|
}
|
|
}
|
|
|
|
void self_destruct_add(unsigned id, unsigned frames)
|
|
{
|
|
assert(count < MAX);
|
|
cmp_t *c = cmps + count++;
|
|
*c = (cmp_t) {
|
|
.entity_id = id,
|
|
.rem = frames,
|
|
.component_id = entity_add_component(id, update, remove, c),
|
|
};
|
|
}
|