Add interactions.
This commit is contained in:
@ -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;
|
||||
}
|
@ -79,3 +79,11 @@ void entityMove(entity_t *entity, const facingdir_t dir);
|
||||
* @return TRUE if the entity is moving, FALSE otherwise.
|
||||
*/
|
||||
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);
|
@ -14,4 +14,5 @@ void npcUpdate(entity_t *ent) {
|
||||
}
|
||||
|
||||
void npcInteract(entity_t *ent) {
|
||||
printf("Interact\n");
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user