Compare commits

...

2 Commits

Author SHA1 Message Date
kdx 09438266c3 overengineer entityimpl a bit more 2023-03-26 07:26:47 +00:00
kdx 83f5fc33df simplify entityimpl a bit 2023-03-26 07:11:55 +00:00
9 changed files with 78 additions and 80 deletions

View File

@ -3,25 +3,6 @@
#include "entityimpl.h"
#include <stdlib.h>
IMPL_UPDATE() {
if (this->deathpart.skip == 0) {
this->vel[1] *= AIR_RESISTANCE;
this->vel[1] += GRAVITY;
entity_move(this, g);
this->deathpart.skip = 3;
} else
this->deathpart.skip -= 1;
} IMPL_END
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;
this->vel[0] = (float)(rand() % 1024) / 1024;
@ -30,4 +11,23 @@ IMPL_INIT(deathpart) {
if (rand() % 2)
this->vel[0] *= -1;
this->width = 1 + rand() % 2;
} IMPL_END
}
IMPL(update) {
if (this->deathpart.skip == 0) {
this->vel[1] *= AIR_RESISTANCE;
this->vel[1] += GRAVITY;
entity_move(this, g);
this->deathpart.skip = 3;
} else
this->deathpart.skip -= 1;
}
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);
}

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,6 @@ 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);
entitytags[type - 1].init(this, x, y);
return this;
}

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,25 +5,26 @@
#include "entitytag.h"
#include <string.h>
#define IMPL_UPDATE() static Entity * \
update([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) {
[[maybe_unused]] static void *_draw;
[[maybe_unused]] static void *_update;
#define IMPL_DRAW() static Entity * \
draw([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) {
#define IMPL(X) static void X(Entity *this, Game *g); \
__attribute__((constructor)) static void init_##X() { _##X = 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); \
#define IMPL_INIT(X) static void 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 { \
static void _init(Entity *this); \
static void init(Entity *this, int x, int y) { \
memset(this, 0, sizeof(*this)); \
this->update = update; \
this->draw = draw; \
this->update = _update; \
this->draw = _draw; \
this->pos[0] = x; \
this->pos[1] = y; \
this->type = entity_type(#X); \
} while(0);
#define IMPL_END return this; }
_init(this); \
} \
static void _init([[maybe_unused]] Entity *this)

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;
void (*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,17 +1,14 @@
#include "entityimpl.h"
#include "rotrect.h"
IMPL_UPDATE() {
} IMPL_END
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;
this->height = 12;
this->exit.dir = 1;
} IMPL_END
}
IMPL(draw) {
LZY_DrawSetColor(BLACK);
rotrect(this->pos[0], this->pos[1],
this->width, this->height, 0.2 * this->exit.dir);
}

View File

@ -5,7 +5,23 @@
#include "rotrect.h"
#include <math.h>
IMPL_UPDATE() {
IMPL_INIT(player) {
this->width = 10;
this->height = 10;
this->player.scale_x = 1.0;
this->player.scale_y = 1.0;
this->player.dirx = 1;
}
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(update) {
const int on_ground = entity_collide(this, g, 0, 1);
this->vel[0] = 2.0 * this->player.dirx;
@ -71,20 +87,4 @@ IMPL_UPDATE() {
if (entity_place_meeting(this, g, entity_type("exit")) != NULL)
g->queue_next_scene = true;
} IMPL_END
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;
this->height = 10;
this->player.scale_x = 1.0;
this->player.scale_y = 1.0;
this->player.dirx = 1;
} IMPL_END
}

View File

@ -1,14 +1,11 @@
#include "entityimpl.h"
#include "rotrect.h"
IMPL_UPDATE() {
} IMPL_END
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;
} IMPL_END
}
IMPL(draw) {
rotrect_draw(g->spike_rect, this->pos[0], this->pos[1]);
rotrect_draw(g->spike_irect, this->pos[0], this->pos[1]);
}