aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2026-01-07 03:57:09 +0000
committernathan <nathansmith@disroot.org>2026-01-07 03:57:09 +0000
commitfd070849940355741b69fe5fbdf75a01dcc8f424 (patch)
tree068b2d87e5db33fe9978a8a1fd5c3fef8697bd55
parent382cb08822808f57902f67a842259e64afb33e72 (diff)
downloadFindThings-fd070849940355741b69fe5fbdf75a01dcc8f424.tar.gz
FindThings-fd070849940355741b69fe5fbdf75a01dcc8f424.tar.bz2
FindThings-fd070849940355741b69fe5fbdf75a01dcc8f424.zip
Cool text animation thingy
-rw-r--r--src/entities/samantha.c2
-rw-r--r--src/player.c19
-rw-r--r--src/settings.c3
-rw-r--r--src/settings.h1
-rw-r--r--src/ui.c48
-rw-r--r--src/ui.h6
-rw-r--r--src/utils.c12
-rw-r--r--src/utils.h2
8 files changed, 81 insertions, 12 deletions
diff --git a/src/entities/samantha.c b/src/entities/samantha.c
index 66b8996..8ee5009 100644
--- a/src/entities/samantha.c
+++ b/src/entities/samantha.c
@@ -49,7 +49,7 @@ InteractionCommand interactWithSamantha(Entity* entity, Game* game,
switch (selection)
{
case SELECTION_INTERACT:
- setInteractionChat(chat, "hihi");
+ setInteractionChat(chat, "hihi, i is thr samantha");
return INTERACTION_TALK;
case SELECTION_NEXT_MESSAGE:
if (samantha->dialogCount == 0)
diff --git a/src/player.c b/src/player.c
index c3e9119..df59ffe 100644
--- a/src/player.c
+++ b/src/player.c
@@ -1,6 +1,7 @@
#include "player.h"
#include "game.h"
#include "settings.h"
+#include "ui.h"
Player createPlayer()
{
@@ -408,13 +409,27 @@ void playerUpdateSelectedEntity(Player* player, WorldUID uid, Game* game)
{
playerInteractWithEntity(player, uid, game, SELECTION_INTERACT);
}
- else if (IsKeyPressed(game->settings.leaveKey))
+
+ // Actions that should only be done while interacting.
+ if (!player->isInteracting)
+ {
+ return;
+ }
+
+ if (IsKeyPressed(game->settings.leaveKey))
{
playerInteractWithEntity(player, uid, game, SELECTION_LEAVE);
}
else if (IsKeyPressed(game->settings.nextMessageKey))
{
- playerInteractWithEntity(player, uid, game, SELECTION_NEXT_MESSAGE);
+ if (isInteractionChatAnimationDone(chat))
+ {
+ playerInteractWithEntity(player, uid, game, SELECTION_NEXT_MESSAGE);
+ }
+ else
+ {
+ endInteractionChatAnimation(chat);
+ }
}
else if (keyPressed >= KEY_ONE && keyPressed <= KEY_NINE)
{
diff --git a/src/settings.c b/src/settings.c
index d31c7cc..78aa0d6 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -8,7 +8,7 @@ Settings defaultSettings()
.screenWidth = 596,
.screenHeight = 447,
.fov = 90.0,
- .maxFPS = 0,
+ .maxFPS = 60,
.showFPSDefault = true,
.backgroundColor = (Color){74, 42, 74, 255},
.useBackgroundTexture = true,
@@ -43,6 +43,7 @@ Settings defaultSettings()
.interactionOutlineSize = 2.0,
.interactionChatHeight = 300.0,
.interactionMenuWidth = 500.0,
+ .interactionChatAnimationSpeed = 0.03,
.mouseSpeed = 0.1,
.forwardKey = KEY_W,
.backwardKey = KEY_S,
diff --git a/src/settings.h b/src/settings.h
index 9b04e0a..a59b36a 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -65,6 +65,7 @@ typedef struct {
float interactionOutlineSize;
float interactionChatHeight;
float interactionMenuWidth;
+ float interactionChatAnimationSpeed;
// Controls.
float mouseSpeed;
diff --git a/src/ui.c b/src/ui.c
index b8451e5..4035b44 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -18,7 +18,7 @@ void resizeInteractionChat(InteractionChat* chat, const Settings* settings)
void initInteractionChat(InteractionChat* chat, const Settings* settings)
{
- memset(&chat->text, 0, INTERACTION_CHAT_MAX * sizeof(char));
+ clearInteractionChat(chat);
chat->visible = false;
chat->entityId = ENTITY_NONE;
@@ -37,17 +37,37 @@ void hideInteractionChat(InteractionChat* chat)
void setInteractionChat(InteractionChat* chat, const char* text)
{
- strncpy(chat->text, text, INTERACTION_CHAT_MAX * sizeof(char) - 1);
+ strncpy(chat->text, text, INTERACTION_CHAT_MAX - 1);
+ chat->textSize = getStringLength(text, INTERACTION_CHAT_MAX);
+ chat->displayUpTo = 0;
+ chat->lastCharacterUpdate = GetTime();
}
void writeToInteractionChat(InteractionChat* chat, const char* text)
{
- strncat(chat->text, text, INTERACTION_CHAT_MAX * sizeof(char) - 1);
+ strncat(chat->text, text, INTERACTION_CHAT_MAX - 1);
+ chat->textSize += getStringLength(text, INTERACTION_CHAT_MAX);
+ chat->textSize %= INTERACTION_CHAT_MAX;
+ chat->lastCharacterUpdate = GetTime();
}
void clearInteractionChat(InteractionChat* chat)
{
- memset(&chat->text, 0, INTERACTION_CHAT_MAX * sizeof(char));
+ memset(&chat->text, 0, INTERACTION_CHAT_MAX);
+ memset(&chat->displayedText, 0, INTERACTION_CHAT_MAX);
+ chat->textSize = 0;
+ chat->displayUpTo = 0;
+}
+
+bool isInteractionChatAnimationDone(InteractionChat* chat)
+{
+ return chat->displayUpTo >= chat->textSize;
+}
+
+void endInteractionChatAnimation(InteractionChat* chat)
+{
+ chat->displayUpTo = chat->textSize;
+ strncpy(chat->displayedText, chat->text, chat->textSize);
}
void updateInteractionChat(InteractionChat* chat, Game* game)
@@ -72,6 +92,19 @@ void updateInteractionChat(InteractionChat* chat, Game* game)
DrawRectangleLinesEx(chat->rect, lineThickness, BLACK);
+ // Do cool animation.
+ double currentTime = GetTime();
+
+ if (chat->displayUpTo < chat->textSize &&
+ currentTime - chat->lastCharacterUpdate >=
+ game->settings.interactionChatAnimationSpeed)
+ {
+ chat->lastCharacterUpdate = currentTime;
+ chat->displayedText[chat->displayUpTo] = chat->text[chat->displayUpTo];
+ ++chat->displayUpTo;
+ }
+
+ // Draw text.
if (chat->entityId != ENTITY_NONE)
{
DrawText(TextFormat("%s says:", getEntityName(chat->entityId)),
@@ -81,7 +114,7 @@ void updateInteractionChat(InteractionChat* chat, Game* game)
WHITE);
}
- DrawText(chat->text,
+ DrawText(chat->displayedText,
chat->rect.x + border,
chat->rect.y + (border * 2.0) + fontSize,
fontSize,
@@ -127,8 +160,7 @@ void setInteractionMenu(InteractionMenu* menu,
for (int index = 0; index < itemCount; ++index)
{
- strncpy(menu->items[index], items[index],
- INTERACTION_LABEL_MAX * sizeof(char) - 1);
+ strncpy(menu->items[index], items[index], INTERACTION_LABEL_MAX - 1);
}
}
@@ -136,7 +168,7 @@ void resetInteractionMenu(InteractionMenu* menu)
{
for (int index = 0; index < INTERACTION_MENU_MAX; ++index)
{
- memset(menu->items[index], 0, INTERACTION_LABEL_MAX * sizeof(char));
+ memset(menu->items[index], 0, INTERACTION_LABEL_MAX);
}
menu->itemCount = 0;
diff --git a/src/ui.h b/src/ui.h
index 2c66eca..9d3f999 100644
--- a/src/ui.h
+++ b/src/ui.h
@@ -11,6 +11,10 @@ typedef char InteractionItems[INTERACTION_MENU_MAX][INTERACTION_LABEL_MAX];
typedef struct {
char text[INTERACTION_CHAT_MAX];
+ char displayedText[INTERACTION_CHAT_MAX];
+ size_t textSize;
+ int displayUpTo; // Used for cool animation.
+ double lastCharacterUpdate;
Rectangle rect;
bool visible;
EntityId entityId;
@@ -31,6 +35,8 @@ void hideInteractionChat(InteractionChat* chat);
void setInteractionChat(InteractionChat* chat, const char* text);
void writeToInteractionChat(InteractionChat* chat, const char* text);
void clearInteractionChat(InteractionChat* chat);
+bool isInteractionChatAnimationDone(InteractionChat* chat);
+void endInteractionChatAnimation(InteractionChat* chat);
void updateInteractionChat(InteractionChat* chat, Game* game);
// Interaction menu procedures.
diff --git a/src/utils.c b/src/utils.c
index 22ed7b4..e2e7d35 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -45,4 +45,16 @@ Image colorsToImage(Color* colors, int width, int height)
};
}
+size_t getStringLength(const char* text, size_t maxLength)
+{
+ size_t stringLength = 0;
+
+ while (stringLength < maxLength && text[stringLength] != '\0')
+ {
+ ++stringLength;
+ }
+
+ return stringLength;
+}
+
// Why does the universe feel strange to exist in?
diff --git a/src/utils.h b/src/utils.h
index 7b43dba..7947041 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -73,4 +73,6 @@ Vector3 randomDirection3(int seed, int* nextSeed);
Image colorsToImage(Color* colors, int width, int height);
+size_t getStringLength(const char* text, size_t maxLength);
+
#endif