Fixed JSON parsing throwing errors

This commit is contained in:
2024-10-06 22:46:16 -05:00
parent bf3912bb7f
commit ecb3b9c5d1
11 changed files with 129 additions and 5 deletions

View File

@ -13,4 +13,5 @@ target_sources(${DAWN_TARGET_NAME}
player.c
sign.c
interact.c
npc.c
)

View File

@ -29,6 +29,14 @@ void entityInit(
assertUnreachable("Should not be initializing a NULL entity.");
break;
case ENTITY_TYPE_NPC:
npcInit(&entity->npc);
break;
case ENTITY_TYPE_SIGN:
signInit(&entity->sign);
break;
case ENTITY_TYPE_PLAYER:
playerInit(entity);
break;

View File

@ -9,6 +9,7 @@
#include "player.h"
#include "sign.h"
#include "entitydirection.h"
#include "npc.h"
typedef struct _map_t map_t;
@ -46,6 +47,7 @@ typedef struct _entity_t {
// Type data
union {
player_t player;
npc_t npc;
sign_t sign;
};
} entity_t;

View File

@ -16,10 +16,20 @@ void entityInteractEntity(
assertNotNull(target, "entityInteractEntity: Target is NULL!");
switch(target->type) {
case ENTITY_TYPE_NPC:
source->state = ENTITY_STATE_TALKING;
target->state = ENTITY_STATE_TALKING;
target->direction = entityDirectionLookAt(
target->x, target->y,
source->x, source->y
);
textboxSetText(target->npc.name, target->npc.text);
return;
case ENTITY_TYPE_SIGN:
source->state = ENTITY_STATE_TALKING;
target->state = ENTITY_STATE_TALKING;
textboxSetText(NULL, target->sign.text);
textboxSetText("Sign", target->sign.text);
return;
default:

27
src/dawn/rpg/entity/npc.c Normal file
View File

@ -0,0 +1,27 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "npc.h"
#include "assert/assert.h"
void npcInit(npc_t *npc) {
assertNotNull(npc, "npcInit: NPC is NULL!");
npc->name[0] = '\0';
npc->text[0] = '\0';
}
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);
}
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);
}

37
src/dawn/rpg/entity/npc.h Normal file
View File

@ -0,0 +1,37 @@
/**
* Copyright (c) 2024 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "ui/textbox.h"
typedef struct {
char_t name[TEXTBOX_TITLE_MAX+1];
char_t text[TEXTBOX_TEXT_MAX+1];
} npc_t;
/**
* Initializes an NPC.
*
* @param npc NPC to initialize.
*/
void npcInit(npc_t *npc);
/**
* Sets the name of an NPC.
*
* @param npc NPC to set name for.
* @param name Name to set.
*/
void npcNameSet(npc_t *npc, const char_t *name);
/**
* Sets the text of an NPC.
*
* @param npc NPC to set text for.
* @param text Text to set.
*/
void npcTextSet(npc_t *npc, const char_t *text);

View File

@ -8,6 +8,11 @@
#include "sign.h"
#include "assert/assert.h"
void signInit(sign_t *sign) {
assertNotNull(sign, "signInit: Sign is NULL!");
sign->text[0] = '\0';
}
void signTextSet(sign_t *sign, const char_t *text) {
assertNotNull(sign, "signTextSet: Sign is NULL!");
assertNotNull(text, "signTextSet: Text is NULL!");

View File

@ -12,6 +12,13 @@ typedef struct {
char_t text[TEXTBOX_TEXT_MAX + 1];
} sign_t;
/**
* Initializes a sign.
*
* @param sign Sign to initialize.
*/
void signInit(sign_t *sign);
/**
* Sets the text of a sign.
*