Add tag and prev, next pointers to entity structs

This commit is contained in:
Camden Dixie O'Brien 2025-01-04 06:13:33 +00:00
parent 6c248b0707
commit 8763c437b3

View File

@ -79,7 +79,19 @@ typedef struct {
dvec_t off, ext; dvec_t off, ext;
} dbox_t; } dbox_t;
typedef enum {
ENTITY_DYN,
ENTITY_OBJ,
} entitytag_t;
typedef struct entity {
entitytag_t tag;
struct entity *prev;
struct entity *next;
} entity_t;
typedef struct { typedef struct {
entity_t e;
int svar, animlen; int svar, animlen;
double speed; double speed;
dvec_t pos, dir; dvec_t pos, dir;
@ -95,6 +107,7 @@ typedef struct {
} objtype_t; } objtype_t;
typedef struct { typedef struct {
entity_t e;
unsigned type; unsigned type;
ivec_t pos; ivec_t pos;
} objentity_t; } objentity_t;
@ -142,7 +155,7 @@ map_tile(const map_t map, int layeridx, double x, double y)
return map[layeridx][row][col]; return map[layeridx][row][col];
} }
static inline int propint(xmlNodePtr node, const char *prop) static inline int prop_int(xmlNodePtr node, const char *prop)
{ {
xmlChar *str = xmlGetProp(node, (const xmlChar *)prop); xmlChar *str = xmlGetProp(node, (const xmlChar *)prop);
assert(str); assert(str);
@ -165,8 +178,8 @@ static void load_layer(map_t map, xmlNodePtr layernode, int layeridx)
if (0 != xmlStrcmp(node->name, (const xmlChar *)"chunk")) if (0 != xmlStrcmp(node->name, (const xmlChar *)"chunk"))
continue; continue;
const int chunk_x = propint(node, "x"); const int chunk_x = prop_int(node, "x");
const int chunk_y = propint(node, "y"); const int chunk_y = prop_int(node, "y");
chunk_contents = node->xmlChildrenNode; chunk_contents = node->xmlChildrenNode;
assert( assert(
@ -227,15 +240,15 @@ load_objtype(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
&& xmlStrcmp(node->name, (const xmlChar *)"object") != 0) && xmlStrcmp(node->name, (const xmlChar *)"object") != 0)
node = node->next; node = node->next;
assert(NULL != node); assert(NULL != node);
unsigned id = propint(node, "gid"); unsigned id = prop_int(node, "gid");
assert(id < MAXOBJTYPES); assert(id < MAXOBJTYPES);
// Populate objtype struct if not already loaded // Populate objtype struct if not already loaded
if (NULL == state->objtypes[id].tex) { if (NULL == state->objtypes[id].tex) {
state->objtypes[id].src.x = 0; state->objtypes[id].src.x = 0;
state->objtypes[id].src.y = 0; state->objtypes[id].src.y = 0;
state->objtypes[id].src.w = propint(node, "width"); state->objtypes[id].src.w = prop_int(node, "width");
state->objtypes[id].src.h = propint(node, "height"); state->objtypes[id].src.h = prop_int(node, "height");
node = node->xmlChildrenNode; node = node->xmlChildrenNode;
while (node != NULL while (node != NULL
@ -293,6 +306,7 @@ load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
assert(state->objcol.buf); assert(state->objcol.buf);
} }
objentity_t *o = &state->objcol.buf[state->objcol.n++]; objentity_t *o = &state->objcol.buf[state->objcol.n++];
o->e.tag = ENTITY_OBJ;
// Load object type // Load object type
xmlChar *templ = xmlGetProp(node, (const xmlChar *)"template"); xmlChar *templ = xmlGetProp(node, (const xmlChar *)"template");
@ -301,8 +315,8 @@ load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
xmlFree(templ); xmlFree(templ);
// Load object location // Load object location
o->pos.x = propint(node, "x"); o->pos.x = prop_int(node, "x");
o->pos.y = propint(node, "y"); o->pos.y = prop_int(node, "y");
} while ((node = node->next) != NULL); } while ((node = node->next) != NULL);
} }
@ -363,6 +377,7 @@ void game_init(int argc, char *argv[], void *mem, SDL_Renderer *renderer)
state->vpos.y = -96; state->vpos.y = -96;
// Initialize player state // Initialize player state
state->p.e.tag = ENTITY_DYN;
state->p.svar = SPRITE_DIR_DOWN; state->p.svar = SPRITE_DIR_DOWN;
state->p.animlen = PANIMLEN; state->p.animlen = PANIMLEN;
state->p.animstep.x = PWIDTH; state->p.animstep.x = PWIDTH;