2023-10-30 06:33:08 +08:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#include "level.h"
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
Level::Level(const QString &name) : mName(name) { }
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
QString Level::name() const
|
|
|
|
{
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<Character> Level::npcs() const
|
|
|
|
{
|
|
|
|
return mNpcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Level::setNpcs(const QList<Character> &npcs)
|
|
|
|
{
|
|
|
|
mNpcs = npcs;
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [fromJson]
|
|
|
|
Level Level::fromJson(const QJsonObject &json)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
Level result;
|
|
|
|
|
|
|
|
if (const QJsonValue v = json["name"]; v.isString())
|
|
|
|
result.mName = v.toString();
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
if (const QJsonValue v = json["npcs"]; v.isArray()) {
|
|
|
|
const QJsonArray npcs = v.toArray();
|
|
|
|
result.mNpcs.reserve(npcs.size());
|
|
|
|
for (const QJsonValue &npc : npcs)
|
|
|
|
result.mNpcs.append(Character::fromJson(npc.toObject()));
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
|
|
|
|
return result;
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [fromJson]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [toJson]
|
|
|
|
QJsonObject Level::toJson() const
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
QJsonObject json;
|
2023-10-30 06:33:08 +08:00
|
|
|
json["name"] = mName;
|
|
|
|
QJsonArray npcArray;
|
2023-11-02 01:02:52 +08:00
|
|
|
for (const Character &npc : mNpcs)
|
|
|
|
npcArray.append(npc.toJson());
|
2023-10-30 06:33:08 +08:00
|
|
|
json["npcs"] = npcArray;
|
2023-11-02 01:02:52 +08:00
|
|
|
return json;
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [toJson]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
void Level::print(QTextStream &s, int indentation) const
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
const QString indent(indentation * 2, ' ');
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
s << indent << "Name:\t" << mName << "\n" << indent << "NPCs:\n";
|
2023-10-30 06:33:08 +08:00
|
|
|
for (const Character &character : mNpcs)
|
2023-11-02 01:02:52 +08:00
|
|
|
character.print(s, indentation + 1);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|