From 28fa0bc946dd98fff611a9f44029ef9d165415ec Mon Sep 17 00:00:00 2001 From: kdx Date: Tue, 28 Mar 2023 04:07:55 +0000 Subject: parent types --- src/entity.c | 24 +++++++++++++++++++++++- src/entity.h | 2 ++ src/entityimpl.h | 5 +++-- src/entitytag.h | 1 + src/part.c | 2 +- src/player.c | 2 +- 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/entity.c b/src/entity.c index 214c066..edb949e 100644 --- a/src/entity.c +++ b/src/entity.c @@ -24,6 +24,27 @@ entity_type(const char *typename) return 0; } +unsigned int +entity_type_parent(unsigned int type) +{ + if (type == 0) + return 0; + if (entitytags[type - 1].parent != NULL) + return entity_type(entitytags[type - 1].parent); + return 0; +} + +bool +entity_is_type(unsigned int type, unsigned int search) +{ + do { + if (type == search) + return true; + type = entity_type_parent(type); + } while (type != 0); + return false; +} + bool entity_collide(Entity *this, int ox, int oy) { @@ -47,7 +68,8 @@ Entity * entity_place_meeting(Entity *this, struct Game *g, unsigned int type) { for (__auto_type i = MAX_ENTITIES - 1; i >= 0; i--) - if (this != &g->entities[i] && g->entities[i].type == type && + if (this != &g->entities[i] && + entity_is_type(g->entities[i].type, type) && entity_meet(this, &g->entities[i])) return &g->entities[i]; return NULL; diff --git a/src/entity.h b/src/entity.h index 45a1a9c..79094ea 100644 --- a/src/entity.h +++ b/src/entity.h @@ -23,6 +23,8 @@ struct Entity { }; unsigned int entity_type(const char *typename); +unsigned int entity_type_parent(unsigned int type); +bool entity_is_type(unsigned int type, unsigned int search); bool entity_collide(Entity *this, int ox, int oy); bool entity_meet(Entity *this, Entity *other); Entity *entity_place_meeting(Entity *this, struct Game *g, unsigned int type); diff --git a/src/entityimpl.h b/src/entityimpl.h index 5ed9dfa..4a71e39 100644 --- a/src/entityimpl.h +++ b/src/entityimpl.h @@ -12,9 +12,10 @@ __attribute__((constructor)) static void init_##X() { _##X = X; } \ static void X([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) -#define IMPL_INIT(X) static void init(Entity *, int, int, int, int); \ -__attribute__((constructor)) static void init_tag() { \ +#define IMPL_INIT(X,P) static void init(Entity *, int, int, int, int); \ +__attribute__((constructor)) static void init_tag(void) { \ entitytags[num_entitytags].init = init; \ + entitytags[num_entitytags].parent = P; \ entitytags[num_entitytags++].name = #X; \ } \ static void _init(Entity *this); \ diff --git a/src/entitytag.h b/src/entitytag.h index 0578089..211e365 100644 --- a/src/entitytag.h +++ b/src/entitytag.h @@ -3,6 +3,7 @@ typedef struct { const char *name; + const char *parent; void (*init)(Entity *this, int x, int y, int w, int h); } EntityTag; diff --git a/src/part.c b/src/part.c index c2d684d..86e17f9 100644 --- a/src/part.c +++ b/src/part.c @@ -1,7 +1,7 @@ #include "entityimpl.h" #include -IMPL_INIT(part) { +IMPL_INIT(part, 0) { this->width = this->height = 1 + rand() % 2; this->ignore_solids = true; this->part.life = 30 + rand() % 10; diff --git a/src/player.c b/src/player.c index 847202b..e157830 100644 --- a/src/player.c +++ b/src/player.c @@ -15,6 +15,6 @@ IMPL(update) { this->pos[1], 0, 0); } -IMPL_INIT(player) { +IMPL_INIT(player, 0) { this->height = this->width = 12; } -- cgit v1.2.3