Basic interaction
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
@@ -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
|
||||
);
|
||||
void directionGetVec2(const direction_t dir, vec2 out);
|
@@ -100,4 +100,8 @@ void entityUpdate(entity_t *entity) {
|
||||
entity->velocity[1] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if(entity->type == ENTITY_TYPE_PLAYER) {
|
||||
playerInteraction(entity);
|
||||
}
|
||||
}
|
@@ -60,3 +60,56 @@ void playerMovement(entity_t *entity) {
|
||||
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);
|
||||
}
|
@@ -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;
|
||||
|
||||
@@ -29,3 +31,10 @@ 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);
|
Reference in New Issue
Block a user