simplify entityimpl a bit

This commit is contained in:
kdx 2023-03-26 07:11:55 +00:00
parent 60b9ce29c4
commit 83f5fc33df
9 changed files with 30 additions and 32 deletions

View File

@ -3,7 +3,7 @@
#include "entityimpl.h"
#include <stdlib.h>
IMPL_UPDATE() {
IMPL(update) {
if (this->deathpart.skip == 0) {
this->vel[1] *= AIR_RESISTANCE;
this->vel[1] += GRAVITY;
@ -11,16 +11,16 @@ IMPL_UPDATE() {
this->deathpart.skip = 3;
} else
this->deathpart.skip -= 1;
} IMPL_END
}
IMPL_DRAW() {
IMPL(draw) {
LZY_DrawSetColor(BLACK);
if (this->width == 1)
(void)LZY_DrawPoint(this->pos[0], this->pos[1]);
else
(void)LZY_DrawRect(this->pos[0], this->pos[1],
this->width, this->width);
} IMPL_END
}
IMPL_INIT(deathpart) {
this->ignore_solids = true;

View File

@ -20,7 +20,7 @@ unsigned int
entity_type(const char *type)
{
for (unsigned int i = 0; i < num_entitytags; i++)
if (strcmp(type, entitytags[i]) == 0)
if (strcmp(type, entitytags[i].name) == 0)
return i + 1;
fprintf(stderr, "unknown type '%s'\n", type);
return 0;
@ -100,5 +100,5 @@ 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);
return entitytags[type - 1].init(this, x, y);
}

View File

@ -11,8 +11,8 @@ enum { ET_NONE = 0 };
typedef struct Entity Entity;
struct Entity {
unsigned long uuid;
Entity *(*update)(Entity *this, struct Game *g);
Entity *(*draw)(Entity *this, struct Game *g);
void (*update)(Entity *this, struct Game *g);
void (*draw)(Entity *this, struct Game *g);
unsigned int type;
int id;
int pos[2];

View File

@ -5,16 +5,13 @@
#include "entitytag.h"
#include <string.h>
#define IMPL_UPDATE() static Entity * \
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(X) static void \
X([[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; \
entitytags[num_entitytags].init = init; \
entitytags[num_entitytags++].name = #X; \
} \
static Entity * \
init(Entity *this, int x, int y) { do { \

View File

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

View File

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

View File

@ -1,14 +1,14 @@
#include "entityimpl.h"
#include "rotrect.h"
IMPL_UPDATE() {
} IMPL_END
IMPL(update) {
}
IMPL_DRAW() {
IMPL(draw) {
LZY_DrawSetColor(BLACK);
rotrect(this->pos[0], this->pos[1],
this->width, this->height, 0.2 * this->exit.dir);
} IMPL_END
}
IMPL_INIT(exit) {
this->width = 12;

View File

@ -5,7 +5,7 @@
#include "rotrect.h"
#include <math.h>
IMPL_UPDATE() {
IMPL(update) {
const int on_ground = entity_collide(this, g, 0, 1);
this->vel[0] = 2.0 * this->player.dirx;
@ -71,15 +71,15 @@ IMPL_UPDATE() {
if (entity_place_meeting(this, g, entity_type("exit")) != NULL)
g->queue_next_scene = true;
} IMPL_END
}
IMPL_DRAW() {
IMPL(draw) {
LZY_DrawSetColor(BLACK);
const double width = this->width * this->player.scale_x + 2;
const double height = this->height * this->player.scale_y + 2;
rotrect(this->pos[0] - 1, this->pos[1] - 1,
width, height, this->player.angle);
} IMPL_END
}
IMPL_INIT(player) {
this->width = 10;

View File

@ -1,13 +1,12 @@
#include "entityimpl.h"
#include "rotrect.h"
IMPL_UPDATE() {
} IMPL_END
IMPL(update) {}
IMPL_DRAW() {
IMPL(draw) {
rotrect_draw(g->spike_rect, this->pos[0], this->pos[1]);
rotrect_draw(g->spike_irect, this->pos[0], this->pos[1]);
} IMPL_END
}
IMPL_INIT(spike) {
this->height = this->width = 8;