Basic interaction
This commit is contained in:
@@ -18,36 +18,31 @@ float_t directionToAngle(const direction_t dir) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void directionGetCoordinates(
|
void directionGetVec2(const direction_t dir, vec2 out) {
|
||||||
const direction_t dir,
|
assertNotNull(out, "Output vector cannot be NULL");
|
||||||
int8_t *x, int8_t *y
|
|
||||||
) {
|
|
||||||
assertNotNull(x, "X coordinate pointer cannot be NULL");
|
|
||||||
assertNotNull(y, "Y coordinate pointer cannot be NULL");
|
|
||||||
|
|
||||||
switch(dir) {
|
switch(dir) {
|
||||||
case DIRECTION_NORTH:
|
case DIRECTION_NORTH:
|
||||||
*x = 0;
|
out[0] = 0.0f;
|
||||||
*y = -1;
|
out[1] = 1.0f;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DIRECTION_SOUTH:
|
case DIRECTION_SOUTH:
|
||||||
*x = 0;
|
out[0] = 0.0f;
|
||||||
*y = 1;
|
out[1] = -1.0f;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DIRECTION_EAST:
|
case DIRECTION_EAST:
|
||||||
*x = 1;
|
out[0] = 1.0f;
|
||||||
*y = 0;
|
out[1] = 0.0f;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DIRECTION_WEST:
|
case DIRECTION_WEST:
|
||||||
*x = -1;
|
out[0] = -1.0f;
|
||||||
*y = 0;
|
out[1] = 0.0f;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
assertUnreachable("Invalid direction");
|
assertUnreachable("Invalid direction");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -29,13 +29,9 @@ typedef enum {
|
|||||||
float_t directionToAngle(const direction_t dir);
|
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 dir The direction to convert.
|
||||||
* @param x Pointer to store the x coordinate.
|
* @param out Pointer to the vec2 array to populate.
|
||||||
* @param y Pointer to store the y coordinate.
|
|
||||||
*/
|
*/
|
||||||
void directionGetCoordinates(
|
void directionGetVec2(const direction_t dir, vec2 out);
|
||||||
const direction_t dir,
|
|
||||||
int8_t *x, int8_t *y
|
|
||||||
);
|
|
@@ -100,4 +100,8 @@ void entityUpdate(entity_t *entity) {
|
|||||||
entity->velocity[1] = 0.0f;
|
entity->velocity[1] = 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(entity->type == ENTITY_TYPE_PLAYER) {
|
||||||
|
playerInteraction(entity);
|
||||||
|
}
|
||||||
}
|
}
|
@@ -59,4 +59,57 @@ void playerMovement(entity_t *entity) {
|
|||||||
} else if(dir[1] > 0) {
|
} else if(dir[1] > 0) {
|
||||||
entity->direction = DIRECTION_DOWN;
|
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"
|
#include "dusk.h"
|
||||||
|
|
||||||
#define PLAYER_SPEED 64.0f
|
#define PLAYER_SPEED 64.0f
|
||||||
|
#define PLAYER_INTERACTION_RANGE 1.0f
|
||||||
|
#define PLAYER_INTERACTION_SIZE 0.5f
|
||||||
|
|
||||||
typedef struct entity_s entity_t;
|
typedef struct entity_s entity_t;
|
||||||
|
|
||||||
@@ -28,4 +30,11 @@ void playerInit(entity_t *entity);
|
|||||||
*
|
*
|
||||||
* @param entity Pointer to the player entity structure.
|
* @param entity Pointer to the player entity structure.
|
||||||
*/
|
*/
|
||||||
void playerMovement(entity_t *entity);
|
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