aboutsummaryrefslogtreecommitdiffstats
path: root/src/player.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c78
1 files changed, 68 insertions, 10 deletions
diff --git a/src/player.c b/src/player.c
index 2d9ec62..715d5ed 100644
--- a/src/player.c
+++ b/src/player.c
@@ -14,7 +14,8 @@ Player createPlayer()
.fovy = 90.0,
.projection = CAMERA_PERSPECTIVE
},
- .cameraAngle = Vector2Zero()
+ .cameraAngle = Vector2Zero(),
+ .interactingWith = ENTITY_NONE
};
}
@@ -100,27 +101,77 @@ bool playerCanEntityBeSelected(Player* player, Entity entity)
<= PLAYER_MAX_SELECT_DISTANCE;
}
-void playerInteractWithEntity(Player* player, Entity* entity, Game* game)
+void playerInteractWithEntity(Player* player, WorldUID uid, Game* game,
+ Selection selection)
{
- printf("%d\n", interactWithEntity(entity, game, SELECTION_INTERACT));
+ InteractionChat* chat = &game->chat;
+ Entity* entity = &game->world.entities[uid];
+ player->interactingWith = uid;
+
+ // Handle selection type.
+ switch (selection)
+ {
+ case SELECTION_INTERACT:
+ clearInteractionChat(chat);
+ chat->entityId = entity->id;
+ break;
+ case SELECTION_LEAVE:
+ player->interactingWith = ENTITY_NONE;
+ chat->entityId = ENTITY_NONE;
+ break;
+ default:
+ break;
+ }
+
+ // Interact with it.
+ switch (interactWithEntity(entity, game, selection))
+ {
+ case INTERACTION_TALK:
+ showInteractionChat(chat);
+ break;
+ case INTERACTION_END:
+ hideInteractionChat(chat);
+ player->interactingWith = ENTITY_NONE;
+ chat->entityId = ENTITY_NONE;
+ break;
+ default:
+ break;
+ }
}
-void playerUpdateSelectedEntity(Player* player, Entity* entity, Game* game)
+void playerUpdateSelectedEntity(Player* player, WorldUID uid, Game* game)
{
+ Entity* entity = &game->world.entities[uid];
+
if (!playerCanEntityBeSelected(player, *entity))
{
+ if (uid == player->interactingWith)
+ {
+ playerInteractWithEntity(player, uid, game, SELECTION_LEAVE);
+ }
+
return;
}
+
+ Color color = PINK;
+
+ if (uid == player->interactingWith)
+ {
+ color = YELLOW;
+ }
- DrawBoundingBox(entity->box, RED);
+ DrawBoundingBox(entity->box, color);
if (IsKeyPressed(game->settings.interactKey))
{
- playerInteractWithEntity(player, entity, game);
+ playerInteractWithEntity(player, uid, game, SELECTION_INTERACT);
+ }
+ else if (IsKeyPressed(game->settings.nextMessageKey))
+ {
+ playerInteractWithEntity(player, uid, game, SELECTION_NEXT_MESSAGE);
}
}
-// TODO: move magic numbers to settings
void updatePlayer(Player* player, Game* game)
{
updatePlayerMovement(player, game);
@@ -135,10 +186,17 @@ void updatePlayer(Player* player, Game* game)
DrawRay(ray, YELLOW);
}
- WorldUID uid = castRayAtWorld(&game->world, ray, false, NULL);
+ if (player->interactingWith == ENTITY_NONE)
+ {
+ WorldUID uid = castRayAtWorld(&game->world, ray, false, NULL);
- if (uid != -1)
+ if (uid != -1)
+ {
+ playerUpdateSelectedEntity(player, uid, game);
+ }
+ }
+ else
{
- playerUpdateSelectedEntity(player, &game->world.entities[uid], game);
+ playerUpdateSelectedEntity(player, player->interactingWith, game);
}
}