refactor entities events

This commit is contained in:
MihailRis 2024-07-16 13:27:39 +03:00
parent 0df5d34e04
commit 54902d7f49
4 changed files with 49 additions and 66 deletions

View File

@ -352,7 +352,7 @@ void scripting::on_entity_spawn(
} }
} }
static bool process_entity_callback( static void process_entity_callback(
const scriptenv& env, const scriptenv& env,
const std::string& name, const std::string& name,
std::function<int(lua::State*)> args std::function<int(lua::State*)> args
@ -367,80 +367,61 @@ static bool process_entity_callback(
} }
} }
lua::pop(L); lua::pop(L);
return true;
} }
bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) { static void process_entity_callback(
const Entity& entity,
const std::string& name,
bool entity_funcs_set::*flag,
std::function<int(lua::State*)> args
) {
const auto& script = entity.getScripting(); const auto& script = entity.getScripting();
for (auto& component : script.components) { for (auto& component : script.components) {
if (component->funcsset.on_despawn) { if (component->funcsset.*flag) {
process_entity_callback(component->env, "on_despawn", nullptr); process_entity_callback(component->env, name, args);
} }
} }
}
void scripting::on_entity_despawn(const Entity& entity) {
process_entity_callback(entity, "on_despawn", &entity_funcs_set::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);
lua::pushinteger(L, entity.getUID()); lua::pushinteger(L, entity.getUID());
lua::call(L, 1, 0); lua::call(L, 1, 0);
return true;
} }
bool scripting::on_entity_grounded(const Entity& entity, float force) { void scripting::on_entity_grounded(const Entity& entity, float force) {
const auto& script = entity.getScripting(); process_entity_callback(entity, "on_grounded", &entity_funcs_set::on_grounded, [force](auto L){
for (auto& component : script.components) {
if (component->funcsset.on_grounded) {
process_entity_callback(component->env, "on_grounded", [force](auto L){
return lua::pushnumber(L, force); return lua::pushnumber(L, force);
}); });
} }
}
return true; void scripting::on_entity_fall(const Entity& entity) {
process_entity_callback(entity, "on_fall", &entity_funcs_set::on_fall, nullptr);
} }
bool scripting::on_entity_fall(const Entity& entity) { void scripting::on_entity_save(const Entity& entity) {
const auto& script = entity.getScripting(); process_entity_callback(entity, "on_save", &entity_funcs_set::on_save, nullptr);
for (auto& component : script.components) {
if (component->funcsset.on_fall) {
process_entity_callback(component->env, "on_fall", nullptr);
}
}
return true;
}
bool scripting::on_entity_save(const Entity& entity) {
const auto& script = entity.getScripting();
for (auto& component : script.components) {
if (component->funcsset.on_save) {
process_entity_callback(component->env, "on_save", nullptr);
}
}
return true;
} }
void scripting::on_sensor_enter(const Entity& entity, size_t index, entityid_t oid) { void scripting::on_sensor_enter(const Entity& entity, size_t index, entityid_t oid) {
const auto& script = entity.getScripting(); process_entity_callback(entity, "on_sensor_enter",
for (auto& component : script.components) { &entity_funcs_set::on_sensor_enter, [index, oid](auto L) {
if (component->funcsset.on_sensor_enter) {
process_entity_callback(component->env, "on_sensor_enter", [index, oid](auto L) {
lua::pushinteger(L, index); lua::pushinteger(L, index);
lua::pushinteger(L, oid); lua::pushinteger(L, oid);
return 2; return 2;
}); });
} }
}
}
void scripting::on_sensor_exit(const Entity& entity, size_t index, entityid_t oid) { void scripting::on_sensor_exit(const Entity& entity, size_t index, entityid_t oid) {
const auto& script = entity.getScripting(); process_entity_callback(entity, "on_sensor_exit",
for (auto& component : script.components) { &entity_funcs_set::on_sensor_exit, [index, oid](auto L) {
if (component->funcsset.on_sensor_exit) {
process_entity_callback(component->env, "on_sensor_exit", [index, oid](auto L) {
lua::pushinteger(L, index); lua::pushinteger(L, index);
lua::pushinteger(L, oid); lua::pushinteger(L, oid);
return 2; return 2;
}); });
} }
}
}
void scripting::on_entities_update() { void scripting::on_entities_update() {
auto L = lua::get_main_thread(); auto L = lua::get_main_thread();

View File

@ -85,10 +85,10 @@ namespace scripting {
dynamic::Value args, dynamic::Value args,
dynamic::Map_sptr saved dynamic::Map_sptr saved
); );
bool on_entity_despawn(const EntityDef& def, const Entity& entity); void on_entity_despawn(const Entity& entity);
bool on_entity_grounded(const Entity& entity, float force); void on_entity_grounded(const Entity& entity, float force);
bool on_entity_fall(const Entity& entity); void on_entity_fall(const Entity& entity);
bool on_entity_save(const Entity& entity); void on_entity_save(const Entity& entity);
void on_entities_update(); void on_entities_update();
void on_entities_render(); void on_entities_render();
void on_sensor_enter(const Entity& entity, size_t index, entityid_t oid); void on_sensor_enter(const Entity& entity, size_t index, entityid_t oid);

View File

@ -165,7 +165,7 @@ void Entities::despawn(entityid_t id) {
auto& eid = entity->getID(); auto& eid = entity->getID();
if (!eid.destroyFlag) { if (!eid.destroyFlag) {
eid.destroyFlag = true; eid.destroyFlag = true;
scripting::on_entity_despawn(entity->getDef(), *entity); scripting::on_entity_despawn(*entity);
} }
} }
} }

View File

@ -15,13 +15,15 @@
#include <entt/entity/registry.hpp> #include <entt/entity/registry.hpp>
struct entity_funcs_set { struct entity_funcs_set {
bool init : 1; bool init;
bool on_despawn : 1; bool on_despawn;
bool on_grounded : 1; bool on_grounded;
bool on_fall : 1; bool on_fall;
bool on_sensor_enter : 1; bool on_sensor_enter;
bool on_sensor_exit : 1; bool on_sensor_exit;
bool on_save : 1; bool on_save;
bool on_aim_on;
bool on_aim_off;
}; };
struct EntityDef; struct EntityDef;