taisei/src/entity.c

141 lines
3.6 KiB
C
Raw Normal View History

/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (c) 2011-2018, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2018, Andrei Alexeyev <akari@alienslab.net>.
*/
#include "taisei.h"
#include "entity.h"
#include "util.h"
#include "renderer/api.h"
2018-07-30 09:04:09 +02:00
#include "global.h"
static struct {
EntityInterface **array;
uint num;
uint capacity;
uint32_t total_spawns;
} entities;
#define FOR_EACH_ENT(ent) for(EntityInterface **_ent = entities.array, *ent = *entities.array; _ent < entities.array + entities.num; ent = *(++_ent))
void ent_init(void) {
memset(&entities, 0, sizeof(entities));
entities.capacity = 4096;
entities.array = calloc(entities.capacity, sizeof(EntityInterface*));
}
void ent_shutdown(void) {
if(entities.num) {
log_warn("%u entities were not properly unregistered", entities.num);
}
FOR_EACH_ENT(ent) {
ent_unregister(ent);
}
free(entities.array);
}
void ent_register(EntityInterface *ent, EntityType type) {
assert(type > _ENT_TYPE_ENUM_BEGIN && type < _ENT_TYPE_ENUM_END);
ent->type = type;
ent->index = entities.num++;
ent->spawn_id = ++entities.total_spawns;
if(ent->spawn_id == 0) {
// This is not really an error, but it may result in weird draw order
// in some corner cases. If this overflow ever actually occurs, though,
// then you've probably got much bigger problems.
log_debug("spawn_id just overflowed. You might be spawning stuff waaaay too often");
}
if(entities.capacity < entities.num) {
entities.capacity *= 2;
entities.array = realloc(entities.array, entities.capacity * sizeof(EntityInterface*));
}
entities.array[ent->index] = ent;
assert(ent->index < entities.num);
assert(entities.array[ent->index] == ent);
}
void ent_unregister(EntityInterface *ent) {
EntityInterface *sub = entities.array[--entities.num];
assert(ent->index <= entities.num);
assert(entities.array[ent->index] == ent);
entities.array[sub->index = ent->index] = sub;
}
static int ent_cmp(const void *ptr1, const void *ptr2) {
const EntityInterface *ent1 = *(const EntityInterface**)ptr1;
const EntityInterface *ent2 = *(const EntityInterface**)ptr2;
int r = (ent1->draw_layer > ent2->draw_layer) - (ent1->draw_layer < ent2->draw_layer);
if(r == 0) {
// Same layer? Put whatever spawned later on top, then.
r = (ent1->spawn_id > ent2->spawn_id) - (ent1->spawn_id < ent2->spawn_id);
}
return r;
}
void ent_draw(EntityPredicate predicate) {
qsort(entities.array, entities.num, sizeof(EntityInterface*), ent_cmp);
if(predicate) {
FOR_EACH_ENT(ent) {
ent->index = _ent - entities.array;
assert(entities.array[ent->index] == ent);
if(ent->draw_func && predicate(ent)) {
r_state_push();
ent->draw_func(ent);
r_state_pop();
}
}
} else {
FOR_EACH_ENT(ent) {
ent->index = _ent - entities.array;
assert(entities.array[ent->index] == ent);
if(ent->draw_func) {
r_state_push();
ent->draw_func(ent);
r_state_pop();
}
}
}
}
2018-07-30 09:04:09 +02:00
DamageResult ent_damage(EntityInterface *ent, const DamageInfo *damage) {
if(ent->damage_func == NULL) {
return DMG_RESULT_INAPPLICABLE;
}
Add Reimu Hakurei as a playable character (#106) * Reimu (#101) * add the reimu * Add Reimu story * account for various blunders * add reimu dialog picture * Reimu: WIP yin-yang orbs * reimu: fix up indents * Reimu: swap the shotmode names to match the kanji order in her Japanese name * Reimu: compatibility with the latest system * WIP ReimuA crap * ReimuA homing trails * more ReimuA stuff * more ReimuA adjustments + enhanced DPS stats * Reimu: stubs for new player animation sequences * Reimu: occupy the 0th character slot * Reimu: tweak needle sprite * Reimu: buff movement speed to better match Touhou * Reimu: fixup for the recent projectile changes * ReimuA: make homing shots a bit smaller; give them custom effect on collision * Reimu: add intermediate frames; move some loose sprites to the atlas * Reimu: fix compile errors * replace DBL_MAX by INFINITY * Don’t draw reimu orbs twice fixes #127 * add new reimu dialog pic * ReimuA adjustments (mostly homing); it's still OP * wip ReimuB gaps * still not sure where i'm going with these gaps * meh * Reimu: premultiplied alpha fixups after rebase * reimuB shot pattern with basic power scaling (not balanced at all) * reimuB: some lame-ass particle effects * ReimuB bomb effect prototype * reimuA bomb prototype * fix reimu shots for the new damage system * Reimu: use the new player_is_bomb_active() function, add placeholder BG for ReimuB * some reimuB bomb projectiles * ReimuB bomb bg and some framebuffer utils required to support it. Might reuse this at least in part for ReimuA unless we come up with something better. * hack to fix ReimuB bomb fade; refactoring needed * reimuA damaging bombs * fix ub * prevent nan when reimuA bombs without enemies present * add a bomb_bg to reimuA * ... * various fantasy seal tweaks * Reimu: placeholder bomb sounds; slight fantasy seal buff * fix null pointer dereference * Reimu "balance" adjustments; minor fixes * putting bandaids over gunshot wounds * Add aoe damage and bullet cancel to ReimuB's bomb * more exorcism porn * make reimu bomb bg runes better visible on dark backgrounds * More ReimuA shot changes
2018-08-11 21:13:48 +02:00
DamageResult res = ent->damage_func(ent, damage);
if(res == DMG_RESULT_OK) {
player_register_damage(&global.plr, ent, damage);
}
return res;
2018-07-30 09:04:09 +02:00
}
void ent_area_damage(complex origin, float radius, const DamageInfo *damage) {
for(Enemy *e = global.enemies.first; e; e = e->next) {
if(cabs(origin - e->pos) < radius) {
ent_damage(&e->ent, damage);
}
}
if(global.boss && cabs(origin - global.boss->pos) < radius) {
ent_damage(&global.boss->ent, damage);
}
}