From a45a2a5bd738e44ae862b548e1a01e12fcd36dca Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 17 Sep 2025 18:54:12 -0500 Subject: [PATCH] Basic interaction --- src/rpg/entity/direction.c | 25 +++++++----------- src/rpg/entity/direction.h | 12 +++------ src/rpg/entity/entity.c | 4 +++ src/rpg/entity/player.c | 53 ++++++++++++++++++++++++++++++++++++++ src/rpg/entity/player.h | 11 +++++++- 5 files changed, 81 insertions(+), 24 deletions(-) diff --git a/src/rpg/entity/direction.c b/src/rpg/entity/direction.c index e4f0e5e..aa04586 100644 --- a/src/rpg/entity/direction.c +++ b/src/rpg/entity/direction.c @@ -18,36 +18,31 @@ float_t directionToAngle(const direction_t dir) { } } -void directionGetCoordinates( - const direction_t dir, - int8_t *x, int8_t *y -) { - assertNotNull(x, "X coordinate pointer cannot be NULL"); - assertNotNull(y, "Y coordinate pointer cannot be NULL"); +void directionGetVec2(const direction_t dir, vec2 out) { + assertNotNull(out, "Output vector cannot be NULL"); switch(dir) { case DIRECTION_NORTH: - *x = 0; - *y = -1; + out[0] = 0.0f; + out[1] = 1.0f; break; case DIRECTION_SOUTH: - *x = 0; - *y = 1; + out[0] = 0.0f; + out[1] = -1.0f; break; case DIRECTION_EAST: - *x = 1; - *y = 0; + out[0] = 1.0f; + out[1] = 0.0f; break; case DIRECTION_WEST: - *x = -1; - *y = 0; + out[0] = -1.0f; + out[1] = 0.0f; break; default: assertUnreachable("Invalid direction"); - break; } } \ No newline at end of file diff --git a/src/rpg/entity/direction.h b/src/rpg/entity/direction.h index 65d6f2e..54b9c11 100644 --- a/src/rpg/entity/direction.h +++ b/src/rpg/entity/direction.h @@ -29,13 +29,9 @@ typedef enum { float_t directionToAngle(const direction_t dir); /** - * Gets the relative coordinates for a given direction. + * Converts a direction to a vec2 unit vector. * - * @param dir The direction to get coordinates for. - * @param x Pointer to store the x coordinate. - * @param y Pointer to store the y coordinate. + * @param dir The direction to convert. + * @param out Pointer to the vec2 array to populate. */ -void directionGetCoordinates( - const direction_t dir, - int8_t *x, int8_t *y -); \ No newline at end of file +void directionGetVec2(const direction_t dir, vec2 out); \ No newline at end of file diff --git a/src/rpg/entity/entity.c b/src/rpg/entity/entity.c index 6a65885..c9ab810 100644 --- a/src/rpg/entity/entity.c +++ b/src/rpg/entity/entity.c @@ -100,4 +100,8 @@ void entityUpdate(entity_t *entity) { entity->velocity[1] = 0.0f; } } + + if(entity->type == ENTITY_TYPE_PLAYER) { + playerInteraction(entity); + } } \ No newline at end of file diff --git a/src/rpg/entity/player.c b/src/rpg/entity/player.c index 63257b0..1246e35 100644 --- a/src/rpg/entity/player.c +++ b/src/rpg/entity/player.c @@ -59,4 +59,57 @@ void playerMovement(entity_t *entity) { } else if(dir[1] > 0) { entity->direction = DIRECTION_DOWN; } +} + +void playerInteraction(entity_t *entity) { + assertNotNull(entity, "Entity pointer cannot be NULL"); + + if(!inputPressed(INPUT_ACTION_ACCEPT)) return; + + physicsbox_t interactBox; + + // Get direction vector + directionGetVec2(entity->direction, interactBox.min); + + // Scale by interact range + glm_vec2_scale(interactBox.min, PLAYER_INTERACTION_RANGE, interactBox.min); + + // Add entity position, this makes the center of the box. + glm_vec2_add(interactBox.min, entity->position, interactBox.min); + + // Copy to max + glm_vec2_copy(interactBox.min, interactBox.max); + + // Size of the hitbox + vec2 halfSize = { + TILESET_ENTITIES.tileWidth * PLAYER_INTERACTION_SIZE * 0.5f, + TILESET_ENTITIES.tileHeight * PLAYER_INTERACTION_SIZE * 0.5f + }; + + // Subtract from min, add to max. + glm_vec2_sub(interactBox.min, halfSize, interactBox.min); + glm_vec2_add(interactBox.max, halfSize, interactBox.max); + + // For each entity + entity_t *start = entity->map->entities; + entity_t *end = &entity->map->entities[entity->map->entityCount]; + vec2 otherSize = { TILESET_ENTITIES.tileWidth, TILESET_ENTITIES.tileHeight }; + physicsbox_t otherBox; + physicsboxboxresult_t result; + + do { + if(start->type != ENTITY_TYPE_NPC) continue; + + // Setup other box. + glm_vec2_copy(start->position, otherBox.min); + glm_vec2_copy(start->position, otherBox.max); + glm_vec2_sub(otherBox.min, otherSize, otherBox.min); + glm_vec2_add(otherBox.min, otherSize, otherBox.max); + + physicsBoxCheckBox(interactBox, otherBox, &result); + if(!result.hit) continue; + + printf("Interacted with entity at (%.2f, %.2f)\n", start->position[0], start->position[1]); + break; + } while(++start != end); } \ No newline at end of file diff --git a/src/rpg/entity/player.h b/src/rpg/entity/player.h index ea88ef6..efb3326 100644 --- a/src/rpg/entity/player.h +++ b/src/rpg/entity/player.h @@ -9,6 +9,8 @@ #include "dusk.h" #define PLAYER_SPEED 64.0f +#define PLAYER_INTERACTION_RANGE 1.0f +#define PLAYER_INTERACTION_SIZE 0.5f typedef struct entity_s entity_t; @@ -28,4 +30,11 @@ void playerInit(entity_t *entity); * * @param entity Pointer to the player entity structure. */ -void playerMovement(entity_t *entity); \ No newline at end of file +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