overengineer entityimpl a bit more

This commit is contained in:
kdx 2023-03-26 07:26:47 +00:00
parent 83f5fc33df
commit 09438266c3
7 changed files with 50 additions and 50 deletions

View File

@ -3,6 +3,16 @@
#include "entityimpl.h"
#include <stdlib.h>
IMPL_INIT(deathpart) {
this->ignore_solids = true;
this->vel[0] = (float)(rand() % 1024) / 1024;
this->vel[1] = -(float)(rand() % 1024) / 512;
this->vel[0] *= this->vel[0];
if (rand() % 2)
this->vel[0] *= -1;
this->width = 1 + rand() % 2;
}
IMPL(update) {
if (this->deathpart.skip == 0) {
this->vel[1] *= AIR_RESISTANCE;
@ -21,13 +31,3 @@ IMPL(draw) {
(void)LZY_DrawRect(this->pos[0], this->pos[1],
this->width, this->width);
}
IMPL_INIT(deathpart) {
this->ignore_solids = true;
this->vel[0] = (float)(rand() % 1024) / 1024;
this->vel[1] = -(float)(rand() % 1024) / 512;
this->vel[0] *= this->vel[0];
if (rand() % 2)
this->vel[0] *= -1;
this->width = 1 + rand() % 2;
} IMPL_END

View File

@ -100,5 +100,6 @@ entity_move(Entity *this, Game *g)
Entity *
entity_init(Entity *this, unsigned int type, int x, int y)
{
return entitytags[type - 1].init(this, x, y);
entitytags[type - 1].init(this, x, y);
return this;
}

View File

@ -5,22 +5,26 @@
#include "entitytag.h"
#include <string.h>
#define IMPL(X) static void \
X([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g)
[[maybe_unused]] static void *_draw;
[[maybe_unused]] static void *_update;
#define IMPL_INIT(X) static Entity *init(Entity *this, int x, int y); \
#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 void init(Entity *this, int x, int y); \
__attribute__((constructor)) static void init_tag() { \
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

@ -3,7 +3,7 @@
typedef struct {
const char *name;
Entity *(*init)(Entity *this, int x, int y);
void (*init)(Entity *this, int x, int y);
} EntityTag;
extern unsigned int num_entitytags;

View File

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

View File

@ -5,6 +5,22 @@
#include "rotrect.h"
#include <math.h>
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);
@ -72,19 +88,3 @@ IMPL(update) {
if (entity_place_meeting(this, g, entity_type("exit")) != NULL)
g->queue_next_scene = true;
}
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_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,13 +1,11 @@
#include "entityimpl.h"
#include "rotrect.h"
IMPL(update) {}
IMPL_INIT(spike) {
this->height = this->width = 8;
}
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_INIT(spike) {
this->height = this->width = 8;
} IMPL_END