change rigs role from assets to content units

This commit is contained in:
MihailRis 2024-07-08 05:03:09 +03:00
parent 7dfda981a8
commit 49aa64a033
16 changed files with 67 additions and 70 deletions

View File

@ -7,9 +7,5 @@
"models": [ "models": [
"drop-block", "drop-block",
"drop-item" "drop-item"
],
"rigs": [
"drop",
"drop-item"
] ]
} }

View File

@ -9,8 +9,5 @@
"misc/moon", "misc/moon",
"misc/sun", "misc/sun",
"gui/crosshair" "gui/crosshair"
],
"rigs": [
"player"
] ]
} }

View File

@ -31,7 +31,6 @@ AssetsLoader::AssetsLoader(Assets* assets, const ResPaths* paths)
addLoader(AssetType::LAYOUT, assetload::layout); addLoader(AssetType::LAYOUT, assetload::layout);
addLoader(AssetType::SOUND, assetload::sound); addLoader(AssetType::SOUND, assetload::sound);
addLoader(AssetType::MODEL, assetload::model); addLoader(AssetType::MODEL, assetload::model);
addLoader(AssetType::RIG, assetload::rig);
} }
void AssetsLoader::addLoader(AssetType tag, aloader_func func) { void AssetsLoader::addLoader(AssetType tag, aloader_func func) {
@ -102,7 +101,6 @@ static std::string assets_def_folder(AssetType tag) {
case AssetType::LAYOUT: return LAYOUTS_FOLDER; case AssetType::LAYOUT: return LAYOUTS_FOLDER;
case AssetType::SOUND: return SOUNDS_FOLDER; case AssetType::SOUND: return SOUNDS_FOLDER;
case AssetType::MODEL: return MODELS_FOLDER; case AssetType::MODEL: return MODELS_FOLDER;
case AssetType::RIG: return RIGS_FOLDER;
} }
return "<error>"; return "<error>";
} }
@ -160,7 +158,6 @@ void AssetsLoader::processPreloadConfig(const fs::path& file) {
processPreloadList(AssetType::TEXTURE, root->list("textures")); processPreloadList(AssetType::TEXTURE, root->list("textures"));
processPreloadList(AssetType::SOUND, root->list("sounds")); processPreloadList(AssetType::SOUND, root->list("sounds"));
processPreloadList(AssetType::MODEL, root->list("models")); processPreloadList(AssetType::MODEL, root->list("models"));
processPreloadList(AssetType::RIG, root->list("rigs"));
// layouts are loaded automatically // layouts are loaded automatically
} }

View File

@ -26,8 +26,7 @@ enum class AssetType {
ATLAS, ATLAS,
LAYOUT, LAYOUT,
SOUND, SOUND,
MODEL, MODEL
RIG,
}; };
class ResPaths; class ResPaths;

View File

@ -228,27 +228,6 @@ assetload::postfunc assetload::model(
} }
} }
assetload::postfunc assetload::rig(
AssetsLoader* loader,
const ResPaths* paths,
const std::string& file,
const std::string& name,
const std::shared_ptr<AssetCfg>&
) {
auto path = paths->find(file+".json");
auto text = files::read_string(path);
try {
auto rig = rigging::RigConfig::parse(text, path.u8string(), name).release();
return [=](Assets* assets) {
// TODO: add models loading
assets->store(std::unique_ptr<rigging::RigConfig>(rig), name);
};
} catch (const parsing_error& err) {
std::cerr << err.errorLog() << std::endl;
throw;
}
}
static void read_anim_file( static void read_anim_file(
const std::string& animFile, const std::string& animFile,
std::vector<std::pair<std::string, int>>& frameList std::vector<std::pair<std::string, int>>& frameList

View File

@ -63,13 +63,6 @@ namespace assetload {
const std::string& name, const std::string& name,
const std::shared_ptr<AssetCfg>& settings const std::shared_ptr<AssetCfg>& settings
); );
postfunc rig(
AssetsLoader*,
const ResPaths* paths,
const std::string& file,
const std::string& name,
const std::shared_ptr<AssetCfg>& settings
);
} }
#endif // ASSETS_ASSET_LOADERS_HPP_ #endif // ASSETS_ASSET_LOADERS_HPP_

View File

