diff --git a/src/dusk/overworld/entity/entity.c b/src/dusk/overworld/entity/entity.c index 77d0c6c..9857ae5 100644 --- a/src/dusk/overworld/entity/entity.c +++ b/src/dusk/overworld/entity/entity.c @@ -109,7 +109,27 @@ void entityMove(entity_t *ent, const facingdir_t dir) { } } -bool entityIsMoving(const entity_t *ent) { +bool_t entityIsMoving(const entity_t *ent) { assertNotNull(ent, "Entity cannot be NULL"); return ent->subX != 0 || ent->subY != 0; +} + +bool_t entityInteract(const entity_t *ent) { + assertNotNull(ent, "Entity cannot be NULL."); + assertTrue(ent->type == ENTITY_TYPE_PLAYER, "Entity should be player."); + if(entityIsMoving(ent)) return false; + + uint8_t targetX = ent->x, targetY = ent->y; + facingDirAdd(ent->direction, &targetX, &targetY); + + entity_t *target = overworldEntityGetAtPosition(targetX, targetY); + assertFalse(target == ent, "Cannot interact with self."); + + if(target == NULL) return false; + assertTrue(target->type < ENTITY_TYPE_COUNT, "Invalid entity type."); + + if(ENTITY_CALLBACKS[target->type].interact == NULL) return false; + ENTITY_CALLBACKS[target->type].interact(target); + + return true; } \ No newline at end of file diff --git a/src/dusk/overworld/entity/entity.h b/src/dusk/overworld/entity/entity.h index 52d58a5..f095099 100644 --- a/src/dusk/overworld/entity/entity.h +++ b/src/dusk/overworld/entity/entity.h @@ -78,4 +78,12 @@ void entityMove(entity_t *entity, const facingdir_t dir); * @param entity The entity to check. * @return TRUE if the entity is moving, FALSE otherwise. */ -bool_t entityIsMoving(const entity_t *entity); \ No newline at end of file +bool_t entityIsMoving(const entity_t *entity); + +/** + * Make this entity attempt to interact with the world. + * + * @param entity The entity that is doing the interaction. + * @return TRUE if an entity was interacted with, FALSE otherwise. + */ +bool_t entityInteract(const entity_t *entity); \ No newline at end of file diff --git a/src/dusk/overworld/entity/npc.c b/src/dusk/overworld/entity/npc.c index 2f30d29..45c66ba 100644 --- a/src/dusk/overworld/entity/npc.c +++ b/src/dusk/overworld/entity/npc.c @@ -14,4 +14,5 @@ void npcUpdate(entity_t *ent) { } void npcInteract(entity_t *ent) { + printf("Interact\n"); } \ No newline at end of file diff --git a/src/dusk/overworld/entity/player.c b/src/dusk/overworld/entity/player.c index 1cbd52f..42aac7e 100644 --- a/src/dusk/overworld/entity/player.c +++ b/src/dusk/overworld/entity/player.c @@ -27,6 +27,7 @@ void playerUpdate(entity_t *ent) { } else if(inputIsDown(INPUT_DOWN)) { entityMove(ent, FACING_DIRECTION_SOUTH); } else if(inputWasPressed(INPUT_ACCEPT)) { + entityInteract(ent); } } \ No newline at end of file