2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2013-08-19 14:29:29 +00:00
|
|
|
|
|
|
|
#include "level.h"
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
2017-08-22 07:52:55 +00:00
|
|
|
#include <QTextStream>
|
2013-08-19 14:29:29 +00:00
|
|
|
|
2017-08-22 07:52:55 +00:00
|
|
|
Level::Level(const QString &name) : mName(name)
|
|
|
|
{
|
2013-08-19 14:29:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:52:55 +00:00
|
|
|
QString Level::name() const
|
|
|
|
{
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2020-06-22 08:12:38 +00:00
|
|
|
QList<Character> Level::npcs() const
|
2013-08-19 14:29:29 +00:00
|
|
|
{
|
|
|
|
return mNpcs;
|
|
|
|
}
|
|
|
|
|
2020-06-22 08:12:38 +00:00
|
|
|
void Level::setNpcs(const QList<Character> &npcs)
|
2013-08-19 14:29:29 +00:00
|
|
|
{
|
|
|
|
mNpcs = npcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
void Level::read(const QJsonObject &json)
|
|
|
|
{
|
2017-08-22 07:52:55 +00:00
|
|
|
if (json.contains("name") && json["name"].isString())
|
|
|
|
mName = json["name"].toString();
|
|
|
|
|
|
|
|
if (json.contains("npcs") && json["npcs"].isArray()) {
|
|
|
|
QJsonArray npcArray = json["npcs"].toArray();
|
|
|
|
mNpcs.clear();
|
|
|
|
mNpcs.reserve(npcArray.size());
|
2023-02-07 12:52:50 +00:00
|
|
|
for (const QJsonValue &v : npcArray) {
|
|
|
|
QJsonObject npcObject = v.toObject();
|
2017-08-22 07:52:55 +00:00
|
|
|
Character npc;
|
|
|
|
npc.read(npcObject);
|
|
|
|
mNpcs.append(npc);
|
|
|
|
}
|
2013-08-19 14:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
void Level::write(QJsonObject &json) const
|
|
|
|
{
|
2017-08-22 07:52:55 +00:00
|
|
|
json["name"] = mName;
|
2013-08-19 14:29:29 +00:00
|
|
|
QJsonArray npcArray;
|
2019-02-03 16:09:10 +00:00
|
|
|
for (const Character &npc : mNpcs) {
|
2013-08-19 14:29:29 +00:00
|
|
|
QJsonObject npcObject;
|
|
|
|
npc.write(npcObject);
|
|
|
|
npcArray.append(npcObject);
|
|
|
|
}
|
|
|
|
json["npcs"] = npcArray;
|
|
|
|
}
|
|
|
|
//! [1]
|
2017-08-22 07:52:55 +00:00
|
|
|
|
|
|
|
void Level::print(int indentation) const
|
|
|
|
{
|
|
|
|
const QString indent(indentation * 2, ' ');
|
|
|
|
QTextStream(stdout) << indent << "Name:\t" << mName << "\n";
|
|
|
|
|
|
|
|
QTextStream(stdout) << indent << "NPCs:\n";
|
|
|
|
for (const Character &character : mNpcs)
|
|
|
|
character.print(2);
|
|
|
|
}
|