From 258976b76c192dc4ca7a0ccc072014e1517c8393 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Thu, 17 Oct 2024 07:36:33 -0700 Subject: [PATCH] Made NPCs interactable with the conversation system. --- assets/en.json | 14 +++++-- assets/testmap.json | 4 +- src/dawn/rpg/conversation/CMakeLists.txt | 2 +- .../rpg/conversation/conversationinteract.c | 42 +++++++++++++++++++ ...versationsign.h => conversationinteract.h} | 4 +- src/dawn/rpg/conversation/conversationsign.c | 32 -------------- src/dawn/rpg/entity/interact.c | 16 +++++-- src/dawn/rpg/entity/npc.c | 4 +- src/dawn/rpg/entity/npc.h | 6 +-- src/dawn/rpg/entity/sign.c | 2 +- src/dawn/rpg/entity/sign.h | 4 +- 11 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 src/dawn/rpg/conversation/conversationinteract.c rename src/dawn/rpg/conversation/{conversationsign.h => conversationinteract.h} (54%) delete mode 100644 src/dawn/rpg/conversation/conversationsign.c diff --git a/assets/en.json b/assets/en.json index 69c86f61..0474a7e2 100644 --- a/assets/en.json +++ b/assets/en.json @@ -5,12 +5,18 @@ "options": "Options", "exit": "Exit" }, + "entities": { + "sign": { + "name": "Sign" + }, + "bob": { + "name": "Bob" + } + }, "maps": { "testmap": { - "sign": { - "text": "This is a sign.", - "name": "Sign" - } + "bob": "Hello, I am Bob.", + "sign": "This is a sign." } } } \ No newline at end of file diff --git a/assets/testmap.json b/assets/testmap.json index 9259338e..b48379b6 100644 --- a/assets/testmap.json +++ b/assets/testmap.json @@ -33,8 +33,8 @@ "type": 2, "x": 9, "y": 9, - "name": "Bob", - "text": "Hello, I am Bob." + "name": "entities.bob.name", + "text": "maps.testmap.bob" }, { "type": 3, diff --git a/src/dawn/rpg/conversation/CMakeLists.txt b/src/dawn/rpg/conversation/CMakeLists.txt index 870ab965..18f9c29e 100644 --- a/src/dawn/rpg/conversation/CMakeLists.txt +++ b/src/dawn/rpg/conversation/CMakeLists.txt @@ -9,6 +9,6 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE conversation.c - conversationsign.c testconversation.c + conversationinteract.c ) \ No newline at end of file diff --git a/src/dawn/rpg/conversation/conversationinteract.c b/src/dawn/rpg/conversation/conversationinteract.c new file mode 100644 index 00000000..7d7acfde --- /dev/null +++ b/src/dawn/rpg/conversation/conversationinteract.c @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "conversationinteract.h" +#include "assert/assert.h" +#include "rpg/entity/entity.h" +#include "locale/language.h" +#include "ui/textbox.h" + +uint16_t conversationInteractInit(conversation_t *convo, const uint16_t i) { + assertNotNull(convo, "Conversation is NULL!"); + assertNotNull(convo->user, "Conversation user is NULL!"); + + entity_t *e = (entity_t*)convo->user; + switch(e->type) { + case ENTITY_TYPE_SIGN: + textboxSetText("entities.sign.name", e->sign.text); + return 0; + + case ENTITY_TYPE_NPC: + textboxSetText(e->npc.name, e->npc.text); + return 0; + + default: + assertUnreachable("Invalid entity type for conversation."); + return CONVERSATION_INVALID; + } +} + +uint16_t conversationInteractUpdate(conversation_t *convo, const uint16_t i) { + switch(i) { + case 0: + return textboxIsOpen() ? CONVERSATION_CONTINUE : CONVERSATION_DONE; + + default: + return CONVERSATION_INVALID; + } +} \ No newline at end of file diff --git a/src/dawn/rpg/conversation/conversationsign.h b/src/dawn/rpg/conversation/conversationinteract.h similarity index 54% rename from src/dawn/rpg/conversation/conversationsign.h rename to src/dawn/rpg/conversation/conversationinteract.h index d0bb0451..ba5273b0 100644 --- a/src/dawn/rpg/conversation/conversationsign.h +++ b/src/dawn/rpg/conversation/conversationinteract.h @@ -8,5 +8,5 @@ #pragma once #include "conversation.h" -uint16_t conversationSignInit(conversation_t *convo, const uint16_t i); -uint16_t conversationSignUpdate(conversation_t *convo, const uint16_t i); \ No newline at end of file +uint16_t conversationInteractInit(conversation_t *convo, const uint16_t i); +uint16_t conversationInteractUpdate(conversation_t *convo, const uint16_t i); \ No newline at end of file diff --git a/src/dawn/rpg/conversation/conversationsign.c b/src/dawn/rpg/conversation/conversationsign.c deleted file mode 100644 index 06a99378..00000000 --- a/src/dawn/rpg/conversation/conversationsign.c +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright (c) 2024 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "conversationsign.h" -#include "assert/assert.h" -#include "rpg/entity/entity.h" -#include "locale/language.h" - -uint16_t conversationSignInit(conversation_t *convo, const uint16_t i) { - assertNotNull(convo, "Conversation is NULL!"); - assertNotNull(convo->user, "Conversation user is NULL!"); - - entity_t *e = (entity_t*)convo->user; - assertTrue(e->type == ENTITY_TYPE_SIGN, "Entity is not a sign!"); - textboxSetText("maps.testmap.sign.name", "maps.testmap.sign.text"); - - return 0; -} - -uint16_t conversationSignUpdate(conversation_t *convo, const uint16_t i) { - switch(i) { - case 0: - return textboxIsOpen() ? CONVERSATION_CONTINUE : CONVERSATION_DONE; - - default: - return CONVERSATION_INVALID; - } -} \ No newline at end of file diff --git a/src/dawn/rpg/entity/interact.c b/src/dawn/rpg/entity/interact.c index babe6226..73cff03f 100644 --- a/src/dawn/rpg/entity/interact.c +++ b/src/dawn/rpg/entity/interact.c @@ -9,8 +9,8 @@ #include "assert/assert.h" #include "game/game.h" #include "locale/language.h" - -#include "rpg/conversation/conversationsign.h" +#include "rpg/conversation/conversationinteract.h" +#include "ui/textbox.h" void entityInteractEntity( entity_t *source, @@ -28,11 +28,19 @@ void entityInteractEntity( target->x, target->y, source->x, source->y ); - textboxSetText(target->npc.name, target->npc.text); + conversationSet( + conversationInteractInit, + conversationInteractUpdate, + target + ); return; case ENTITY_TYPE_SIGN: - conversationSet(conversationSignInit, conversationSignUpdate, target); + conversationSet( + conversationInteractInit, + conversationInteractUpdate, + target + ); return; case ENTITY_TYPE_DOOR: diff --git a/src/dawn/rpg/entity/npc.c b/src/dawn/rpg/entity/npc.c index b395ba5c..17a4b4d8 100644 --- a/src/dawn/rpg/entity/npc.c +++ b/src/dawn/rpg/entity/npc.c @@ -17,11 +17,11 @@ void npcInit(npc_t *npc) { void npcNameSet(npc_t *npc, const char_t *name) { assertNotNull(npc, "npcNameSet: NPC is NULL!"); assertNotNull(name, "npcNameSet: Name is NULL!"); - strncpy(npc->name, name, TEXTBOX_TITLE_MAX); + strncpy(npc->name, name, LANGUAGE_STRING_KEY_LENGTH_MAX); } void npcTextSet(npc_t *npc, const char_t *text) { assertNotNull(npc, "npcTextSet: NPC is NULL!"); assertNotNull(text, "npcTextSet: Text is NULL!"); - strncpy(npc->text, text, TEXTBOX_TEXT_MAX); + strncpy(npc->text, text, LANGUAGE_STRING_KEY_LENGTH_MAX); } \ No newline at end of file diff --git a/src/dawn/rpg/entity/npc.h b/src/dawn/rpg/entity/npc.h index 5df8bcbc..a351b6c8 100644 --- a/src/dawn/rpg/entity/npc.h +++ b/src/dawn/rpg/entity/npc.h @@ -6,11 +6,11 @@ */ #pragma once -#include "ui/textbox.h" +#include "locale/language.h" typedef struct { - char_t name[TEXTBOX_TITLE_MAX+1]; - char_t text[TEXTBOX_TEXT_MAX+1]; + char_t name[LANGUAGE_STRING_KEY_LENGTH_MAX+1]; + char_t text[LANGUAGE_STRING_KEY_LENGTH_MAX+1]; } npc_t; /** diff --git a/src/dawn/rpg/entity/sign.c b/src/dawn/rpg/entity/sign.c index 6654e110..8940984d 100644 --- a/src/dawn/rpg/entity/sign.c +++ b/src/dawn/rpg/entity/sign.c @@ -16,5 +16,5 @@ void signInit(sign_t *sign) { void signTextSet(sign_t *sign, const char_t *text) { assertNotNull(sign, "signTextSet: Sign is NULL!"); assertNotNull(text, "signTextSet: Text is NULL!"); - strncpy(sign->text, text, TEXTBOX_TEXT_MAX); + strncpy(sign->text, text, LANGUAGE_STRING_KEY_LENGTH_MAX); } \ No newline at end of file diff --git a/src/dawn/rpg/entity/sign.h b/src/dawn/rpg/entity/sign.h index 0316b5c2..42eff634 100644 --- a/src/dawn/rpg/entity/sign.h +++ b/src/dawn/rpg/entity/sign.h @@ -6,10 +6,10 @@ */ #pragma once -#include "ui/textbox.h" +#include "locale/language.h" typedef struct { - char_t text[TEXTBOX_TEXT_MAX + 1]; + char_t text[LANGUAGE_STRING_KEY_LENGTH_MAX+1]; } sign_t; /**