feat: passing args to component in entity definition

This commit is contained in:
MihailRis
2025-08-06 23:34:00 +03:00
parent f2aa77db8b
commit f6be6689aa
5 changed files with 33 additions and 7 deletions
+12 -1
View File
@@ -30,7 +30,18 @@ template<> void ContentUnitLoader<EntityDef>::loadUnit(
if (auto found = root.at("components")) { if (auto found = root.at("components")) {
for (const auto& elem : *found) { for (const auto& elem : *found) {
def.components.emplace_back(elem.asString()); std::string name;
dv::value params;
if (elem.isObject()) {
name = elem["name"].asString();
if (elem.has("args")) {
params = elem["args"];
}
} else {
name = elem.asString();
}
def.components.push_back(ComponentInstance {
std::move(name), std::move(params)});
} }
} }
if (auto found = root.at("hitbox")) { if (auto found = root.at("hitbox")) {
+2
View File
@@ -603,6 +603,8 @@ void scripting::on_entity_spawn(
} else { } else {
lua::createtable(L, 0, 0); lua::createtable(L, 0, 0);
} }
} else if (component->params != nullptr) {
lua::pushvalue(L, component->params);
} else { } else {
lua::createtable(L, 0, 0); lua::createtable(L, 0, 0);
} }
+2 -2
View File
@@ -173,9 +173,9 @@ entityid_t Entities::spawn(
auto& scripting = registry.emplace<ScriptComponents>(entity); auto& scripting = registry.emplace<ScriptComponents>(entity);
registry.emplace<rigging::Skeleton>(entity, skeleton->instance()); registry.emplace<rigging::Skeleton>(entity, skeleton->instance());
for (auto& componentName : def.components) { for (auto& instance : def.components) {
auto component = std::make_unique<UserComponent>( auto component = std::make_unique<UserComponent>(
componentName, EntityFuncsSet {}, nullptr instance.component, EntityFuncsSet {}, nullptr, instance.params
); );
scripting.components.emplace_back(std::move(component)); scripting.components.emplace_back(std::move(component));
} }
+9 -2
View File
@@ -80,11 +80,18 @@ struct UserComponent {
std::string name; std::string name;
EntityFuncsSet funcsset; EntityFuncsSet funcsset;
scriptenv env; scriptenv env;
dv::value params;
UserComponent( UserComponent(
const std::string& name, EntityFuncsSet funcsset, scriptenv env const std::string& name,
EntityFuncsSet funcsset,
scriptenv env,
dv::value params
) )
: name(name), funcsset(funcsset), env(env) { : name(name),
funcsset(funcsset),
env(std::move(env)),
params(std::move(params)) {
} }
}; };
+8 -2
View File
@@ -5,6 +5,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "typedefs.hpp" #include "typedefs.hpp"
#include "data/dv.hpp"
#include "maths/aabb.hpp" #include "maths/aabb.hpp"
#include "physics/Hitbox.hpp" #include "physics/Hitbox.hpp"
@@ -12,12 +13,17 @@ namespace rigging {
class SkeletonConfig; class SkeletonConfig;
} }
struct ComponentInstance {
std::string component;
dv::value params;
};
struct EntityDef { struct EntityDef {
/// @brief Entity string id (with prefix included) /// @brief Entity string id (with prefix included)
std::string const name; std::string const name;
/// @brief Component IDs /// @brief Component instances
std::vector<std::string> components; std::vector<ComponentInstance> components;
/// @brief Physic body type /// @brief Physic body type
BodyType bodyType = BodyType::DYNAMIC; BodyType bodyType = BodyType::DYNAMIC;