Working on game state
This commit is contained in:
1 parent
b01e970554
commit
2807d82d51
12 files changed
+501
-445
No files matched your search
@@ -364,4 +364,46 @@ void DialogueRuntime::presentChoices(const Node& node) {
|
||||
presentation.cutsceneBlackAlpha = 0.0f;
|
||||
}
|
||||
|
||||
void DialogueRuntime::save(nlohmann::json& out) const
|
||||
{
|
||||
if (!isActive() || !activeDialogue) {
|
||||
out["active"] = false;
|
||||
return;
|
||||
}
|
||||
out["active"] = true;
|
||||
out["dialogueId"] = activeDialogue->id;
|
||||
out["currentNodeId"] = currentNodeId;
|
||||
}
|
||||
|
||||
void DialogueRuntime::load(const nlohmann::json& in)
|
||||
{
|
||||
stop();
|
||||
if (!in.value("active", false)) return;
|
||||
|
||||
const std::string dialogueId = in.value("dialogueId", "");
|
||||
const std::string nodeId = in.value("currentNodeId", "");
|
||||
if (dialogueId.empty() || nodeId.empty() || !database) return;
|
||||
|
||||
const DialogueDefinition* dialogue = database->findDialogue(dialogueId);
|
||||
if (!dialogue) {
|
||||
std::cerr << "[dialogue] load: dialogue '" << dialogueId << "' not found\n";
|
||||
return;
|
||||
}
|
||||
|
||||
activeDialogue = dialogue;
|
||||
currentNodeId.clear();
|
||||
visibleChoices.clear();
|
||||
selectedChoice = -1;
|
||||
revealCharacters = 0.0f;
|
||||
presentation = {};
|
||||
presentation.dialogueId = dialogue->id;
|
||||
|
||||
enterNode(nodeId);
|
||||
|
||||
// Snap text to fully revealed so the typing animation doesn't replay.
|
||||
presentation.visibleText = presentation.fullText;
|
||||
presentation.revealCompleted = true;
|
||||
revealCharacters = static_cast<float>(presentation.fullText.size());
|
||||
}
|
||||
|
||||
} // namespace ZL::Dialogue
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "dialogue/DialogueDatabase.h"
|
||||
#include "quest/QuestJournal.h"
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -40,6 +41,13 @@ public:
|
||||
void setGlobalFlagStore(std::unordered_map<std::string, int>* store);
|
||||
void setQuestJournal(Quest::QuestJournal* journal);
|
||||
|
||||
// Saves whether a dialogue is active, its id, and current node.
|
||||
// Cutscene state is not saved.
|
||||
void save(nlohmann::json& out) const;
|
||||
// Restores saved dialogue state. Text reveal snaps to completion so the
|
||||
// typing animation doesn't replay. No callbacks are fired on restore.
|
||||
void load(const nlohmann::json& in);
|
||||
|
||||
private:
|
||||
enum class Mode {
|
||||
Inactive,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include "dialogue/DialogueOverlay.h"
|
||||
#include "dialogue/DialogueRuntime.h"
|
||||
#include "cutscene/CutsceneDatabase.h"
|
||||
@@ -41,6 +42,9 @@ public:
|
||||
void setOnDialogueAdvanced(std::function<void()> cb);
|
||||
void stopDialogue();
|
||||
|
||||
void saveDialogueState(nlohmann::json& out) const { dialogueRuntime.save(out); }
|
||||
void loadDialogueState(const nlohmann::json& in) { dialogueRuntime.load(in); }
|
||||
|
||||
bool isActive() const { return dialogueRuntime.isActive() || cutsceneRuntime.isActive(); }
|
||||
bool blocksGameplayInput() const { return isActive(); }
|
||||
|
||||
|
||||
Reference in new issue
Block a user