From 7d46b983100e3b81467229796145ead3fc44d2e2 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 4 Nov 2025 09:03:36 -0600 Subject: [PATCH] Ent movement --- src/rpg/entity/entity.c | 17 +++++++---------- src/rpg/entity/entitytype.h | 10 +++++++++- src/rpg/entity/npc.c | 2 +- src/rpg/entity/npc.h | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/rpg/entity/entity.c b/src/rpg/entity/entity.c index a904e75..091c594 100644 --- a/src/rpg/entity/entity.c +++ b/src/rpg/entity/entity.c @@ -43,8 +43,8 @@ void entityUpdate(entity_t *entity) { } // Movement code. - if(entity->type == ENTITY_TYPE_PLAYER) { - playerInput(entity); + if(ENTITY_CALLBACKS[entity->type].movement != NULL) { + ENTITY_CALLBACKS[entity->type].movement(entity); } } @@ -100,14 +100,11 @@ void entityWalk(entity_t *entity, const entitydir_t direction) { entity_t * entityGetAt(const worldpos_t position) { entity_t *ent = ENTITIES; do { - if( - ent->type != ENTITY_TYPE_NULL && - ent->position.x == position.x && - ent->position.y == position.y && - ent->position.z == position.z - ) { - return ent; - } + if(ent->type == ENTITY_TYPE_NULL) continue; + if(ent->position.x != position.x) continue; + if(ent->position.y != position.y) continue; + if(ent->position.z != position.z) continue; + return ent; } while(++ent, ent < &ENTITIES[ENTITY_COUNT]); return NULL; diff --git a/src/rpg/entity/entitytype.h b/src/rpg/entity/entitytype.h index 7369b75..a556ba0 100644 --- a/src/rpg/entity/entitytype.h +++ b/src/rpg/entity/entitytype.h @@ -30,6 +30,12 @@ typedef struct { */ void (*init)(entity_t *entity); + /** + * Movement callback for the entity type. + * @param entity Pointer to the entity to move. + */ + void (*movement)(entity_t *entity); + /** * Interaction callback for the entity type. * @param player Pointer to the player entity. @@ -43,11 +49,13 @@ static const entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = { [ENTITY_TYPE_NULL] = { NULL }, [ENTITY_TYPE_PLAYER] = { - .init = playerInit + .init = playerInit, + .movement = playerInput }, [ENTITY_TYPE_NPC] = { .init = npcInit, + .movement = npcMovement, .interact = npcInteract } }; \ No newline at end of file diff --git a/src/rpg/entity/npc.c b/src/rpg/entity/npc.c index 1f97e0f..662a47b 100644 --- a/src/rpg/entity/npc.c +++ b/src/rpg/entity/npc.c @@ -12,7 +12,7 @@ void npcInit(entity_t *entity) { assertNotNull(entity, "Entity pointer cannot be NULL"); } -void npcUpdate(entity_t *entity) { +void npcMovement(entity_t *entity) { assertNotNull(entity, "Entity pointer cannot be NULL"); } diff --git a/src/rpg/entity/npc.h b/src/rpg/entity/npc.h index a444da2..df36f15 100644 --- a/src/rpg/entity/npc.h +++ b/src/rpg/entity/npc.h @@ -26,7 +26,7 @@ void npcInit(entity_t *entity); * * @param entity Pointer to the entity structure to update. */ -void npcUpdate(entity_t *entity); +void npcMovement(entity_t *entity); /** * Handles interaction with an NPC entity.