entity init by name

This commit is contained in:
kdx 2023-03-26 05:45:46 +00:00
parent aed7f6fa32
commit 20d86d093f
12 changed files with 32 additions and 35 deletions

View File

@ -3,8 +3,3 @@
typedef struct {
int skip;
} DeathPart;
struct Entity;
struct Game;
struct Entity *deathpart_init(struct Entity *this, int x, int y);

View File

@ -96,3 +96,9 @@ entity_move(Entity *this, Game *g)
}
}
}
Entity *
entity_init(Entity *this, unsigned int type, int x, int y)
{
return entityinits[type - 1](this, x, y);
}

View File

@ -34,3 +34,4 @@ 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);
void entity_move(Entity *this, struct Game *g);
Entity *entity_init(Entity *this, unsigned int type, int x, int y);

View File

@ -6,13 +6,18 @@
#include <string.h>
#define IMPL_UPDATE() static Entity * \
update([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) {
update([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) {
#define IMPL_DRAW() static Entity * \
draw([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) {
#define IMPL_INIT(X) __attribute__((constructor)) static void init_tag() { \
draw([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) {
#define IMPL_INIT(X) static Entity *init(Entity *this, int x, int y); \
__attribute__((constructor)) static void init_tag() { \
entityinits[num_entitytags] = init; \
entitytags[num_entitytags++] = #X; \
} \
Entity *X##_init(Entity *this, int x, int y) { do { \
static Entity * \
init(Entity *this, int x, int y) { do { \
memset(this, 0, sizeof(*this)); \
this->update = update; \
this->draw = draw; \
@ -20,4 +25,5 @@ Entity *X##_init(Entity *this, int x, int y) { do { \
this->pos[1] = y; \
this->type = entity_type(#X); \
} while(0);
#define IMPL_END return this; }

View File

@ -1,4 +1,5 @@
#include "entitytag.h"
unsigned int num_entitytags = 0;
const char *entitytags[256] = {0};
const char *entitytags[256] = {};
EntityInit entityinits[256] = {};

View File

@ -1,4 +1,7 @@
#pragma once
#include "entity.h"
typedef Entity *(*EntityInit)(Entity *this, int x, int y);
extern unsigned int num_entitytags;
extern const char *entitytags[256];
extern EntityInit entityinits[256];

View File

@ -3,8 +3,3 @@
typedef struct {
int dir;
} Exit;
struct Entity;
struct Game;
struct Entity *exit_init(struct Entity *this, int x, int y);

View File

@ -1,10 +1,8 @@
#include "game.h"
#include "exit.h"
#include "entity.h"
#include "map.h"
#include "player.h"
#include "cfg.h"
#include "rotrect.h"
#include "spike.h"
#include <string.h>
void
@ -71,16 +69,19 @@ game_restart_scene(Game *this)
Entity *e;
switch (map_get(x, y)) {
case 2:
e = player_init(game_create_entity(this), dx, dy);
e = entity_init(game_create_entity(this),
entity_type("player"), dx, dy);
e->player.dirx = this->player_dir;
break;
case 3:
case 4:
e = exit_init(game_create_entity(this), dx, dy);
e = entity_init(game_create_entity(this),
entity_type("exit"), dx, dy);
e->exit.dir = (map_get(x, y) == 3) ? -1 : 1;
break;
case 5:
spike_init(game_create_entity(this), dx, dy);
e = entity_init(game_create_entity(this),
entity_type("spike"), dx, dy);
break;
default:
break;

View File

@ -3,7 +3,7 @@
static const unsigned int keys[6] = {LZYK_LEFT, LZYK_RIGHT, LZYK_UP,
LZYK_DOWN, LZYK_O, LZYK_X};
static int states[6] = {0};
static int states[6] = {};
void input_update(void)
{

View File

@ -1,4 +1,3 @@
#include "deathpart.h"
#include "entity.h"
#include "entityimpl.h"
#include "game.h"
@ -59,7 +58,8 @@ IMPL_UPDATE() {
for (int y = 0; y < 7; y++) {
int dx = this->pos[0] - 6;
for (int x = 0; x < 7; x++) {
deathpart_init(game_create_entity(g), dx, dy);
entity_init(game_create_entity(g),
entity_type("deathpart"), dx, dy);
dx += 2;
}
dy += 2;

View File

@ -10,8 +10,3 @@ typedef struct {
double angle;
int dirx;
} Player;
struct Entity;
struct Game;
struct Entity *player_init(struct Entity *this, int x, int y);

View File

@ -1,6 +0,0 @@
#pragma once
struct Entity;
struct Game;
struct Entity *spike_init(struct Entity *this, int x, int y);