character.h
#include <QJsonObject>
class Character {
public:
enum ClassType {
Warrior, Mage, Archer
};
Character();
Character(const QString &name, int level, ClassType classType);
QString name() const;
void setName(const QString &name);
int level() const;
void setLevel(int level);
ClassType classType() const;
void setClassType(ClassType classType);
void read(const QJsonObject &json);
void write(QJsonObject &json) const;
private:
QString mName;
int mLevel;
ClassType mClassType;
};
character.cpp
#include "character.h"
Character::Character() :
mLevel(0),
mClassType(Warrior) {
}
Character::Character(const QString &name, int level, Character::ClassType classType) :
mName(name),
mLevel(level),
mClassType(classType)
{
}
QString Character::name() const
{
return mName;
}
void Character::setName(const QString &name)
{
mName = name;
}
int Character::level() const
{
return mLevel;
}
void Character::setLevel(int level)
{
mLevel = level;
}
Character::ClassType Character::classType() const
{
return mClassType;
}
void Character::setClassType(Character::ClassType classType)
{
mClassType = classType;
}
void Character::read(const QJsonObject &json)
{
mName = json["name"].toString(); mLevel = json["level"].toDouble(); mClassType = ClassType(qRound(json["classType"].toDouble())); } void Character::write(QJsonObject &json) const { json["name"] = mName; json["level"] = mLevel; json["classType"] = mClassType; }
game.h
#include <QJsonObject>
#include "character.h"
#include "level.h"
class Game {
public:
Game();
enum SaveFormat {
Json, Binary
};
const Character &player() const;
const QList<Level> &levels() const;
void newGame();
bool loadGame(SaveFormat saveFormat);
bool saveGame(SaveFormat saveFormat) const;
void read(const QJsonObject &json);
void write(QJsonObject &json) const;
private:
Character mPlayer;
QList<Level> mLevels;
};
game.cpp
#include "game.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
Game::Game()
{
}
const Character &Game::player() const
{
return mPlayer;
}
const QList<Level> &Game::levels() const {
return mLevels;
}
void Game::newGame() {
mPlayer = Character();
mPlayer.setName(QStringLiteral("Hero"));
mPlayer.setClassType(Character::Archer);
mPlayer.setLevel(15);
mLevels.clear();
Level village;
QList<Character> villageNpcs;
villageNpcs.append(Character(QStringLiteral("Barry the Blacksmith"), 10, Character::Warrior));
villageNpcs.append(Character(QStringLiteral("Terry the Trader"), 10, Character::Warrior));
village.setNpcs(villageNpcs);
mLevels.append(village);
Level dungeon;
QList<Character> dungeonNpcs;
dungeonNpcs.append(Character(QStringLiteral("Eric the Evil"), 20, Character::Mage));
dungeonNpcs.append(Character(QStringLiteral("Eric's Sidekick #1"), 5, Character::Warrior));
dungeonNpcs.append(Character(QStringLiteral("Eric's Sidekick #2"), 5, Character::Warrior));
dungeon.setNpcs(dungeonNpcs);
mLevels.append(dungeon);
}
bool Game::loadGame(Game::SaveFormat saveFormat)
{
QFile loadFile(saveFormat == Json
? QStringLiteral("save.json")
: QStringLiteral("save.dat"));
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc(saveFormat == Json
? QJsonDocument::fromJson(saveData)
: QJsonDocument::fromBinaryData(saveData));
read(loadDoc.object());
return true;
}
bool Game::saveGame(Game::SaveFormat saveFormat) const
{
QFile saveFile(saveFormat == Json
? QStringLiteral("save.json")
: QStringLiteral("save.dat"));
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonObject gameObject;
write(gameObject);
QJsonDocument saveDoc(gameObject);
saveFile.write(saveFormat == Json
? saveDoc.toJson()
: saveDoc.toBinaryData());
return true;
}
void Game::read(const QJsonObject &json)
{
mPlayer.read(json["player"].toObject()); mLevels.clear(); QJsonArray levelArray = json["levels"].toArray(); for (int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex) { QJsonObject levelObject = levelArray[levelIndex].toObject(); Level level; level.read(levelObject); mLevels.append(level); } } void Game::write(QJsonObject &json) const { QJsonObject playerObject; mPlayer.write(playerObject); json["player"] = playerObject; QJsonArray levelArray; foreach (const Level level, mLevels) { QJsonObject levelObject; level.write(levelObject); levelArray.append(levelObject); } json["levels"] = levelArray; }
level.h
#include <QJsonObject>
#include <QList>
#include "character.h"
class Level {
public:
Level();
const QList<Character> &npcs() const;
void setNpcs(const QList<Character> &npcs);
void read(const QJsonObject &json);
void write(QJsonObject &json) const;
private:
QList<Character> mNpcs;
};
level.cpp
#include "level.h"
#include <QJsonArray>
Level::Level()
{
}
const QList<Character> &Level::npcs() const
{
return mNpcs;
}
void Level::setNpcs(const QList<Character> &npcs)
{
mNpcs = npcs;
}
void Level::read(const QJsonObject &json)
{
mNpcs.clear();
QJsonArray npcArray = json["npcs"].toArray();
for (int npcIndex = 0; npcIndex < npcArray.size(); ++npcIndex) {
QJsonObject npcObject = npcArray[npcIndex].toObject();
Character npc;
npc.read(npcObject);
mNpcs.append(npc);
}
}
void Level::write(QJsonObject &json) const
{
QJsonArray npcArray;
foreach (const Character npc, mNpcs) {
QJsonObject npcObject;
npc.write(npcObject);
npcArray.append(npcObject);
}
json["npcs"] = npcArray;
}
main.cpp
#include <QCoreApplication>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include <QJsonArray>
#include "game.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Game game;
game.newGame();
// Game is played; changes are made...
if (!game.saveGame(Game::Json))
return 1;
if (!game.saveGame(Game::Binary))
return 1;
Game fromJsonGame;
if (!fromJsonGame.loadGame(Game::Json))
return 1;
Game fromBinaryGame;
if (!fromBinaryGame.loadGame(Game::Binary))
return 1;
return a.exec();
}