make BlockMaterial serializable

This commit is contained in:
MihailRis 2025-04-13 20:52:03 +03:00
parent 862ac496a8
commit 9e28b783fd
4 changed files with 26 additions and 9 deletions

View File

@ -186,11 +186,7 @@ void ContentUnitLoader<DefT>::loadUnit(
void ContentLoader::loadBlockMaterial(
BlockMaterial& def, const io::path& file
) {
auto root = io::read_json(file);
root.at("steps-sound").get(def.stepsSound);
root.at("place-sound").get(def.placeSound);
root.at("break-sound").get(def.breakSound);
root.at("hit-sound").get(def.hitSound);
def.deserialize(io::read_json(file));
if (def.hitSound.empty()) {
def.hitSound = def.stepsSound;
}

View File

@ -243,7 +243,7 @@ void scripting::on_content_load(Content* content) {
const auto& materials = content->getBlockMaterials();
lua::createtable(L, 0, materials.size());
for (const auto& [name, material] : materials) {
lua::pushvalue(L, material->serialize());
lua::pushvalue(L, material->toTable());
lua::setfield(L, name);
}
lua::setfield(L, "materials");

View File

@ -8,7 +8,7 @@
#include "presets/ParticlesPreset.hpp"
#include "util/stringutil.hpp"
dv::value BlockMaterial::serialize() const {
dv::value BlockMaterial::toTable() const {
return dv::object({
{"name", name},
{"stepsSound", stepsSound},
@ -18,6 +18,24 @@ dv::value BlockMaterial::serialize() const {
});
}
dv::value BlockMaterial::serialize() const {
return dv::object({
{"name", name},
{"steps-sound", stepsSound},
{"place-sound", placeSound},
{"break-sound", breakSound},
{"hit-sound", hitSound}
});
}
void BlockMaterial::deserialize(const dv::value& src) {
src.at("name").get(name);
src.at("steps-sound").get(stepsSound);
src.at("place-sound").get(placeSound);
src.at("break-sound").get(breakSound);
src.at("hit-sound").get(hitSound);
}
CoordSystem::CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ)
: axes({axisX, axisY, axisZ}) {
fix = glm::ivec3(0);

View File

@ -10,6 +10,7 @@
#include "maths/aabb.hpp"
#include "typedefs.hpp"
#include "util/EnumMetadata.hpp"
#include "interfaces/Serializable.hpp"
struct ParticlesPreset;
@ -116,14 +117,16 @@ VC_ENUM_END
using BoxModel = AABB;
/// @brief Common kit of block properties applied to groups of blocks
struct BlockMaterial {
struct BlockMaterial : Serializable {
std::string name;
std::string stepsSound;
std::string placeSound;
std::string breakSound;
std::string hitSound;
dv::value serialize() const;
dv::value toTable() const; // for compatibility
dv::value serialize() const override;
void deserialize(const dv::value& src) override;
};
/// @brief Block properties definition