@ -8,6 +8,7 @@
#include "../voxels/Block.hpp" #include "../voxels/Block.hpp"
#include "../items/ItemDef.hpp" #include "../items/ItemDef.hpp"
#include "../objects/EntityDef.hpp" #include "../objects/EntityDef.hpp"
#include "../objects/rigging.hpp"
#include "ContentPack.hpp" #include "ContentPack.hpp"
#include "../logic/scripting/scripting.hpp" #include "../logic/scripting/scripting.hpp"
@ -27,20 +28,30 @@ Content::Content(
ContentUnitDefs<Block> blocks, ContentUnitDefs<Block> blocks,
ContentUnitDefs<ItemDef> items, ContentUnitDefs<ItemDef> items,
ContentUnitDefs<EntityDef> entities, ContentUnitDefs<EntityDef> entities,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs, UptrsMap<std::string, ContentPackRuntime> packs,
std::unordered_map<std::string, std::unique_ptr<BlockMaterial>> blockMaterials UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::RigConfig> rigs
) : indices(std::move(indices)), ) : indices(std::move(indices)),
packs(std::move(packs)), packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)), blockMaterials(std::move(blockMaterials)),
rigs(std::move(rigs)),
blocks(std::move(blocks)), blocks(std::move(blocks)),
items(std::move(items)), items(std::move(items)),
entities(std::move(entities)), entities(std::move(entities)),
drawGroups(std::move(drawGroups)) drawGroups(std::move(drawGroups))
{} {}
Content::~Content() { Content::~Content() {
} }
const rigging::RigConfig* Content::getRig(const std::string& id) const {
auto found = rigs.find(id);
if (found == rigs.end()) {
return nullptr;
}
return found->second.get();
}
const BlockMaterial* Content::findBlockMaterial(const std::string& id) const { const BlockMaterial* Content::findBlockMaterial(const std::string& id) const {
auto found = blockMaterials.find(id); auto found = blockMaterials.find(id);
if (found == blockMaterials.end()) { if (found == blockMaterials.end()) {
@ -57,10 +68,10 @@ const ContentPackRuntime* Content::getPackRuntime(const std::string& id) const {
return found->second.get(); return found->second.get();
} }
const std::unordered_map<std::string, std::unique_ptr<BlockMaterial>>& Content::getBlockMaterials() const { const UptrsMap<std::string, BlockMaterial>& Content::getBlockMaterials() const {
return blockMaterials; return blockMaterials;
} }
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const { const UptrsMap<std::string, ContentPackRuntime>& Content::getPacks() const {
return packs; return packs;
} }

View File

@ -11,6 +11,8 @@
#include <set> #include <set>
using DrawGroups = std::set<ubyte>; using DrawGroups = std::set<ubyte>;
template<class K, class V>
using UptrsMap = std::unordered_map<K, std::unique_ptr<V>>;
class Block; class Block;
struct BlockMaterial; struct BlockMaterial;
@ -19,6 +21,10 @@ struct EntityDef;
class Content; class Content;
class ContentPackRuntime; class ContentPackRuntime;
namespace rigging {
class RigConfig;
}
enum class contenttype { enum class contenttype {
none, block, item, entity none, block, item, entity
}; };
@ -83,9 +89,9 @@ public:
template<class T> template<class T>
class ContentUnitDefs { class ContentUnitDefs {
std::unordered_map<std::string, std::unique_ptr<T>> defs; UptrsMap<std::string, T> defs;
public: public:
ContentUnitDefs(std::unordered_map<std::string, std::unique_ptr<T>> defs) ContentUnitDefs(UptrsMap<std::string, T> defs)
: defs(std::move(defs)) { : defs(std::move(defs)) {
} }
@ -108,8 +114,9 @@ public:
/// @brief Content is a definitions repository /// @brief Content is a definitions repository
class Content { class Content {
std::unique_ptr<ContentIndices> indices; std::unique_ptr<ContentIndices> indices;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs; UptrsMap<std::string, ContentPackRuntime> packs;
std::unordered_map<std::string, std::unique_ptr<BlockMaterial>> blockMaterials; UptrsMap<std::string, BlockMaterial> blockMaterials;
UptrsMap<std::string, rigging::RigConfig> rigs;
public: public:
ContentUnitDefs<Block> blocks; ContentUnitDefs<Block> blocks;
ContentUnitDefs<ItemDef> items; ContentUnitDefs<ItemDef> items;
@ -122,8 +129,9 @@ public:
ContentUnitDefs<Block> blocks, ContentUnitDefs<Block> blocks,
ContentUnitDefs<ItemDef> items, ContentUnitDefs<ItemDef> items,
ContentUnitDefs<EntityDef> entities, ContentUnitDefs<EntityDef> entities,
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs, UptrsMap<std::string, ContentPackRuntime> packs,
std::unordered_map<std::string, std::unique_ptr<BlockMaterial>> blockMaterials UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::RigConfig> rigs
); );
~Content(); ~Content();
@ -131,11 +139,12 @@ public:
return indices.get(); return indices.get();
} }
const rigging::RigConfig* getRig(const std::string& id) const;
const BlockMaterial* findBlockMaterial(const std::string& id) const; const BlockMaterial* findBlockMaterial(const std::string& id) const;
const ContentPackRuntime* getPackRuntime(const std::string& id) const; const ContentPackRuntime* getPackRuntime(const std::string& id) const;
const std::unordered_map<std::string, std::unique_ptr<BlockMaterial>>& getBlockMaterials() const; const UptrsMap<std::string, BlockMaterial>& getBlockMaterials() const;
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& getPacks() const; const UptrsMap<std::string, ContentPackRuntime>& getPacks() const;
}; };
#endif // CONTENT_CONTENT_HPP_ #endif // CONTENT_CONTENT_HPP_

View File

@ -1,11 +1,17 @@
#include "ContentBuilder.hpp" #include "ContentBuilder.hpp"
#include "../objects/rigging.hpp"
ContentBuilder::~ContentBuilder() {} ContentBuilder::~ContentBuilder() {}
void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) { void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) {
packs[pack->getId()] = std::move(pack); packs[pack->getId()] = std::move(pack);
} }
void ContentBuilder::add(std::unique_ptr<rigging::RigConfig> rig) {
rigs[rig->getName()] = std::move(rig);
}
BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) { BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) {
blockMaterials[id] = std::make_unique<BlockMaterial>(); blockMaterials[id] = std::make_unique<BlockMaterial>();
auto& material = *blockMaterials[id]; auto& material = *blockMaterials[id];
@ -68,7 +74,8 @@ std::unique_ptr<Content> ContentBuilder::build() {
items.build(), items.build(),
entities.build(), entities.build(),
std::move(packs), std::move(packs),
std::move(blockMaterials) std::move(blockMaterials),
std::move(rigs)
); );
// Now, it's time to resolve foreign keys // Now, it's time to resolve foreign keys

