From e51c1cf50bcaeca47c20d5e35df3df713d892a57 Mon Sep 17 00:00:00 2001 From: kdx Date: Tue, 28 Mar 2023 04:29:21 +0000 Subject: store entity name and spawn from type --- map/brulez.tmj | 78 -------------------------------------------------------- map/game.tmj | 49 +++++++++++++++++++++++++++++++++++ src/entity.c | 5 ++-- src/entity.h | 4 ++- src/entityimpl.h | 6 +++-- src/entitytag.h | 2 +- src/game.c | 4 +-- src/map.c | 2 ++ src/player.c | 4 +-- 9 files changed, 66 insertions(+), 88 deletions(-) delete mode 100644 map/brulez.tmj create mode 100644 map/game.tmj diff --git a/map/brulez.tmj b/map/brulez.tmj deleted file mode 100644 index d5997f7..0000000 --- a/map/brulez.tmj +++ /dev/null @@ -1,78 +0,0 @@ -{ "compressionlevel":-1, - "height":14, - "infinite":false, - "layers":[ - { - "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - "height":14, - "id":1, - "name":"Tile Layer 1", - "opacity":1, - "type":"tilelayer", - "visible":true, - "width":25, - "x":0, - "y":0 - }, - { - "draworder":"topdown", - "id":2, - "name":"Object Layer 1", - "objects":[ - { - "height":16, - "id":1, - "name":"player", - "rotation":0, - "type":"", - "visible":true, - "width":16, - "x":144, - "y":96 - }, - { - "height":32, - "id":2, - "name":"player", - "rotation":0, - "type":"", - "visible":true, - "width":32, - "x":256, - "y":144 - }], - "opacity":1, - "type":"objectgroup", - "visible":true, - "x":0, - "y":0 - }], - "nextlayerid":3, - "nextobjectid":3, - "orientation":"orthogonal", - "renderorder":"right-down", - "tiledversion":"1.10.0", - "tileheight":16, - "tilesets":[ - { - "firstgid":1, - "source":"tset.tsj" - }], - "tilewidth":16, - "type":"map", - "version":"1.10", - "width":25 -} \ No newline at end of file diff --git a/map/game.tmj b/map/game.tmj new file mode 100644 index 0000000..5401795 --- /dev/null +++ b/map/game.tmj @@ -0,0 +1,49 @@ +{ "compressionlevel":-1, + "height":14, + "infinite":false, + "layers":[ + { + "draworder":"topdown", + "id":2, + "name":"Object Layer 1", + "objects":[ + { + "height":16, + "id":1, + "name":"player", + "rotation":0, + "type":"player", + "visible":true, + "width":16, + "x":144, + "y":96 + }, + { + "height":32, + "id":2, + "name":"player", + "rotation":0, + "type":"player", + "visible":true, + "width":32, + "x":256, + "y":144 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }], + "nextlayerid":3, + "nextobjectid":3, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.10.0", + "tileheight":16, + "tilesets":[], + "tilewidth":16, + "type":"map", + "version":"1.10", + "width":25 +} \ No newline at end of file diff --git a/src/entity.c b/src/entity.c index edb949e..8a4e45e 100644 --- a/src/entity.c +++ b/src/entity.c @@ -115,8 +115,9 @@ entity_move(Entity *this, [[maybe_unused]] struct Game *g) } Entity * -entity_init(Entity *this, unsigned int type, int x, int y, int w, int h) +entity_init(Entity *this, unsigned int type, const char *name, int x, int y, + int w, int h) { - entitytags[type - 1].init(this, x, y, w, h); + entitytags[type - 1].init(this, name, x, y, w, h); return this; } diff --git a/src/entity.h b/src/entity.h index 79094ea..6b8d2e7 100644 --- a/src/entity.h +++ b/src/entity.h @@ -9,6 +9,7 @@ struct Entity { void (*update)(Entity *this, struct Game *g); void (*draw)(Entity *this, struct Game *g); unsigned int type; + const char *name; int pos[2]; double vel[2]; double rem[2]; @@ -29,4 +30,5 @@ bool entity_collide(Entity *this, 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, int w, int h); +Entity *entity_init(Entity *this, unsigned int type, const char *name, + int x, int y, int w, int h); diff --git a/src/entityimpl.h b/src/entityimpl.h index 4a71e39..397e58b 100644 --- a/src/entityimpl.h +++ b/src/entityimpl.h @@ -12,17 +12,19 @@ __attribute__((constructor)) static void init_##X() { _##X = X; } \ static void X([[maybe_unused]] Entity *this, [[maybe_unused]] Game *g) -#define IMPL_INIT(X,P) static void init(Entity *, int, int, int, int); \ +#define IMPL_INIT(X,P) static void init(Entity *, const char *, \ + int, int, int, int); \ __attribute__((constructor)) static void init_tag(void) { \ entitytags[num_entitytags].init = init; \ entitytags[num_entitytags].parent = P; \ entitytags[num_entitytags++].name = #X; \ } \ static void _init(Entity *this); \ -static void init(Entity *this, int x, int y, int w, int h) { \ +static void init(Entity *this, const char *name, int x, int y, int w, int h) { \ memset(this, 0, sizeof(*this)); \ this->update = _update; \ this->draw = _draw; \ + this->name = name; \ this->pos[0] = x; \ this->pos[1] = y; \ this->width = w; \ diff --git a/src/entitytag.h b/src/entitytag.h index 211e365..814ed4d 100644 --- a/src/entitytag.h +++ b/src/entitytag.h @@ -4,7 +4,7 @@ typedef struct { const char *name; const char *parent; - void (*init)(Entity *this, int x, int y, int w, int h); + void (*init)(Entity *, const char *name, int x, int y, int w, int h); } EntityTag; extern unsigned int num_entitytags; diff --git a/src/game.c b/src/game.c index 6967a3a..5166d99 100644 --- a/src/game.c +++ b/src/game.c @@ -63,12 +63,12 @@ game_restart_scene(Game *this) const __auto_type objects = map_objects(&size); for (__auto_type i = 0u; i < size; i++) { const __auto_type object = &objects[i]; - const __auto_type type = entity_type(object->name); + const __auto_type type = entity_type(object->type); if (type == 0) continue; const __auto_type x = object->x + object->width / 2; const __auto_type y = object->y + object->height / 2; - entity_init(game_create_entity(this), type, x, y, + entity_init(game_create_entity(this), type, object->name, x, y, object->width, object->height); } } diff --git a/src/map.c b/src/map.c index babb118..61b272e 100644 --- a/src/map.c +++ b/src/map.c @@ -36,6 +36,8 @@ map_get(int x, int y) { if (x < 0 || y < 0 || x >= map_width() || y >= map_height()) return 1; + if (tmj2c_maps[map_id]->numlayers < 1) + return 1; return tmj2c_maps[map_id]->layers[0].data[x + y * map_width()]; } diff --git a/src/player.c b/src/player.c index e157830..460e80a 100644 --- a/src/player.c +++ b/src/player.c @@ -11,8 +11,8 @@ IMPL(update) { this->vel[0] = 2 * TZR_IsKeyDown(SDL_SCANCODE_RIGHT) - 2 * TZR_IsKeyDown(SDL_SCANCODE_LEFT); entity_move(this, g); - entity_init(game_create_entity(g), entity_type("part"), this->pos[0], - this->pos[1], 0, 0); + entity_init(game_create_entity(g), entity_type("part"), NULL, + this->pos[0], this->pos[1], 0, 0); } IMPL_INIT(player, 0) { -- cgit v1.2.3