Merge pull request #134 from DanielProl1xy/serializable
Added Serializable Class
This commit is contained in:
commit
f587cd586f
@ -536,7 +536,7 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
}
|
||||
|
||||
void WorldFiles::writePlayer(Player* player) {
|
||||
files::write_json(getPlayerFile(), player->write().release());
|
||||
files::write_json(getPlayerFile(), player->serialize().release());
|
||||
}
|
||||
|
||||
bool WorldFiles::readPlayer(Player* player) {
|
||||
@ -546,37 +546,7 @@ bool WorldFiles::readPlayer(Player* player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto root = files::read_json(file);
|
||||
auto posarr = root->list("position");
|
||||
glm::vec3& position = player->hitbox->position;
|
||||
position.x = posarr->num(0);
|
||||
position.y = posarr->num(1);
|
||||
position.z = posarr->num(2);
|
||||
player->camera->position = position;
|
||||
|
||||
auto rotarr = root->list("rotation");
|
||||
player->cam.x = rotarr->num(0);
|
||||
player->cam.y = rotarr->num(1);
|
||||
|
||||
if (root->has("spawnpoint")) {
|
||||
auto sparr = root->list("spawnpoint");
|
||||
player->setSpawnPoint(glm::vec3(
|
||||
sparr->num(0),
|
||||
sparr->num(1),
|
||||
sparr->num(2)
|
||||
));
|
||||
} else {
|
||||
player->setSpawnPoint(position);
|
||||
}
|
||||
|
||||
root->flag("flight", player->flight);
|
||||
root->flag("noclip", player->noclip);
|
||||
player->setChosenSlot(root->getInt("chosen-slot", player->getChosenSlot()));
|
||||
|
||||
auto invmap = root->map("inventory");
|
||||
if (invmap) {
|
||||
player->getInventory()->read(invmap);
|
||||
}
|
||||
player->deserialize(files::read_json(file).get());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
15
src/interfaces/Serializable.h
Normal file
15
src/interfaces/Serializable.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef SERIALIZABLE_H
|
||||
#define SERIALIZABLE_H
|
||||
|
||||
#include <memory>
|
||||
#include "../coders/json.h"
|
||||
|
||||
class Serializable
|
||||
{
|
||||
public:
|
||||
virtual ~Serializable() { }
|
||||
virtual std::unique_ptr<dynamic::Map> serialize() const = 0;
|
||||
virtual void deserialize(dynamic::Map* src) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -44,7 +44,7 @@ void Inventory::move(
|
||||
}
|
||||
}
|
||||
|
||||
void Inventory::read(const dynamic::Map* src) {
|
||||
void Inventory::deserialize(dynamic::Map* src) {
|
||||
auto slotsarr = src->list("slots");
|
||||
size_t slotscount = std::min(slotsarr->size(), slots.size());
|
||||
for (size_t i = 0; i < slotscount; i++) {
|
||||
@ -56,7 +56,7 @@ void Inventory::read(const dynamic::Map* src) {
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> Inventory::write() const {
|
||||
std::unique_ptr<dynamic::Map> Inventory::serialize() const {
|
||||
auto map = std::make_unique<dynamic::Map>();
|
||||
auto& slotsarr = map->putList("slots");
|
||||
for (size_t i = 0; i < slots.size(); i++) {
|
||||
|
||||
@ -8,12 +8,13 @@
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../data/dynamic.h"
|
||||
#include "../interfaces/Serializable.h"
|
||||
|
||||
class ContentLUT;
|
||||
class ContentIndices;
|
||||
|
||||
// TODO: items indices fix
|
||||
class Inventory {
|
||||
class Inventory : Serializable {
|
||||
std::vector<ItemStack> slots;
|
||||
public:
|
||||
Inventory(size_t size);
|
||||
@ -33,9 +34,9 @@ public:
|
||||
size_t end=-1);
|
||||
|
||||
/* deserializing inventory */
|
||||
void read(const dynamic::Map* src);
|
||||
void deserialize(dynamic::Map* src) override;
|
||||
/* serializing inventory */
|
||||
std::unique_ptr<dynamic::Map> write() const;
|
||||
std::unique_ptr<dynamic::Map> serialize() const override;
|
||||
|
||||
static void convert(dynamic::Map* data, const ContentLUT* lut);
|
||||
|
||||
|
||||
@ -170,7 +170,7 @@ glm::vec3 Player::getSpawnPoint() const {
|
||||
return spawnpoint;
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> Player::write() const {
|
||||
std::unique_ptr<dynamic::Map> Player::serialize() const {
|
||||
glm::vec3 position = hitbox->position;
|
||||
auto root = std::make_unique<dynamic::Map>();
|
||||
auto& posarr = root->putList("position");
|
||||
@ -190,10 +190,45 @@ std::unique_ptr<dynamic::Map> Player::write() const {
|
||||
root->put("flight", flight);
|
||||
root->put("noclip", noclip);
|
||||
root->put("chosen-slot", chosenSlot);
|
||||
root->put("inventory", inventory->write().release());
|
||||
root->put("inventory", inventory->serialize().release());
|
||||
return root;
|
||||
}
|
||||
|
||||
void Player::deserialize(dynamic::Map *src) {
|
||||
|
||||
auto posarr = src->list("position");
|
||||
glm::vec3& position = hitbox->position;
|
||||
position.x = posarr->num(0);
|
||||
position.y = posarr->num(1);
|
||||
position.z = posarr->num(2);
|
||||
camera->position = position;
|
||||
|
||||
auto rotarr = src->list("rotation");
|
||||
cam.x = rotarr->num(0);
|
||||
cam.y = rotarr->num(1);
|
||||
|
||||
if (src->has("spawnpoint")) {
|
||||
auto sparr = src->list("spawnpoint");
|
||||
setSpawnPoint(glm::vec3(
|
||||
sparr->num(0),
|
||||
sparr->num(1),
|
||||
sparr->num(2)
|
||||
));
|
||||
} else {
|
||||
setSpawnPoint(position);
|
||||
}
|
||||
|
||||
src->flag("flight", flight);
|
||||
src->flag("noclip", noclip);
|
||||
setChosenSlot(src->getInt("chosen-slot", getChosenSlot()));
|
||||
|
||||
auto invmap = src->map("inventory");
|
||||
if (invmap) {
|
||||
getInventory()->deserialize(invmap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
|
||||
auto inventory = data->map("inventory");
|
||||
if (inventory) {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include "../data/dynamic.h"
|
||||
#include "../voxels/voxel.h"
|
||||
#include "../settings.h"
|
||||
#include "../interfaces/Serializable.h"
|
||||
|
||||
class Camera;
|
||||
class Hitbox;
|
||||
@ -31,7 +32,7 @@ struct PlayerInput {
|
||||
bool flight;
|
||||
};
|
||||
|
||||
class Player {
|
||||
class Player : Serializable {
|
||||
float speed;
|
||||
int chosenSlot;
|
||||
glm::vec3 spawnpoint {};
|
||||
@ -65,7 +66,8 @@ public:
|
||||
void setSpawnPoint(glm::vec3 point);
|
||||
glm::vec3 getSpawnPoint() const;
|
||||
|
||||
std::unique_ptr<dynamic::Map> write() const;
|
||||
std::unique_ptr<dynamic::Map> serialize() const override;
|
||||
void deserialize(dynamic::Map *src) override;
|
||||
|
||||
static void convert(dynamic::Map* data, const ContentLUT* lut);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user