Interact
This commit is contained in:
@@ -56,7 +56,7 @@ void entityUpdate(entity_t *entity) {
|
||||
|
||||
// Movement code.
|
||||
if(entity->type == ENTITY_TYPE_PLAYER) {
|
||||
playerMovement(entity);
|
||||
playerInput(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,3 +109,19 @@ 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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
void entityDirGetRelative(
|
||||
const entitydir_t dir, worldunits_t *relX, worldunits_t *relY
|
||||
);
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
void playerInput(entity_t *entity);
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user