Working on quest system
This commit is contained in:
1 parent
aaae368a07
commit
e7ae148ead
22 files changed
+357
-103
No files matched your search
+26
-25
@@ -12,38 +12,19 @@ const char* toString(QuestStatus status) {
|
||||
switch (status) {
|
||||
case QuestStatus::Hidden: return "Hidden";
|
||||
case QuestStatus::Available: return "Available";
|
||||
case QuestStatus::Active: return "Active";
|
||||
case QuestStatus::Completed: return "Completed";
|
||||
case QuestStatus::Failed: return "Failed";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char* toString(QuestCategory category) {
|
||||
switch (category) {
|
||||
case QuestCategory::Main: return "Main";
|
||||
case QuestCategory::Side: return "Side";
|
||||
case QuestCategory::Contract: return "Contract";
|
||||
case QuestCategory::Tutorial: return "Tutorial";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static QuestStatus parseQuestStatus(const std::string& value) {
|
||||
if (value == "Available") return QuestStatus::Available;
|
||||
if (value == "Active") return QuestStatus::Active;
|
||||
if (value == "Completed") return QuestStatus::Completed;
|
||||
if (value == "Failed") return QuestStatus::Failed;
|
||||
return QuestStatus::Hidden;
|
||||
}
|
||||
|
||||
static QuestCategory parseQuestCategory(const std::string& value) {
|
||||
if (value == "Main") return QuestCategory::Main;
|
||||
if (value == "Contract") return QuestCategory::Contract;
|
||||
if (value == "Tutorial") return QuestCategory::Tutorial;
|
||||
return QuestCategory::Side;
|
||||
}
|
||||
|
||||
bool QuestJournal::loadFromFile(const std::string& path, const std::string& zipFile) {
|
||||
quests.clear();
|
||||
questOrder.clear();
|
||||
@@ -87,9 +68,8 @@ bool QuestJournal::loadFromFile(const std::string& path, const std::string& zipF
|
||||
def.id = item.value("id", "");
|
||||
def.title = item.value("title", def.id);
|
||||
def.description = item.value("description", "");
|
||||
def.category = parseQuestCategory(item.value("category", "Side"));
|
||||
def.initialStatus = parseQuestStatus(item.value("status", "Hidden"));
|
||||
def.recommendedLevel = item.value("recommendedLevel", 0);
|
||||
def.autoComplete = item.value("autoComplete", false);
|
||||
|
||||
if (item.contains("objectives") && item["objectives"].is_array()) {
|
||||
for (const auto& obj : item["objectives"]) {
|
||||
@@ -97,6 +77,7 @@ bool QuestJournal::loadFromFile(const std::string& path, const std::string& zipF
|
||||
objective.id = obj.value("id", "");
|
||||
objective.text = obj.value("text", "");
|
||||
objective.completed = obj.value("completed", false);
|
||||
objective.visible = obj.value("visible", true);
|
||||
if (!objective.id.empty()) {
|
||||
def.objectives.push_back(objective);
|
||||
}
|
||||
@@ -136,10 +117,6 @@ bool QuestJournal::unlockQuest(const std::string& questId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QuestJournal::startQuest(const std::string& questId) {
|
||||
return setStatus(questId, QuestStatus::Active);
|
||||
}
|
||||
|
||||
bool QuestJournal::completeQuest(const std::string& questId) {
|
||||
return setStatus(questId, QuestStatus::Completed);
|
||||
}
|
||||
@@ -155,6 +132,30 @@ bool QuestJournal::setObjectiveCompleted(const std::string& questId, const std::
|
||||
for (auto& objective : quest->definition.objectives) {
|
||||
if (objective.id == objectiveId) {
|
||||
objective.completed = completed;
|
||||
|
||||
if (completed && quest->definition.autoComplete && quest->status == QuestStatus::Available) {
|
||||
const bool allDone = std::all_of(
|
||||
quest->definition.objectives.begin(),
|
||||
quest->definition.objectives.end(),
|
||||
[](const QuestObjective& o) { return o.completed; });
|
||||
if (allDone)
|
||||
quest->status = QuestStatus::Completed;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool QuestJournal::setObjectiveVisible(const std::string& questId, const std::string& objectiveId, bool visible) {
|
||||
QuestState* quest = findQuest(questId);
|
||||
if (!quest) return false;
|
||||
|
||||
for (auto& objective : quest->definition.objectives) {
|
||||
if (objective.id == objectiveId) {
|
||||
objective.visible = visible;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ public:
|
||||
bool loadFromFile(const std::string& path, const std::string& zipFile = "");
|
||||
|
||||
bool unlockQuest(const std::string& questId);
|
||||
bool startQuest(const std::string& questId);
|
||||
bool completeQuest(const std::string& questId);
|
||||
bool failQuest(const std::string& questId);
|
||||
|
||||
bool setObjectiveCompleted(const std::string& questId, const std::string& objectiveId, bool completed = true);
|
||||
bool setObjectiveVisible(const std::string& questId, const std::string& objectiveId, bool visible = true);
|
||||
bool setActiveObjective(const std::string& questId, int objectiveIndex);
|
||||
|
||||
QuestState* findQuest(const std::string& questId);
|
||||
|
||||
+2
-11
@@ -8,31 +8,23 @@ namespace ZL::Quest {
|
||||
enum class QuestStatus {
|
||||
Hidden,
|
||||
Available,
|
||||
Active,
|
||||
Completed,
|
||||
Failed
|
||||
};
|
||||
|
||||
enum class QuestCategory {
|
||||
Main,
|
||||
Side,
|
||||
Contract,
|
||||
Tutorial
|
||||
};
|
||||
|
||||
struct QuestObjective {
|
||||
std::string id;
|
||||
std::string text;
|
||||
bool completed = false;
|
||||
bool visible = true;
|
||||
};
|
||||
|
||||
struct QuestDefinition {
|
||||
std::string id;
|
||||
std::string title;
|
||||
std::string description;
|
||||
QuestCategory category = QuestCategory::Side;
|
||||
QuestStatus initialStatus = QuestStatus::Hidden;
|
||||
int recommendedLevel = 0;
|
||||
bool autoComplete = false;
|
||||
std::vector<QuestObjective> objectives;
|
||||
};
|
||||
|
||||
@@ -44,6 +36,5 @@ struct QuestState {
|
||||
};
|
||||
|
||||
const char* toString(QuestStatus status);
|
||||
const char* toString(QuestCategory category);
|
||||
|
||||
} // namespace ZL::Quest
|
||||
Reference in new issue
Block a user