Update parsers to use more real C data.

This commit is contained in:
2025-06-23 22:23:18 -05:00
parent 22af9d1507
commit 24eab84f4f
17 changed files with 85 additions and 57 deletions

View File

@ -17,29 +17,37 @@ entity_t ENTITIES[ENTITY_COUNT_MAX] = {0};
entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
{NULL}, // ENTITY_TYPE_NULL
{
.init = playerNPCInit,
.update = playerNPCUpdate,
.load = playerEntityLoad,
.update = playerEntityUpdate,
},
{
.init = npcInit,
.load = npcLoad,
.update = npcUpdate,
.interact = npcInteract,
},
};
void entityInit(entity_t *entity, const entitytype_t type) {
void entityLoad(entity_t *entity, const entity_t *source) {
assertNotNull(entity, "Entity pointer cannot be NULL");
assertTrue(type != ENTITY_TYPE_NULL, "Entity type NULL");
assertTrue(type < ENTITY_TYPE_COUNT, "Entity type out of bounds");
assertNotNull(source, "Source entity pointer cannot be NULL");
assertTrue(source->type != ENTITY_TYPE_NULL, "Source entity type NULL");
assertTrue(source->type < ENTITY_TYPE_COUNT, "Source entity type bad");
assertNotNull(
ENTITY_CALLBACKS[type].init,
ENTITY_CALLBACKS[source->type].load,
"Entity type has no i nit callback"
);
memoryZero(entity, sizeof(entity_t));
entity->type = type;
ENTITY_CALLBACKS[type].init(entity);
entity->type = source->type;
entity->x = source->x;
entity->y = source->y;
entity->vx = source->vx;
entity->vy = source->vy;
entity->dir = source->dir;
entity->id = source->id;
ENTITY_CALLBACKS[entity->type].load(entity, source);
}
void entityUpdate(entity_t *entity) {

View File

@ -45,7 +45,7 @@ typedef struct _entity_t {
} entity_t;
typedef struct {
void (*init) (entity_t *entity);
void (*load) (entity_t *entity, const entity_t *source);
void (*update) (entity_t *entity);
void (*interact)(entity_t *player, entity_t *self);
} entitycallback_t;
@ -54,12 +54,12 @@ extern entity_t ENTITIES[ENTITY_COUNT_MAX];
extern entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT];
/**
* Initializes an entity with the given type.
* Loads an entity from the generated entity data.
*
* @param entity Pointer to the entity to initialize.
* @param type The type of the entity to initialize.
* @param source Pointer to the source entity data.
*/
void entityInit(entity_t *entity, const entitytype_t type);
void entityLoad(entity_t *entity, const entity_t *source);
/**
* Updates the entity's state.

View File

@ -10,8 +10,10 @@
#include "locale/language.h"
#include "assert/assert.h"
void npcInit(entity_t *entity) {
void npcLoad(entity_t *entity, const entity_t *source) {
assertNotNull(entity, "Entity pointer cannot be NULL");
assertNotNull(source, "Source entity pointer cannot be NULL");
assertTrue(source->type == ENTITY_TYPE_NPC, "Source entity type must be NPC");
}
void npcUpdate(entity_t *entity) {

View File

@ -25,8 +25,9 @@ typedef struct {
* Initializes the NPC entity.
*
* @param entity The entity to initialize.
* @param source The source entity to copy data from.
*/
void npcInit(entity_t *entity);
void npcLoad(entity_t *entity, const entity_t *source);
/**
* Updates the NPC entity.

View File

@ -19,21 +19,25 @@ inventory_t PLAYER_INVENTORY;
void playerInit() {
entity_t *ent = &ENTITIES[0];
entityInit(ent, ENTITY_TYPE_PLAYER);
ent->id = PLAYER_ENTITY_ID;
ent->x = WORLD_PLAYER_SPAWN_X;
ent->y = WORLD_PLAYER_SPAWN_Y;
entity_t playerEntityData = {
.id = PLAYER_ENTITY_ID,
.type = ENTITY_TYPE_PLAYER,
.x = WORLD_PLAYER_SPAWN_X,
.y = WORLD_PLAYER_SPAWN_Y,
};
entityLoad(ent, &playerEntityData);
inventoryInit(&PLAYER_INVENTORY, INVENTORY_SIZE_MAX);
}
void playerNPCInit(entity_t *entity) {
void playerEntityLoad(entity_t *entity, const entity_t *source) {
assertNotNull(entity, "Entity pointer cannot be NULL");
assertNotNull(source, "Source entity pointer cannot be NULL");
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity type must be PLAYER");
assertTrue(source->type == entity->type, "Source/Entity type mismatch");
}
void playerNPCUpdate(entity_t *entity) {
void playerEntityUpdate(entity_t *entity) {
assertNotNull(entity, "Entity pointer cannot be NULL");
assertTrue(entity->type == ENTITY_TYPE_PLAYER, "Entity type must be PLAYER");

View File

@ -32,15 +32,16 @@ extern inventory_t PLAYER_INVENTORY;
void playerInit(void);
/**
* Initializes the player entity.
* Loads the player entity.
*
* @param entity The entity to initialize.
* @param source The source entity to copy data from.
*/
void playerNPCInit(entity_t *entity);
void playerEntityLoad(entity_t *entity, const entity_t *source);
/**
* Updates the player entity.
*
* @param entity The entity to update.
*/
void playerNPCUpdate(entity_t *entity);
void playerEntityUpdate(entity_t *entity);

View File

@ -222,7 +222,7 @@ void chunkLoad(chunk_t *chunk, const uint16_t x, const uint16_t y) {
);
// Load chunk entities
const chunkentity_t *data;
const entity_t *data;
entity_t *entity;
data = chunkData->entities;
while(data < chunkData->entities + CHUNK_ENTITY_COUNT_MAX) {
@ -257,15 +257,8 @@ void chunkLoad(chunk_t *chunk, const uint16_t x, const uint16_t y) {
entity++;
};
// Initialize this entity.
entityInit(entity, data->type);
entity->id = data->id;
// Positions are chunk-relative.
entity->x = fx248Fromu32((chunk->x * CHUNK_WIDTH * TILE_WIDTH_HEIGHT) + data->x);
entity->y = fx248Fromu32((chunk->y * CHUNK_HEIGHT * TILE_WIDTH_HEIGHT)+data->y);
// Next entity
// Load this entity.
entityLoad(entity, data);
data++;
}
}

View File

@ -9,15 +9,8 @@
#include "chunk.h"
#include "entity/entity.h"
typedef struct {
uint32_t id;
entitytype_t type;
uint8_t x, y;
entitydir_t dir;
} chunkentity_t;
typedef struct {
uint8_t layerBase[CHUNK_TILE_COUNT];
uint8_t layerBaseOverlay[CHUNK_TILE_COUNT];
chunkentity_t entities[CHUNK_ENTITY_COUNT_MAX];
entity_t entities[CHUNK_ENTITY_COUNT_MAX];
} chunkdata_t;