diff --git a/src/rpg/entity/entity.c b/src/rpg/entity/entity.c index 44d6339..2eca099 100644 --- a/src/rpg/entity/entity.c +++ b/src/rpg/entity/entity.c @@ -56,7 +56,7 @@ void entityUpdate(entity_t *entity) { // Movement code. if(entity->type == ENTITY_TYPE_PLAYER) { - playerMovement(entity); + playerInput(entity); } } @@ -108,4 +108,20 @@ void entityWalk(entity_t *entity, const entitydir_t direction) { entity->animation = ENTITY_ANIM_WALK; entity->animFrame = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking +} + +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; + } + } while(++ent, ent < &ENTITIES[ENTITY_COUNT]); + + return NULL; } \ No newline at end of file diff --git a/src/rpg/entity/entity.h b/src/rpg/entity/entity.h index 9823502..4ef4b14 100644 --- a/src/rpg/entity/entity.h +++ b/src/rpg/entity/entity.h @@ -72,3 +72,12 @@ void entityTurn(entity_t *entity, const entitydir_t direction); * @param direction The direction to walk in. */ void entityWalk(entity_t *entity, const entitydir_t direction); + +/** + * Gets the entity at a specific world position. + * + * @param map Pointer to the map to check. + * @param pos The world position to check. + * @return Pointer to the entity at the position, or NULL if none. + */ +entity_t *entityGetAt(const worldpos_t pos); \ No newline at end of file diff --git a/src/rpg/entity/entitydir.c b/src/rpg/entity/entitydir.c index 0523aeb..0af488d 100644 --- a/src/rpg/entity/entitydir.c +++ b/src/rpg/entity/entitydir.c @@ -8,7 +8,11 @@ #include "entitydir.h" #include "assert/assert.h" -void entityDirGetRelative(const entitydir_t from, int8_t *outX, int8_t *outY) { +void entityDirGetRelative( + const entitydir_t from, + worldunits_t *outX, + worldunits_t *outY +) { assertValidEntityDir(from, "Invalid direction provided"); assertNotNull(outX, "Output X pointer cannot be NULL"); assertNotNull(outY, "Output Y pointer cannot be NULL"); diff --git a/src/rpg/entity/entitydir.h b/src/rpg/entity/entitydir.h index c47c2c5..20fa150 100644 --- a/src/rpg/entity/entitydir.h +++ b/src/rpg/entity/entitydir.h @@ -6,7 +6,7 @@ */ #pragma once -#include "dusk.h" +#include "rpg/world/worldpos.h" typedef enum { ENTITY_DIR_SOUTH = 0, @@ -42,4 +42,6 @@ typedef enum { * @param relX Pointer to store the relative x offset. * @param relY Pointer to store the relative y offset. */ -void entityDirGetRelative(const entitydir_t dir, int8_t *relX, int8_t *relY); \ No newline at end of file +void entityDirGetRelative( + const entitydir_t dir, worldunits_t *relX, worldunits_t *relY +); \ No newline at end of file diff --git a/src/rpg/entity/player.c b/src/rpg/entity/player.c index 0b76552..c1d84ba 100644 --- a/src/rpg/entity/player.c +++ b/src/rpg/entity/player.c @@ -30,7 +30,7 @@ void playerInit(entity_t *entity) { assertNotNull(entity, "Entity pointer cannot be NULL"); } -void playerMovement(entity_t *entity) { +void playerInput(entity_t *entity) { assertNotNull(entity, "Entity pointer cannot be NULL"); // Turn @@ -48,11 +48,23 @@ void playerMovement(entity_t *entity) { if(entity->direction != dirMap->direction) continue; return entityWalk(entity, dirMap->direction); } while((++dirMap)->action != 0xFF); -} -void playerInteraction(entity_t *entity) { - assertNotNull(entity, "Entity pointer cannot be NULL"); + // Interaction + if(inputPressed(INPUT_ACTION_ACCEPT)) { + worldunit_t x, y, z; + { + worldunits_t relX, relY; + entityDirGetRelative(entity->direction, &relX, &relY); + x = entity->position.x + relX; + y = entity->position.y + relY; + z = entity->position.z; + } - if(!inputPressed(INPUT_ACTION_ACCEPT)) return; + entity_t *interact = entityGetAt((worldpos_t){ x, y, z }); + if(interact != NULL) { + printf("INTERACT\n"); + return; + } + } } \ No newline at end of file diff --git a/src/rpg/entity/player.h b/src/rpg/entity/player.h index ea08ef3..97dfd31 100644 --- a/src/rpg/entity/player.h +++ b/src/rpg/entity/player.h @@ -26,11 +26,4 @@ void playerInit(entity_t *entity); * * @param entity Pointer to the player entity structure. */ -void playerMovement(entity_t *entity); - -/** - * Handles interaction logic for the player entity. - * - * @param entity Pointer to the player entity structure. - */ -void playerInteraction(entity_t *entity); \ No newline at end of file +void playerInput(entity_t *entity); \ No newline at end of file diff --git a/src/rpg/world/worldpos.h b/src/rpg/world/worldpos.h index 9ff19e9..81f91bb 100644 --- a/src/rpg/world/worldpos.h +++ b/src/rpg/world/worldpos.h @@ -8,6 +8,9 @@ #pragma once #include "dusk.h" +typedef uint8_t worldunit_t; +typedef int8_t worldunits_t; + typedef struct worldpos_s { - uint32_t x, y, z; + worldunit_t x, y, z; } worldpos_t; \ No newline at end of file