feat: multiple components per entity support

This commit is contained in:
MihailRis
2024-07-04 19:47:52 +03:00
parent a6aa42a521
commit 8f379f2ee7
8 changed files with 120 additions and 65 deletions
+3
View File
@@ -1,4 +1,7 @@
{ {
"components": [
"drop"
],
"hitbox": [0.2, 0.125, 0.2], "hitbox": [0.2, 0.125, 0.2],
"triggers": [ "triggers": [
["aabb", -0.2, -0.2, -0.2, 0.2, 0.2, 0.2], ["aabb", -0.2, -0.2, -0.2, 0.2, 0.2, 0.2],
+9 -6
View File
@@ -53,6 +53,7 @@ return {
entity.rigidbody = new_Rigidbody(eid) entity.rigidbody = new_Rigidbody(eid)
entity.modeltree = new_Modeltree(eid) entity.modeltree = new_Modeltree(eid)
entity.data = {} entity.data = {}
entity.components = {}
entities[eid] = entity; entities[eid] = entity;
return entity return entity
end, end,
@@ -62,17 +63,19 @@ return {
remove_Entity = function(eid) remove_Entity = function(eid)
local entity = entities[eid] local entity = entities[eid]
if entity then if entity then
entity.env = nil entity.components = nil
entities[eid] = nil; entities[eid] = nil;
end end
end, end,
update = function() update = function()
for id,entity in pairs(entities) do for id,entity in pairs(entities) do
local callback = entity.env.on_update for _, component in pairs(entity.components) do
if callback then local callback = component.on_update
local result, err = pcall(callback) if callback then
if err then local result, err = pcall(callback)
print(err) if err then
print(err)
end
end end
end end
end end
+10 -5
View File
@@ -311,7 +311,11 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa
void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs::path& file) { void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs::path& file) {
auto root = files::read_json(file); auto root = files::read_json(file);
root->str("script-name", def.scriptName); if (auto componentsarr = root->list("components")) {
for (size_t i = 0; i < componentsarr->size(); i++) {
def.components.push_back(componentsarr->str(i));
}
}
if (auto boxarr = root->list("hitbox")) { if (auto boxarr = root->list("hitbox")) {
def.hitbox = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2)); def.hitbox = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
} }
@@ -341,9 +345,11 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const st
auto configFile = folder/fs::path("entities/"+name+".json"); auto configFile = folder/fs::path("entities/"+name+".json");
if (fs::exists(configFile)) loadEntity(def, full, configFile); if (fs::exists(configFile)) loadEntity(def, full, configFile);
auto scriptfile = folder/fs::path("scripts/components/"+def.scriptName+".lua"); for (auto& componentName : def.components) {
if (fs::is_regular_file(scriptfile)) { auto scriptfile = folder/fs::path("scripts/components/"+componentName+".lua");
scripting::load_entity_component(env, def, scriptfile); if (fs::is_regular_file(scriptfile)) {
scripting::load_entity_component(env, componentName, scriptfile);
}
} }
} }
@@ -437,7 +443,6 @@ void ContentLoader::load() {
std::string full = colon == std::string::npos ? pack->id + ":" + name : name; std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/'; if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.entities.create(full); auto& def = builder.entities.create(full);
if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
loadEntity(def, full, name); loadEntity(def, full, name);
stats->totalEntities++; stats->totalEntities++;
} }
+66 -41
View File
@@ -101,7 +101,9 @@ scriptenv scripting::create_doc_environment(const scriptenv& parent, const std::
} }
[[nodiscard]] [[nodiscard]]
static scriptenv create_entity_environment(const scriptenv& parent, int entityIdx) { static scriptenv create_component_environment(const scriptenv& parent,
int entityIdx,
const std::string& name) {
auto L = lua::get_main_thread(); auto L = lua::get_main_thread();
int id = lua::create_environment(L, *parent); int id = lua::create_environment(L, *parent);
@@ -115,7 +117,12 @@ static scriptenv create_entity_environment(const scriptenv& parent, int entityId
lua::pushvalue(L, -2); lua::pushvalue(L, -2);
lua::setfield(L, "entity"); lua::setfield(L, "entity");
lua::setfield(L, "env"); lua::pop(L);
if (lua::getfield(L, "components")) {
lua::pushenv(L, id);
lua::setfield(L, name);
lua::pop(L);
}
lua::pop(L); lua::pop(L);
return std::shared_ptr<int>(new int(id), [=](int* id) { return std::shared_ptr<int>(new int(id), [=](int* id) {
@@ -256,10 +263,10 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
}); });
} }
scriptenv scripting::on_entity_spawn( void scripting::on_entity_spawn(
const EntityDef& def, const EntityDef& def,
entityid_t eid, entityid_t eid,
entity_funcs_set& funcsset, const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args dynamic::Value args
) { ) {
auto L = lua::get_main_thread(); auto L = lua::get_main_thread();
@@ -268,22 +275,30 @@ scriptenv scripting::on_entity_spawn(
lua::pushinteger(L, eid); lua::pushinteger(L, eid);
lua::call(L, 1); lua::call(L, 1);
} }
auto entityenv = create_entity_environment(get_root_environment(), -1); for (size_t i = 0; i < components.size()-1; i++) {
lua::get_from(L, lua::CHUNKS_TABLE, def.scriptName, true); lua::pushvalue(L, -1);
lua::pushenv(L, *entityenv); }
lua::pushvalue(L, args); for (auto& component : components) {
lua::setfield(L, "ARGS"); auto compenv = create_component_environment(
lua::setfenv(L); get_root_environment(), -1, component->name);
lua::call_nothrow(L, 0, 0); lua::get_from(L, lua::CHUNKS_TABLE, component->name, true);
lua::pushenv(L, *compenv);
lua::pushvalue(L, args);
lua::setfield(L, "ARGS");
lua::setfenv(L);
lua::call_nothrow(L, 0, 0);
lua::pushenv(L, *entityenv); lua::pushenv(L, *compenv);
funcsset.on_grounded = lua::hasfield(L, "on_grounded"); auto& funcsset = component->funcsset;
funcsset.on_fall = lua::hasfield(L, "on_fall"); funcsset.on_grounded = lua::hasfield(L, "on_grounded");
funcsset.on_despawn = lua::hasfield(L, "on_despawn"); funcsset.on_fall = lua::hasfield(L, "on_fall");
funcsset.on_trigger_enter = lua::hasfield(L, "on_trigger_enter"); funcsset.on_despawn = lua::hasfield(L, "on_despawn");
funcsset.on_trigger_exit = lua::hasfield(L, "on_trigger_exit"); funcsset.on_trigger_enter = lua::hasfield(L, "on_trigger_enter");
lua::pop(L, 2); funcsset.on_trigger_exit = lua::hasfield(L, "on_trigger_exit");
return entityenv; lua::pop(L, 2);
component->env = compenv;
}
} }
static bool process_entity_callback( static bool process_entity_callback(
@@ -306,8 +321,10 @@ static bool process_entity_callback(
bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) { bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) {
const auto& script = entity.getScripting(); const auto& script = entity.getScripting();
if (script.funcsset.on_despawn) { for (auto& component : script.components) {
process_entity_callback(script.env, "on_despawn", nullptr); if (component->funcsset.on_despawn) {
process_entity_callback(component->env, "on_despawn", nullptr);
}
} }
auto L = lua::get_main_thread(); auto L = lua::get_main_thread();
lua::get_from(L, "stdcomp", "remove_Entity", true); lua::get_from(L, "stdcomp", "remove_Entity", true);
@@ -318,42 +335,50 @@ bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) {
bool scripting::on_entity_grounded(const Entity& entity, float force) { bool scripting::on_entity_grounded(const Entity& entity, float force) {
const auto& script = entity.getScripting(); const auto& script = entity.getScripting();
if (script.funcsset.on_grounded) { for (auto& component : script.components) {
return process_entity_callback(script.env, "on_grounded", [force](auto L){ if (component->funcsset.on_grounded) {
return lua::pushnumber(L, force); process_entity_callback(component->env, "on_grounded", [force](auto L){
}); return lua::pushnumber(L, force);
});
}
} }
return true; return true;
} }
bool scripting::on_entity_fall(const Entity& entity) { bool scripting::on_entity_fall(const Entity& entity) {
const auto& script = entity.getScripting(); const auto& script = entity.getScripting();
if (script.funcsset.on_fall) { for (auto& component : script.components) {
return process_entity_callback(script.env, "on_fall", nullptr); if (component->funcsset.on_fall) {
process_entity_callback(component->env, "on_fall", nullptr);
}
} }
return true; return true;
} }
void scripting::on_trigger_enter(const Entity& entity, size_t index, entityid_t oid) { void scripting::on_trigger_enter(const Entity& entity, size_t index, entityid_t oid) {
const auto& script = entity.getScripting(); const auto& script = entity.getScripting();
if (script.funcsset.on_trigger_enter) { for (auto& component : script.components) {
process_entity_callback(script.env, "on_trigger_enter", [index, oid](auto L) { if (component->funcsset.on_trigger_enter) {
lua::pushinteger(L, index); process_entity_callback(component->env, "on_trigger_enter", [index, oid](auto L) {
lua::pushinteger(L, oid); lua::pushinteger(L, index);
return 2; lua::pushinteger(L, oid);
}); return 2;
});
}
} }
} }
void scripting::on_trigger_exit(const Entity& entity, size_t index, entityid_t oid) { void scripting::on_trigger_exit(const Entity& entity, size_t index, entityid_t oid) {
const auto& script = entity.getScripting(); const auto& script = entity.getScripting();
if (script.funcsset.on_trigger_exit) { for (auto& component : script.components) {
process_entity_callback(script.env, "on_trigger_exit", [index, oid](auto L) { if (component->funcsset.on_trigger_exit) {
lua::pushinteger(L, index); process_entity_callback(component->env, "on_trigger_exit", [index, oid](auto L) {
lua::pushinteger(L, oid); lua::pushinteger(L, index);
return 2; lua::pushinteger(L, oid);
}); return 2;
});
}
} }
} }
@@ -447,12 +472,12 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi
funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby"); funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby");
} }
void scripting::load_entity_component(const scriptenv& penv, const EntityDef& def, const fs::path& file) { void scripting::load_entity_component(const scriptenv& penv, const std::string& name, const fs::path& file) {
auto L = lua::get_main_thread(); auto L = lua::get_main_thread();
std::string src = files::read_string(file); std::string src = files::read_string(file);
logger.info() << "script (component) " << file.u8string(); logger.info() << "script (component) " << file.u8string();
lua::loadbuffer(L, *penv, src, file.u8string()); lua::loadbuffer(L, *penv, src, file.u8string());
lua::store_in(L, lua::CHUNKS_TABLE, def.scriptName); lua::store_in(L, lua::CHUNKS_TABLE, name);
} }
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) { void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
+5 -5
View File
@@ -27,7 +27,7 @@ class Inventory;
class UiDocument; class UiDocument;
struct block_funcs_set; struct block_funcs_set;
struct item_funcs_set; struct item_funcs_set;
struct entity_funcs_set; struct UserComponent;
struct uidocscript; struct uidocscript;
class BlocksController; class BlocksController;
class LevelController; class LevelController;
@@ -76,10 +76,10 @@ namespace scripting {
/// @return true if prevents default action /// @return true if prevents default action
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z); bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
scriptenv on_entity_spawn( void on_entity_spawn(
const EntityDef& def, const EntityDef& def,
entityid_t eid, entityid_t eid,
entity_funcs_set&, const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args dynamic::Value args
); );
bool on_entity_despawn(const EntityDef& def, const Entity& entity); bool on_entity_despawn(const EntityDef& def, const Entity& entity);
@@ -124,7 +124,7 @@ namespace scripting {
void load_entity_component( void load_entity_component(
const scriptenv& env, const scriptenv& env,
const EntityDef& def, const std::string& name,
const fs::path& file); const fs::path& file);
/// @brief Load package-specific world script /// @brief Load package-specific world script
+7 -4
View File
@@ -90,12 +90,15 @@ entityid_t Entities::spawn(
create_trigger_callback<scripting::on_trigger_exit>(this) create_trigger_callback<scripting::on_trigger_exit>(this)
}; };
} }
auto& scripting = registry.emplace<Scripting>( auto& scripting = registry.emplace<ScriptComponents>(entity);
entity, entity_funcs_set {}, nullptr);
entities[id] = entity; entities[id] = entity;
registry.emplace<rigging::Rig>(entity, rig->instance()); registry.emplace<rigging::Rig>(entity, rig->instance());
scripting.env = scripting::on_entity_spawn( for (auto& componentName : def.components) {
def, id, scripting.funcsset, std::move(args)); auto component = std::make_unique<UserComponent>(
componentName, entity_funcs_set {}, nullptr);
scripting.components.emplace_back(std::move(component));
}
scripting::on_entity_spawn(def, id, scripting.components, std::move(args));
return id; return id;
} }
+18 -3
View File
@@ -6,6 +6,7 @@
#include "../data/dynamic.hpp" #include "../data/dynamic.hpp"
#include <vector> #include <vector>
#include <memory>
#include <optional> #include <optional>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#define GLM_ENABLE_EXPERIMENTAL #define GLM_ENABLE_EXPERIMENTAL
@@ -65,9 +66,23 @@ struct Rigidbody {
std::vector<Trigger> triggers; std::vector<Trigger> triggers;
}; };
struct Scripting { struct UserComponent {
std::string name;
entity_funcs_set funcsset; entity_funcs_set funcsset;
scriptenv env; scriptenv env;
UserComponent(const std::string& name, entity_funcs_set funcsset, scriptenv env)
: name(name), funcsset(funcsset), env(env) {}
};
struct ScriptComponents {
std::vector<std::unique_ptr<UserComponent>> components;
ScriptComponents() = default;
ScriptComponents(ScriptComponents&& other)
: components(std::move(other.components)) {
}
}; };
class Level; class Level;
@@ -116,8 +131,8 @@ public:
return registry.get<Rigidbody>(entity); return registry.get<Rigidbody>(entity);
} }
Scripting& getScripting() const { ScriptComponents& getScripting() const {
return registry.get<Scripting>(entity); return registry.get<ScriptComponents>(entity);
} }
rigging::Rig& getModeltree() const; rigging::Rig& getModeltree() const;
+2 -1
View File
@@ -15,8 +15,9 @@ namespace rigging {
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;
std::vector<std::string> components;
std::string scriptName = name.substr(name.find(':')+1);
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 {};