Content packs structure update

This commit is contained in:
MihailRis
2023-12-16 12:47:20 +03:00
parent 04f06a4717
commit 924957ddee
10 changed files with 131 additions and 86 deletions
+23 -46
View File
@@ -3,6 +3,7 @@
#include <iostream>
#include <string>
#include <memory>
#include <glm/glm.hpp>
#include "Content.h"
#include "../voxels/Block.h"
@@ -10,27 +11,21 @@
#include "../coders/json.h"
#include "../typedefs.h"
#include <glm/glm.hpp>
#include "ContentPack.h"
// don't ask
using glm::vec3;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::unique_ptr;
using std::filesystem::path;
namespace fs = std::filesystem;
ContentLoader::ContentLoader(path folder) : folder(folder) {}
ContentLoader::ContentLoader(const ContentPack* pack) : pack(pack) {
}
// TODO: add basic validation and logging
Block* ContentLoader::loadBlock(string name, path file) {
unique_ptr<json::JObject> root(files::read_json(file));
unique_ptr<Block> def(new Block(name));
Block* ContentLoader::loadBlock(std::string name, fs::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file));
std::unique_ptr<Block> def(new Block(name));
// block texturing
if (root->has("texture")) {
string texture;
std::string texture;
root->str("texture", texture);
for (uint i = 0; i < 6; i++)
def->textureFaces[i] = texture;
@@ -42,19 +37,19 @@ Block* ContentLoader::loadBlock(string name, path file) {
}
// block model
string model = "block";
std::string model = "block";
root->str("model", model);
if (model == "block") def->model = BlockModel::block;
else if (model == "aabb") def->model = BlockModel::aabb;
else if (model == "X") def->model = BlockModel::xsprite;
else if (model == "none") def->model = BlockModel::none;
else {
cerr << "unknown model " << model << endl;
std::cerr << "unknown model " << model << std::endl;
def->model = BlockModel::none;
}
// rotation profile
string profile = "none";
std::string profile = "none";
root->str("rotation", profile);
def->rotatable = profile != "none";
if (profile == "pipe") {
@@ -62,16 +57,16 @@ Block* ContentLoader::loadBlock(string name, path file) {
} else if (profile == "pane") {
def->rotations = BlockRotProfile::PANE;
} else if (profile != "none") {
cerr << "unknown rotation profile " << profile << endl;
std::cerr << "unknown rotation profile " << profile << std::endl;
def->rotatable = false;
}
// block hitbox AABB [x, y, z, width, height, depth]
json::JArray* hitboxobj = root->arr("hitbox");
if (hitboxobj) {
json::JArray* boxobj = root->arr("hitbox");
if (boxobj) {
AABB& aabb = def->hitbox;
aabb.a = vec3(hitboxobj->num(0), hitboxobj->num(1), hitboxobj->num(2));
aabb.b = vec3(hitboxobj->num(3), hitboxobj->num(4), hitboxobj->num(5));
aabb.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2));
aabb.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5));
aabb.b += aabb.a;
}
@@ -96,35 +91,17 @@ Block* ContentLoader::loadBlock(string name, path file) {
}
void ContentLoader::load(ContentBuilder* builder) {
cout << "-- loading content " << folder << endl;
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
path file = folder / path("package.json");
string source = files::read_string(file);
unique_ptr<json::JObject> root = nullptr;
try {
root.reset(json::parse(file.filename().string(), source));
} catch (const parsing_error& error) {
cerr << error.errorLog() << endl;
throw std::runtime_error("could not load content package");
}
string id;
string version;
root->str("id", id);
root->str("version", version);
cout << " id: " << id << endl;
cout << " version: " << version << endl;
auto folder = pack->folder;
auto root = files::read_json(pack->getContentFile());
json::JArray* blocksarr = root->arr("blocks");
if (blocksarr) {
cout << " blocks: " << blocksarr->size() << endl;
for (uint i = 0; i < blocksarr->size(); i++) {
string name = blocksarr->str(i);
cout << " loading block " << id << ":" << name << endl;
path blockfile = folder/path("blocks/"+name+".json");
builder->add(loadBlock(id+":"+name, blockfile));
std::string name = blocksarr->str(i);
fs::path blockfile = folder/fs::path("blocks/"+name+".json");
builder->add(loadBlock(pack->id+":"+name, blockfile));
}
}
}