Made NPCs interactable with the conversation system.

This commit is contained in:
2024-10-17 07:36:33 -07:00
parent 7867076bbe
commit 258976b76c
11 changed files with 77 additions and 53 deletions

View File

@ -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."
}
}
}

View File

@ -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,

View File

@ -9,6 +9,6 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
conversation.c
conversationsign.c
testconversation.c
conversationinteract.c
)

View File

@ -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;
}
}

View File

@ -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);
uint16_t conversationInteractInit(conversation_t *convo, const uint16_t i);
uint16_t conversationInteractUpdate(conversation_t *convo, const uint16_t i);

View File

@ -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;
}
}

View File

@ -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:

View File

@ -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);
}

View File

@ -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;
/**

View File

@ -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);
}

View File

@ -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;
/**