block materials loading

This commit is contained in:
MihailRis
2024-03-09 03:36:08 +03:00
parent 90f3373f46
commit f4cc413f61
11 changed files with 144 additions and 40 deletions
+25 -2
View File
@@ -28,6 +28,10 @@ void ContentBuilder::add(ContentPackRuntime* pack) {
packs.emplace(pack->getId(), pack);
}
void ContentBuilder::add(BlockMaterial material) {
blockMaterials.emplace(material.name, material);
}
Block& ContentBuilder::createBlock(std::string id) {
auto found = blockDefs.find(id);
if (found != blockDefs.end()) {
@@ -109,7 +113,12 @@ Content* ContentBuilder::build() {
auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices);
auto content = std::make_unique<Content>(
indices, std::move(groups), blockDefs, itemDefs, std::move(packs)
indices,
std::move(groups),
blockDefs,
itemDefs,
std::move(packs),
std::move(blockMaterials)
);
// Now, it's time to resolve foreign keys
@@ -136,11 +145,13 @@ Content::Content(
std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs,
std::unordered_map<std::string, BlockMaterial> blockMaterials
) : blockDefs(blockDefs),
itemDefs(itemDefs),
indices(indices),
packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)),
drawGroups(std::move(drawGroups))
{}
@@ -178,6 +189,14 @@ ItemDef& Content::requireItem(std::string id) const {
return *found->second;
}
const BlockMaterial* Content::findBlockMaterial(std::string id) const {
auto found = blockMaterials.find(id);
if (found == blockMaterials.end()) {
return nullptr;
}
return &found->second;
}
const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
auto found = packs.find(id);
if (found == packs.end()) {
@@ -186,6 +205,10 @@ const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
return found->second.get();
}
const std::unordered_map<std::string, BlockMaterial>& Content::getBlockMaterials() const {
return blockMaterials;
}
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const {
return packs;
}
+10 -2
View File
@@ -8,10 +8,10 @@
#include <unordered_map>
#include <set>
#include "../typedefs.h"
#include "../voxels/Block.h"
using DrawGroups = std::set<ubyte>;
class Block;
class ItemDef;
class Content;
class ContentPackRuntime;
@@ -48,6 +48,8 @@ class ContentBuilder {
std::unordered_map<std::string, ItemDef*> itemDefs;
std::vector<std::string> itemIds;
std::unordered_map<std::string, BlockMaterial> blockMaterials;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
public:
~ContentBuilder();
@@ -55,6 +57,7 @@ public:
void add(Block* def);
void add(ItemDef* def);
void add(ContentPackRuntime* pack);
void add(BlockMaterial material);
Block& createBlock(std::string id);
ItemDef& createItem(std::string id);
@@ -111,6 +114,7 @@ class Content {
std::unordered_map<std::string, ItemDef*> itemDefs;
std::unique_ptr<ContentIndices> indices;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
std::unordered_map<std::string, BlockMaterial> blockMaterials;
public:
std::unique_ptr<DrawGroups> const drawGroups;
@@ -119,7 +123,8 @@ public:
std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs,
std::unordered_map<std::string, BlockMaterial> blockMaterials
);
~Content();
@@ -133,8 +138,11 @@ public:
ItemDef* findItem(std::string id) const;
ItemDef& requireItem(std::string id) const;
const BlockMaterial* findBlockMaterial(std::string id) const;
const ContentPackRuntime* getPackRuntime(std::string id) const;
const std::unordered_map<std::string, BlockMaterial>& getBlockMaterials() const;
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& getPacks() const;
};
+18
View File
@@ -303,6 +303,15 @@ void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) {
}
}
BlockMaterial ContentLoader::loadBlockMaterial(fs::path file, std::string full) {
auto root = files::read_json(file);
BlockMaterial material {full};
root->str("steps-sound", material.stepsSound);
root->str("place-sound", material.placeSound);
root->str("break-sound", material.breakSound);
return material;
}
void ContentLoader::load(ContentBuilder& builder) {
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
@@ -363,4 +372,13 @@ void ContentLoader::load(ContentBuilder& builder) {
stats.totalItems++;
}
}
fs::path materialsDir = folder / fs::u8path("block_materials");
if (fs::is_directory(materialsDir)) {
for (auto entry : fs::directory_iterator(materialsDir)) {
fs::path file = entry.path();
std::string name = pack->id+":"+file.stem().u8string();
builder.add(loadBlockMaterial(file, name));
}
}
}
+8 -4
View File
@@ -1,12 +1,13 @@
#ifndef CONTENT_CONTENT_LOADER_H_
#define CONTENT_CONTENT_LOADER_H_
#include "../voxels/Block.h"
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
class Block;
class ItemDef;
struct ContentPack;
class ContentBuilder;
@@ -22,12 +23,15 @@ class ContentLoader {
void loadBlock(Block& def, std::string full, std::string name);
void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
void loadItem(ItemDef& def, std::string full, std::string name);
BlockMaterial loadBlockMaterial(fs::path file, std::string full);
public:
ContentLoader(ContentPack* pack);
bool fixPackIndices(std::filesystem::path folder,
dynamic::Map* indicesRoot,
std::string contentSection);
bool fixPackIndices(
fs::path folder,
dynamic::Map* indicesRoot,
std::string contentSection
);
void fixPackIndices();
void loadBlock(Block& def, std::string name, fs::path file);
void loadItem(ItemDef& def, std::string name, fs::path file);