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
@@ -0,0 +1,3 @@
{
"steps-sound": "steps/grass"
}
@@ -0,0 +1,3 @@
{
"steps-sound": "steps/stone"
}
@@ -0,0 +1,3 @@
{
"steps-sound": "steps/wood"
}
+14 -6
View File
@@ -61,6 +61,14 @@ void addLayouts(int env, const std::string& prefix, const fs::path& folder, Asse
} }
} }
void AssetsLoader::tryAddSound(std::string name) {
if (name.empty()) {
return;
}
fs::path file = SOUNDS_FOLDER"/"+name+".ogg";
add(ASSET_SOUND, file, name);
}
void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) { void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal"); loader.add(ASSET_FONT, FONTS_FOLDER"/font", "normal");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui", "ui"); loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui", "ui");
@@ -81,12 +89,12 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/sun.png", "misc/sun"); loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/sun.png", "misc/sun");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/crosshair.png", "gui/crosshair"); loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/crosshair.png", "gui/crosshair");
// (test code) for (auto& entry : content->getBlockMaterials()) {
// TODO: remove auto& material = entry.second;
loader.add(ASSET_SOUND, SOUNDS_FOLDER"/steps/grass.ogg", "steps/grass"); loader.tryAddSound(material.stepsSound);
loader.add(ASSET_SOUND, SOUNDS_FOLDER"/steps/stone.ogg", "steps/stone"); loader.tryAddSound(material.placeSound);
loader.add(ASSET_SOUND, SOUNDS_FOLDER"/steps/wood.ogg", "steps/wood"); loader.tryAddSound(material.breakSound);
loader.add(ASSET_SOUND, SOUNDS_FOLDER"/steps/ground.ogg", "steps/ground"); }
addLayouts(0, "core", loader.getPaths()->getMainRoot()/fs::path("layouts"), loader); addLayouts(0, "core", loader.getPaths()->getMainRoot()/fs::path("layouts"), loader);
for (auto& entry : content->getPacks()) { for (auto& entry : content->getPacks()) {
+2
View File
@@ -49,6 +49,8 @@ class AssetsLoader {
std::map<int, aloader_func> loaders; std::map<int, aloader_func> loaders;
std::queue<aloader_entry> entries; std::queue<aloader_entry> entries;
const ResPaths* paths; const ResPaths* paths;
void tryAddSound(std::string name);
public: public:
AssetsLoader(Assets* assets, const ResPaths* paths); AssetsLoader(Assets* assets, const ResPaths* paths);
void addLoader(int tag, aloader_func func); void addLoader(int tag, aloader_func func);
+25 -2
View File
@@ -28,6 +28,10 @@ void ContentBuilder::add(ContentPackRuntime* pack) {
packs.emplace(pack->getId(), pack); packs.emplace(pack->getId(), pack);
} }
void ContentBuilder::add(BlockMaterial material) {
blockMaterials.emplace(material.name, material);
}
Block& ContentBuilder::createBlock(std::string id) { Block& ContentBuilder::createBlock(std::string id) {
auto found = blockDefs.find(id); auto found = blockDefs.find(id);
if (found != blockDefs.end()) { if (found != blockDefs.end()) {
@@ -109,7 +113,12 @@ Content* ContentBuilder::build() {
auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices); auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices);
auto content = std::make_unique<Content>( 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 // Now, it's time to resolve foreign keys
@@ -136,11 +145,13 @@ Content::Content(
std::unique_ptr<DrawGroups> drawGroups, std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs, std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs, 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), ) : blockDefs(blockDefs),
itemDefs(itemDefs), itemDefs(itemDefs),
indices(indices), indices(indices),
packs(std::move(packs)), packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)),
drawGroups(std::move(drawGroups)) drawGroups(std::move(drawGroups))
{} {}
@@ -178,6 +189,14 @@ ItemDef& Content::requireItem(std::string id) const {
return *found->second; 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 { const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
auto found = packs.find(id); auto found = packs.find(id);
if (found == packs.end()) { if (found == packs.end()) {
@@ -186,6 +205,10 @@ const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
return found->second.get(); 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 { const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const {
return packs; return packs;
} }
+10 -2
View File
@@ -8,10 +8,10 @@
#include <unordered_map> #include <unordered_map>
#include <set> #include <set>
#include "../typedefs.h" #include "../typedefs.h"
#include "../voxels/Block.h"
using DrawGroups = std::set<ubyte>; using DrawGroups = std::set<ubyte>;
class Block;
class ItemDef; class ItemDef;
class Content; class Content;
class ContentPackRuntime; class ContentPackRuntime;
@@ -48,6 +48,8 @@ class ContentBuilder {
std::unordered_map<std::string, ItemDef*> itemDefs; std::unordered_map<std::string, ItemDef*> itemDefs;
std::vector<std::string> itemIds; std::vector<std::string> itemIds;
std::unordered_map<std::string, BlockMaterial> blockMaterials;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs; std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
public: public:
~ContentBuilder(); ~ContentBuilder();
@@ -55,6 +57,7 @@ public:
void add(Block* def); void add(Block* def);
void add(ItemDef* def); void add(ItemDef* def);
void add(ContentPackRuntime* pack); void add(ContentPackRuntime* pack);
void add(BlockMaterial material);
Block& createBlock(std::string id); Block& createBlock(std::string id);
ItemDef& createItem(std::string id); ItemDef& createItem(std::string id);
@@ -111,6 +114,7 @@ class Content {
std::unordered_map<std::string, ItemDef*> itemDefs; std::unordered_map<std::string, ItemDef*> itemDefs;
std::unique_ptr<ContentIndices> indices; std::unique_ptr<ContentIndices> indices;
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;
public: public:
std::unique_ptr<DrawGroups> const drawGroups; std::unique_ptr<DrawGroups> const drawGroups;
@@ -119,7 +123,8 @@ public:
std::unique_ptr<DrawGroups> drawGroups, std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs, std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs, 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(); ~Content();
@@ -133,8 +138,11 @@ public:
ItemDef* findItem(std::string id) const; ItemDef* findItem(std::string id) const;
ItemDef& requireItem(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 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; 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) { void ContentLoader::load(ContentBuilder& builder) {
std::cout << "-- loading pack [" << pack->id << "]" << std::endl; std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
@@ -363,4 +372,13 @@ void ContentLoader::load(ContentBuilder& builder) {
stats.totalItems++; 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));
}
}
} }
+7 -3
View File
@@ -1,12 +1,13 @@
#ifndef CONTENT_CONTENT_LOADER_H_ #ifndef CONTENT_CONTENT_LOADER_H_
#define CONTENT_CONTENT_LOADER_H_ #define CONTENT_CONTENT_LOADER_H_
#include "../voxels/Block.h"
#include <string> #include <string>
#include <filesystem> #include <filesystem>
namespace fs = std::filesystem; namespace fs = std::filesystem;
class Block;
class ItemDef; class ItemDef;
struct ContentPack; struct ContentPack;
class ContentBuilder; class ContentBuilder;
@@ -22,12 +23,15 @@ class ContentLoader {
void loadBlock(Block& def, std::string full, std::string name); void loadBlock(Block& def, std::string full, std::string name);
void loadCustomBlockModel(Block& def, dynamic::Map* primitives); void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
void loadItem(ItemDef& def, std::string full, std::string name); void loadItem(ItemDef& def, std::string full, std::string name);
BlockMaterial loadBlockMaterial(fs::path file, std::string full);
public: public:
ContentLoader(ContentPack* pack); ContentLoader(ContentPack* pack);
bool fixPackIndices(std::filesystem::path folder, bool fixPackIndices(
fs::path folder,
dynamic::Map* indicesRoot, dynamic::Map* indicesRoot,
std::string contentSection); std::string contentSection
);
void fixPackIndices(); void fixPackIndices();
void loadBlock(Block& def, std::string name, fs::path file); void loadBlock(Block& def, std::string name, fs::path file);
void loadItem(ItemDef& def, std::string name, fs::path file); void loadItem(ItemDef& def, std::string name, fs::path file);
+9 -5
View File
@@ -8,6 +8,7 @@
#include "../voxels/Block.h" #include "../voxels/Block.h"
#include "../assets/Assets.h" #include "../assets/Assets.h"
#include "../graphics/Atlas.h" #include "../graphics/Atlas.h"
#include "../content/Content.h"
#include "../logic/LevelController.h" #include "../logic/LevelController.h"
#include "../logic/PlayerController.h" #include "../logic/PlayerController.h"
@@ -16,8 +17,8 @@ LevelFrontend::LevelFrontend(Level* level, Assets* assets)
: level(level), : level(level),
assets(assets), assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)), contentCache(std::make_unique<ContentGfxCache>(level->content, assets)),
blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content)) { blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content))
} {}
void LevelFrontend::observe(LevelController* controller) { void LevelFrontend::observe(LevelController* controller) {
controller->getPlayerController()->listenBlockInteraction( controller->getPlayerController()->listenBlockInteraction(
@@ -25,9 +26,12 @@ void LevelFrontend::observe(LevelController* controller) {
if (type != BlockInteraction::step) { if (type != BlockInteraction::step) {
return; return;
} }
// (test code) auto material = level->content->findBlockMaterial(def->material);
// TODO: replace with BlockMaterial properties access if (material == nullptr) {
auto sound = assets->getSound("steps/"+def->material.substr(def->material.find(':')+1)); return;
}
auto sound = assets->getSound(material->stepsSound);
audio::play( audio::play(
sound, sound,
glm::vec3(), glm::vec3(),
+32 -4
View File
@@ -18,6 +18,8 @@ inline constexpr uint FACE_PY = 3;
inline constexpr uint FACE_MZ = 4; inline constexpr uint FACE_MZ = 4;
inline constexpr uint FACE_PZ = 5; inline constexpr uint FACE_PZ = 5;
/// @brief Grid size used for physics solver collision checking with
/// complex hitboxes
inline constexpr uint BLOCK_AABB_GRID = 16; inline constexpr uint BLOCK_AABB_GRID = 16;
inline std::string DEFAULT_MATERIAL = "base:stone"; inline std::string DEFAULT_MATERIAL = "base:stone";
@@ -37,7 +39,7 @@ struct CoordSystem {
glm::ivec3 axisY; glm::ivec3 axisY;
glm::ivec3 axisZ; glm::ivec3 axisZ;
// Grid 3d position fix offset (for negative vectors) /// @brief Grid 3d position fix offset (for negative vectors)
glm::ivec3 fix; glm::ivec3 fix;
CoordSystem() = default; CoordSystem() = default;
@@ -57,6 +59,7 @@ struct BlockRotProfile {
/// @brief Wood logs, pillars, pipes /// @brief Wood logs, pillars, pipes
static const BlockRotProfile PIPE; static const BlockRotProfile PIPE;
/// @brief Doors, signs and other panes /// @brief Doors, signs and other panes
static const BlockRotProfile PANE; static const BlockRotProfile PANE;
}; };
@@ -80,9 +83,9 @@ using BoxModel = AABB;
/// @brief Common kit of block properties applied to groups of blocks /// @brief Common kit of block properties applied to groups of blocks
struct BlockMaterial { struct BlockMaterial {
std::string name; std::string name;
std::string stepsSound; std::string stepsSound {""};
std::string placeSound; std::string placeSound {""};
std::string breakSound; std::string breakSound {""};
}; };
/// @brief Block properties definition /// @brief Block properties definition
@@ -90,6 +93,7 @@ class Block {
public: public:
/// @brief Block string id (with prefix included) /// @brief Block string id (with prefix included)
std::string const name; std::string const name;
/// @brief Textures set applied to block sides /// @brief Textures set applied to block sides
std::string textureFaces[6]; // -x,x, -y,y, -z,z std::string textureFaces[6]; // -x,x, -y,y, -z,z
@@ -98,57 +102,81 @@ public:
std::vector<glm::vec3> modelExtraPoints = {}; //initially made for tetragons std::vector<glm::vec3> modelExtraPoints = {}; //initially made for tetragons
std::vector<UVRegion> modelUVs = {}; // boxes' tex-UVs also there std::vector<UVRegion> modelUVs = {}; // boxes' tex-UVs also there
/// @brief id of used BlockMaterial, may specify non-existing material
std::string material = DEFAULT_MATERIAL; std::string material = DEFAULT_MATERIAL;
/// @brief Light emission R, G, B, S (sky lights: sun, moon, radioactive clouds) /// @brief Light emission R, G, B, S (sky lights: sun, moon, radioactive clouds)
uint8_t emission[4] {0, 0, 0, 0}; uint8_t emission[4] {0, 0, 0, 0};
/// @brief Influences visible block sides for transparent blocks /// @brief Influences visible block sides for transparent blocks
uint8_t drawGroup = 0; uint8_t drawGroup = 0;
/// @brief Block model type /// @brief Block model type
BlockModel model = BlockModel::block; BlockModel model = BlockModel::block;
/// @brief Does the block passing lights into itself /// @brief Does the block passing lights into itself
bool lightPassing = false; bool lightPassing = false;
/// @brief Does the block passing top-down sky lights into itself /// @brief Does the block passing top-down sky lights into itself
bool skyLightPassing = false; bool skyLightPassing = false;
/// @brief Is the block a physical obstacle /// @brief Is the block a physical obstacle
bool obstacle = true; bool obstacle = true;
/// @brief Can the block be selected /// @brief Can the block be selected
bool selectable = true; bool selectable = true;
/// @brief Can the block be replaced with other. /// @brief Can the block be replaced with other.
/// Examples of replaceable blocks: air, flower, water /// Examples of replaceable blocks: air, flower, water
bool replaceable = false; bool replaceable = false;
/// @brief Can player destroy the block /// @brief Can player destroy the block
bool breakable = true; bool breakable = true;
/// @brief Can the block be oriented different ways /// @brief Can the block be oriented different ways
bool rotatable = false; bool rotatable = false;
/// @brief Can the block exist without physical support be a solid block below /// @brief Can the block exist without physical support be a solid block below
bool grounded = false; bool grounded = false;
/// @brief Turns off block item generation /// @brief Turns off block item generation
bool hidden = false; bool hidden = false;
/// @brief Set of block physical hitboxes /// @brief Set of block physical hitboxes
std::vector<AABB> hitboxes; std::vector<AABB> hitboxes;
/// @brief Set of available block rotations (coord-systems) /// @brief Set of available block rotations (coord-systems)
BlockRotProfile rotations; BlockRotProfile rotations;
/// @brief Item will be picked on MMB click on the block /// @brief Item will be picked on MMB click on the block
std::string pickingItem = name+BLOCK_ITEM_SUFFIX; std::string pickingItem = name+BLOCK_ITEM_SUFFIX;
/// @brief Block script name in blocks/ without extension /// @brief Block script name in blocks/ without extension
std::string scriptName = name.substr(name.find(':')+1); std::string scriptName = name.substr(name.find(':')+1);
/// @brief Default block layout will be used by hud.open_block(...) /// @brief Default block layout will be used by hud.open_block(...)
std::string uiLayout = name; std::string uiLayout = name;
/// @brief Block inventory size. 0 - no inventory /// @brief Block inventory size. 0 - no inventory
uint inventorySize = 0; uint inventorySize = 0;
/// @brief Runtime indices (content indexing results)
struct { struct {
/// @brief block runtime integer id /// @brief block runtime integer id
blockid_t id; blockid_t id;
/// @brief is the block completely opaque for render and raycast /// @brief is the block completely opaque for render and raycast
bool solid = true; bool solid = true;
/// @brief does the block emit any lights /// @brief does the block emit any lights
bool emissive = false; bool emissive = false;
/// @brief set of hitboxes sets with all coord-systems precalculated /// @brief set of hitboxes sets with all coord-systems precalculated
std::vector<AABB> hitboxes[BlockRotProfile::MAX_COUNT]; std::vector<AABB> hitboxes[BlockRotProfile::MAX_COUNT];
/// @brief set of block callbacks flags /// @brief set of block callbacks flags
block_funcs_set funcsset {}; block_funcs_set funcsset {};
/// @brief picking item integer id /// @brief picking item integer id
itemid_t pickingItem = 0; itemid_t pickingItem = 0;
} rt; } rt;