add triggers

This commit is contained in:
MihailRis
2024-06-30 16:25:08 +03:00
parent 50c714692a
commit 5769be8ec8
15 changed files with 239 additions and 64 deletions
+9 -9
View File
@@ -30,15 +30,6 @@ void LevelController::update(float delta, bool input, bool pause) {
settings.chunks.loadDistance.get() +
settings.chunks.padding.get() * 2);
chunks->update(settings.chunks.loadSpeed.get());
player->update(delta, input, pause);
// erease null pointers
level->objects.erase(
std::remove_if(
level->objects.begin(), level->objects.end(),
[](auto obj) { return obj == nullptr; }),
level->objects.end()
);
level->entities->clean();
if (!pause) {
@@ -52,6 +43,15 @@ void LevelController::update(float delta, bool input, bool pause) {
level->entities->updatePhysics(delta);
level->entities->update();
}
player->update(delta, input, pause);
// erease null pointers
level->objects.erase(
std::remove_if(
level->objects.begin(), level->objects.end(),
[](auto obj) { return obj == nullptr; }),
level->objects.end()
);
}
void LevelController::saveWorld() {
+27 -2
View File
@@ -273,6 +273,8 @@ scriptenv scripting::on_entity_spawn(const EntityDef& def, entityid_t eid, entit
funcsset.on_grounded = lua::hasfield(L, "on_grounded");
funcsset.on_fall = lua::hasfield(L, "on_fall");
funcsset.on_despawn = lua::hasfield(L, "on_despawn");
funcsset.on_trigger_enter = lua::hasfield(L, "on_trigger_enter");
funcsset.on_trigger_exit = lua::hasfield(L, "on_trigger_exit");
lua::pop(L, 2);
return entityenv;
}
@@ -325,6 +327,29 @@ bool scripting::on_entity_fall(const Entity& entity) {
return true;
}
void scripting::on_trigger_enter(const Entity& entity, size_t index, entityid_t oid) {
const auto& script = entity.getScripting();
if (script.funcsset.on_trigger_enter) {
process_entity_callback(script.env, "on_trigger_enter", [index, oid](auto L) {
lua::pushinteger(L, index);
lua::pushinteger(L, oid);
return 2;
});
}
}
void scripting::on_trigger_exit(const Entity& entity, size_t index, entityid_t oid) {
const auto& script = entity.getScripting();
if (script.funcsset.on_trigger_exit) {
process_entity_callback(script.env, "on_trigger_exit", [index, oid](auto L) {
lua::pushinteger(L, index);
lua::pushinteger(L, oid);
return 2;
});
}
}
void scripting::on_entities_update() {
auto L = lua::get_main_thread();
lua::get_from(L, STDCOMP, "update", true);
@@ -415,10 +440,10 @@ 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");
}
void scripting::load_entity_script(const scriptenv& penv, const EntityDef& def, const fs::path& file) {
void scripting::load_entity_component(const scriptenv& penv, const EntityDef& def, const fs::path& file) {
auto L = lua::get_main_thread();
std::string src = files::read_string(file);
logger.info() << "script (entity) " << file.u8string();
logger.info() << "script (component) " << file.u8string();
lua::loadbuffer(L, 0, src, file.u8string());
lua::store_in(L, lua::CHUNKS_TABLE, def.scriptName);
}
+3 -1
View File
@@ -81,6 +81,8 @@ namespace scripting {
bool on_entity_grounded(const Entity& entity, float force);
bool on_entity_fall(const Entity& entity);
void on_entities_update();
void on_trigger_enter(const Entity& entity, size_t index, entityid_t oid);
void on_trigger_exit(const Entity& entity, size_t index, entityid_t oid);
/// @brief Called on UI view show
void on_ui_open(
@@ -115,7 +117,7 @@ namespace scripting {
const fs::path& file,
item_funcs_set& funcsset);
void load_entity_script(
void load_entity_component(
const scriptenv& env,
const EntityDef& def,
const fs::path& file);