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 "game.h"
|
|
|
|
|
|
|
|
#include <QCborMap>
|
|
|
|
#include <QCborValue>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2023-10-30 06:33:08 +08:00
|
|
|
Character Game::player() const
|
|
|
|
{
|
|
|
|
return mPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<Level> Game::levels() const
|
|
|
|
{
|
|
|
|
return mLevels;
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [newGame]
|
2023-10-30 06:33:08 +08:00
|
|
|
void Game::newGame()
|
|
|
|
{
|
|
|
|
mPlayer = Character();
|
2023-11-02 01:02:52 +08:00
|
|
|
mPlayer.setName("Hero"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
mPlayer.setClassType(Character::Archer);
|
|
|
|
mPlayer.setLevel(QRandomGenerator::global()->bounded(15, 21));
|
|
|
|
|
|
|
|
mLevels.clear();
|
|
|
|
mLevels.reserve(2);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
Level village("Village"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
QList<Character> villageNpcs;
|
|
|
|
villageNpcs.reserve(2);
|
2023-11-02 01:02:52 +08:00
|
|
|
villageNpcs.append(Character("Barry the Blacksmith"_L1,
|
|
|
|
QRandomGenerator::global()->bounded(8, 11), Character::Warrior));
|
|
|
|
villageNpcs.append(Character("Terry the Trader"_L1,
|
|
|
|
QRandomGenerator::global()->bounded(6, 8), Character::Warrior));
|
2023-10-30 06:33:08 +08:00
|
|
|
village.setNpcs(villageNpcs);
|
|
|
|
mLevels.append(village);
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
Level dungeon("Dungeon"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
QList<Character> dungeonNpcs;
|
|
|
|
dungeonNpcs.reserve(3);
|
2023-11-02 01:02:52 +08:00
|
|
|
dungeonNpcs.append(Character("Eric the Evil"_L1,
|
|
|
|
QRandomGenerator::global()->bounded(18, 26), Character::Mage));
|
|
|
|
dungeonNpcs.append(Character("Eric's Left Minion"_L1,
|
|
|
|
QRandomGenerator::global()->bounded(5, 7), Character::Warrior));
|
|
|
|
dungeonNpcs.append(Character("Eric's Right Minion"_L1,
|
|
|
|
QRandomGenerator::global()->bounded(4, 9), Character::Warrior));
|
2023-10-30 06:33:08 +08:00
|
|
|
dungeon.setNpcs(dungeonNpcs);
|
|
|
|
mLevels.append(dungeon);
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [newGame]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [loadGame]
|
2023-10-30 06:33:08 +08:00
|
|
|
bool Game::loadGame(Game::SaveFormat saveFormat)
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
QFile loadFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
if (!loadFile.open(QIODevice::ReadOnly)) {
|
|
|
|
qWarning("Couldn't open save file.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray saveData = loadFile.readAll();
|
|
|
|
|
|
|
|
QJsonDocument loadDoc(saveFormat == Json
|
2023-11-02 01:02:52 +08:00
|
|
|
? QJsonDocument::fromJson(saveData)
|
|
|
|
: QJsonDocument(QCborValue::fromCbor(saveData).toMap().toJsonObject()));
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
read(loadDoc.object());
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
QTextStream(stdout) << "Loaded save for " << loadDoc["player"]["name"].toString()
|
|
|
|
<< " using " << (saveFormat != Json ? "CBOR" : "JSON") << "...\n";
|
2023-10-30 06:33:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [loadGame]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [saveGame]
|
2023-10-30 06:33:08 +08:00
|
|
|
bool Game::saveGame(Game::SaveFormat saveFormat) const
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
QFile saveFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
if (!saveFile.open(QIODevice::WriteOnly)) {
|
|
|
|
qWarning("Couldn't open save file.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
QJsonObject gameObject = toJson();
|
|
|
|
saveFile.write(saveFormat == Json ? QJsonDocument(gameObject).toJson()
|
|
|
|
: QCborValue::fromJsonValue(gameObject).toCbor());
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [saveGame]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [read]
|
2023-10-30 06:33:08 +08:00
|
|
|
void Game::read(const QJsonObject &json)
|
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
if (const QJsonValue v = json["player"]; v.isObject())
|
|
|
|
mPlayer = Character::fromJson(v.toObject());
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
if (const QJsonValue v = json["levels"]; v.isArray()) {
|
|
|
|
const QJsonArray levels = v.toArray();
|
2023-10-30 06:33:08 +08:00
|
|
|
mLevels.clear();
|
2023-11-02 01:02:52 +08:00
|
|
|
mLevels.reserve(levels.size());
|
|
|
|
for (const QJsonValue &level : levels)
|
|
|
|
mLevels.append(Level::fromJson(level.toObject()));
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [read]
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
//! [toJson]
|
|
|
|
QJsonObject Game::toJson() const
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 01:02:52 +08:00
|
|
|
QJsonObject json;
|
|
|
|
json["player"] = mPlayer.toJson();
|
|
|
|
|
|
|
|
QJsonArray levels;
|
|
|
|
for (const Level &level : mLevels)
|
|
|
|
levels.append(level.toJson());
|
|
|
|
json["levels"] = levels;
|
|
|
|
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 Game::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 << "Player\n";
|
|
|
|
mPlayer.print(s, indentation + 1);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 01:02:52 +08:00
|
|
|
s << indent << "Levels\n";
|
2023-10-30 06:33:08 +08:00
|
|
|
for (const Level &level : mLevels)
|
2023-11-02 01:02:52 +08:00
|
|
|
level.print(s, indentation + 1);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|