i'm not sorry

This commit is contained in:
kdx 2023-03-23 16:16:22 +01:00
parent 030d8a78d0
commit 9e00511b26
8 changed files with 34 additions and 15 deletions

View File

@ -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;

View File

@ -2,9 +2,12 @@
#include "game.h"
#include "map.h"
#include "cfg.h"
#include "entitytag.h"
#include <stddef.h>
#include <string.h>
#include <stdio.h>
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)

View File

@ -2,18 +2,11 @@
#include "player.h"
#include "exit.h"
#include "deathpart.h"
#include "spike.h"
#include <stdbool.h>
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);

View File

@ -2,17 +2,21 @@
#include "game.h"
#include "cfg.h"
#include "lzy.h"
#include "entitytag.h"
#include <string.h>
#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; }

4
src/entitytag.c Normal file
View File

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

4
src/entitytag.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
extern unsigned int num_entitytags;
extern const char *entitytags[256];

View File

@ -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;

View File

@ -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