View File

@ -23,7 +23,7 @@ class ContentUnitBuilder {
} }
} }
public: public:
std::unordered_map<std::string, std::unique_ptr<T>> defs; UptrsMap<std::string, T> defs;
std::vector<std::string> names; std::vector<std::string> names;
ContentUnitBuilder( ContentUnitBuilder(
@ -49,8 +49,9 @@ public:
}; };
class ContentBuilder { class ContentBuilder {
std::unordered_map<std::string, std::unique_ptr<BlockMaterial>> blockMaterials; UptrsMap<std::string, BlockMaterial> blockMaterials;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs; UptrsMap<std::string, rigging::RigConfig> rigs;
UptrsMap<std::string, ContentPackRuntime> packs;
std::unordered_map<std::string, contenttype> allNames; std::unordered_map<std::string, contenttype> allNames;
public: public:
ContentUnitBuilder<Block> blocks {allNames, contenttype::block}; ContentUnitBuilder<Block> blocks {allNames, contenttype::block};
@ -60,6 +61,7 @@ public:
~ContentBuilder(); ~ContentBuilder();
void add(std::unique_ptr<ContentPackRuntime> pack); void add(std::unique_ptr<ContentPackRuntime> pack);
void add(std::unique_ptr<rigging::RigConfig> rig);
BlockMaterial& createBlockMaterial(const std::string& id); BlockMaterial& createBlockMaterial(const std::string& id);

View File

@ -9,6 +9,7 @@
#include "../debug/Logger.hpp" #include "../debug/Logger.hpp"
#include "../files/files.hpp" #include "../files/files.hpp"
#include "../items/ItemDef.hpp" #include "../items/ItemDef.hpp"
#include "../objects/rigging.hpp"
#include "../logic/scripting/scripting.hpp" #include "../logic/scripting/scripting.hpp"
#include "../typedefs.hpp" #include "../typedefs.hpp"
#include "../util/listutil.hpp" #include "../util/listutil.hpp"
@ -458,4 +459,14 @@ void ContentLoader::load() {
loadBlockMaterial(builder.createBlockMaterial(name), file); loadBlockMaterial(builder.createBlockMaterial(name), file);
} }
} }
fs::path rigsDir = folder / fs::u8path("rigs");
if (fs::is_directory(rigsDir)) {
for (const auto& entry : fs::directory_iterator(rigsDir)) {
const fs::path& file = entry.path();
std::string name = pack->id+":"+file.stem().u8string();
std::string text = files::read_string(file);
builder.add(rigging::RigConfig::parse(text, file.u8string(), name));
}
}
} }

View File

