From 9e00511b26ab8b47f33cb3c64da10aa1e3f71295 Mon Sep 17 00:00:00 2001 From: kdx Date: Thu, 23 Mar 2023 16:16:22 +0100 Subject: i'm not sorry --- src/background.c | 2 +- src/entity.c | 14 +++++++++++++- src/entity.h | 10 ++-------- src/entityimpl.h | 8 ++++++-- src/entitytag.c | 4 ++++ src/entitytag.h | 4 ++++ src/game.c | 3 ++- src/player.c | 4 ++-- 8 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 src/entitytag.c create mode 100644 src/entitytag.h diff --git a/src/background.c b/src/background.c index ad9e9ce..f0991e4 100644 --- a/src/background.c +++ b/src/background.c @@ -15,7 +15,7 @@ draw_square(double size, double angle) void background_update(Game *g) { - if (game_entity_count(g, ET_player) > 0) + if (game_entity_count(g, entity_type("player")) > 0) tick += 1.0; else tick += 0.25; diff --git a/src/entity.c b/src/entity.c index 53ee9d3..5f3a0dd 100644 --- a/src/entity.c +++ b/src/entity.c @@ -2,9 +2,12 @@ #include "game.h" #include "map.h" #include "cfg.h" +#include "entitytag.h" #include +#include +#include -void +static void _points(Entity *this, int ox, int oy, int *x0, int *x1, int *y0, int *y1) { *x0 = this->pos[0] - this->width / 2 + ox; @@ -13,6 +16,15 @@ _points(Entity *this, int ox, int oy, int *x0, int *x1, int *y0, int *y1) *y1 = *y0 + this->height - 1; } +unsigned int +entity_type(const char *type) +{ + for (unsigned int i = 0; i < num_entitytags; i++) + if (strcmp(type, entitytags[i]) == 0) + return i + 1; + fprintf(stderr, "unknown type '%s'\n", type); + return 0; +} bool entity_collide(Entity *this, Game *g, int ox, int oy) diff --git a/src/entity.h b/src/entity.h index 5c75ab2..532bd12 100644 --- a/src/entity.h +++ b/src/entity.h @@ -2,18 +2,11 @@ #include "player.h" #include "exit.h" #include "deathpart.h" -#include "spike.h" #include struct Game; -enum { - ET_NONE, - ET_player, - ET_exit, - ET_deathpart, - ET_spike, -}; +enum { ET_NONE = 0 }; typedef struct Entity Entity; struct Entity { @@ -36,6 +29,7 @@ struct Entity { }; }; +unsigned int entity_type(const char *type); bool entity_collide(Entity *this, struct Game *g, 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 63aa91f..859b44c 100644 --- a/src/entityimpl.h +++ b/src/entityimpl.h @@ -2,17 +2,21 @@ #include "game.h" #include "cfg.h" #include "lzy.h" +#include "entitytag.h" #include #define IMPL_UPDATE() static Entity*update(Entity*this,Game*g){(void)this,(void)g; #define IMPL_DRAW() static Entity*draw(Entity*this,Game*g){(void)this,(void)g; -#define IMPL_INIT(X) Entity *X##_init(Entity *this, int x, int y) { do { \ +#define IMPL_INIT(X) __attribute__((constructor)) static void init_tag() { \ + entitytags[num_entitytags++] = #X; \ +} \ +Entity *X##_init(Entity *this, int x, int y) { do { \ memset(this, 0, sizeof(*this)); \ this->update = update; \ this->draw = draw; \ this->pos[0] = x; \ this->pos[1] = y; \ - this->type = ET_##X; \ + this->type = entity_type(#X); \ } while(0); #define IMPL_INIT_END #define IMPL_END return this; } diff --git a/src/entitytag.c b/src/entitytag.c new file mode 100644 index 0000000..0c0e952 --- /dev/null +++ b/src/entitytag.c @@ -0,0 +1,4 @@ +#include "entitytag.h" + +unsigned int num_entitytags = 0; +const char *entitytags[256] = {0}; diff --git a/src/entitytag.h b/src/entitytag.h new file mode 100644 index 0000000..2b423d2 --- /dev/null +++ b/src/entitytag.h @@ -0,0 +1,4 @@ +#pragma once + +extern unsigned int num_entitytags; +extern const char *entitytags[256]; diff --git a/src/game.c b/src/game.c index 40319d5..55cb303 100644 --- a/src/game.c +++ b/src/game.c @@ -31,7 +31,8 @@ game_update(Game *this) } if (this->queue_next_scene) { this->queue_next_scene = false; - this->player_dir = game_get_entity(this, ET_exit)->exit.dir; + this->player_dir = game_get_entity(this, + entity_type("exit"))->exit.dir; map_next(); game_restart_scene(this); return; diff --git a/src/player.c b/src/player.c index f1dfc7f..4b1d65f 100644 --- a/src/player.c +++ b/src/player.c @@ -54,7 +54,7 @@ IMPL_UPDATE() { this->player.dirx *= -1; if (this->bonk_ceiling || - entity_place_meeting(this, g, ET_spike) != NULL) { + entity_place_meeting(this, g, entity_type("spike")) != NULL) { int dy = this->pos[1] - 6; for (int y = 0; y < 7; y++) { int dx = this->pos[0] - 6; @@ -68,7 +68,7 @@ IMPL_UPDATE() { g->queue_restart_scene = 45; } - if (entity_place_meeting(this, g, ET_exit) != NULL) + if (entity_place_meeting(this, g, entity_type("exit")) != NULL) g->queue_next_scene = true; } IMPL_END -- cgit v1.2.3