@ -40,7 +40,7 @@ static int l_spawn(lua::State* L) {
Transform transform { Transform transform {
pos, glm::vec3(1.0f), glm::mat3(1.0f), {}, true pos, glm::vec3(1.0f), glm::mat3(1.0f), {}, true
}; };
level->entities->spawn(scripting::engine->getAssets(), def, transform, args); level->entities->spawn(def, transform, args);
return 1; return 1;
} }
@ -53,9 +53,8 @@ static int l_despawn(lua::State* L) {
static int l_set_rig(lua::State* L) { static int l_set_rig(lua::State* L) {
if (auto entity = get_entity(L, 1)) { if (auto entity = get_entity(L, 1)) {
auto assets = scripting::engine->getAssets();
std::string rigName = lua::require_string(L, 2); std::string rigName = lua::require_string(L, 2);
auto rigConfig = assets->get<rigging::RigConfig>(rigName); auto rigConfig = content->getRig(rigName);
if (rigConfig == nullptr) { if (rigConfig == nullptr) {
throw std::runtime_error("rig not found '"+rigName+"'"); throw std::runtime_error("rig not found '"+rigName+"'");
} }

View File

@ -38,7 +38,7 @@ rigging::Rig& Entity::getModeltree() const {
return registry.get<rigging::Rig>(entity); return registry.get<rigging::Rig>(entity);
} }
void Entity::setRig(rigging::RigConfig* rigConfig) { void Entity::setRig(const rigging::RigConfig* rigConfig) {
auto& rig = registry.get<rigging::Rig>(entity); auto& rig = registry.get<rigging::Rig>(entity);
rig.config = rigConfig; rig.config = rigConfig;
rig.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f)); rig.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
@ -60,14 +60,13 @@ static triggercallback create_trigger_callback(Entities* entities) {
} }
entityid_t Entities::spawn( entityid_t Entities::spawn(
Assets* assets,
EntityDef& def, EntityDef& def,
Transform transform, Transform transform,
dynamic::Value args, dynamic::Value args,
dynamic::Map_sptr saved, dynamic::Map_sptr saved,
entityid_t uid) entityid_t uid)
{ {
auto rig = assets->get<rigging::RigConfig>(def.rigName); auto rig = level->content->getRig(def.rigName);
if (rig == nullptr) { if (rig == nullptr) {
throw std::runtime_error("rig "+def.rigName+" not found"); throw std::runtime_error("rig "+def.rigName+" not found");
} }
@ -140,8 +139,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) {
dynamic::get_vec(tsfmap, "pos", transform.pos); dynamic::get_vec(tsfmap, "pos", transform.pos);
} }
dynamic::Map_sptr savedMap = map->map("comps"); dynamic::Map_sptr savedMap = map->map("comps");
auto assets = scripting::engine->getAssets(); spawn(def, transform, dynamic::NONE, savedMap, uid);
spawn(assets, def, transform, dynamic::NONE, savedMap, uid);
} }
void Entities::loadEntities(dynamic::Map_sptr root) { void Entities::loadEntities(dynamic::Map_sptr root) {

View File

@ -138,7 +138,7 @@ public:
rigging::Rig& getModeltree() const; rigging::Rig& getModeltree() const;
void setRig(rigging::RigConfig* rigConfig); void setRig(const rigging::RigConfig* rigConfig);
entityid_t getUID() const { entityid_t getUID() const {
return registry.get<EntityId>(entity).uid; return registry.get<EntityId>(entity).uid;
@ -170,7 +170,6 @@ public:
void render(Assets* assets, ModelBatch& batch, const Frustum& frustum); void render(Assets* assets, ModelBatch& batch, const Frustum& frustum);
entityid_t spawn( entityid_t spawn(
Assets* assets,
EntityDef& def, EntityDef& def,
Transform transform, Transform transform,
dynamic::Value args=dynamic::NONE, dynamic::Value args=dynamic::NONE,

View File

@ -21,7 +21,7 @@ struct EntityDef {
glm::vec3 hitbox {0.5f}; glm::vec3 hitbox {0.5f};
std::vector<std::pair<size_t, AABB>> boxTriggers {}; std::vector<std::pair<size_t, AABB>> boxTriggers {};
std::vector<std::pair<size_t, float>> radialTriggers {}; std::vector<std::pair<size_t, float>> radialTriggers {};
std::string rigName = name.substr(name.find(":") + 1); std::string rigName = name;
struct { struct {
bool enabled = true; bool enabled = true;

View File

@ -64,7 +64,7 @@ namespace rigging {
}; };
struct Rig { struct Rig {
RigConfig* config; const RigConfig* config;
Pose pose; Pose pose;
Pose calculated; Pose calculated;
std::unordered_map<std::string, std::string> textures; std::unordered_map<std::string, std::string> textures;
@ -99,7 +99,7 @@ namespace rigging {
Rig& rig, Rig& rig,
const glm::mat4& matrix) const; const glm::mat4& matrix) const;
Rig instance() { Rig instance() const {
return Rig { return Rig {
this, Pose(nodes.size()), Pose(nodes.size()), {} this, Pose(nodes.size()), Pose(nodes.size()), {}
}; };