From 982c8b132da6bb5dd8d11b387d3a275bfb1fdb16 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 29 Jun 2024 22:19:42 +0300 Subject: [PATCH] add on_update event --- res/content/base/scripts/drop.lua | 14 ++++++++------ res/modules/internal/stdcomp.lua | 17 ++++++++++++++++- src/logic/LevelController.cpp | 1 + src/logic/scripting/scripting.cpp | 13 ++++++++++--- src/logic/scripting/scripting.hpp | 1 + src/objects/Entities.cpp | 4 ++++ src/objects/Entities.hpp | 1 + 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/res/content/base/scripts/drop.lua b/res/content/base/scripts/drop.lua index 3740cc0e..dbdace91 100644 --- a/res/content/base/scripts/drop.lua +++ b/res/content/base/scripts/drop.lua @@ -1,9 +1,11 @@ -function on_despawn() - print("despawn") -end - +inair = true function on_grounded() entity.transform:set_rot(mat4.rotate({0, 1, 0}, math.random()*360)) - print(stdcomp) - entity:despawn() + inair = false +end + +function on_update() + if inair then + entity.transform:set_rot(mat4.rotate(entity.transform:get_rot(), {0, 1, 0}, math.random()*32)) + end end diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 3316bae4..1fd16e1b 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -40,6 +40,21 @@ return { return entity end, remove_Entity = function(eid) - entities[eid] = nil; + local entity = entities[eid] + if entity then + entity.env = nil + entities[eid] = nil; + end + end, + update = function() + for id,entity in pairs(entities) do + local callback = entity.env.on_update + if callback then + local result, err = pcall(callback) + if err then + print(err) + end + end + end end } diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index f42bc926..11c7533d 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -50,6 +50,7 @@ void LevelController::update(float delta, bool input, bool pause) { } blocks->update(delta); level->entities->updatePhysics(delta); + level->entities->update(); } } diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index fb4fc4ea..cd6c407f 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -114,8 +114,9 @@ static scriptenv create_entity_environment(const scriptenv& parent, int entityId lua::pushvalue(L, -2); lua::setfield(L, "entity"); - - lua::pop(L, 2); + + lua::setfield(L, "env"); + lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { lua::removeEnvironment(L, *id); @@ -307,12 +308,18 @@ bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) { bool scripting::on_entity_grounded(const EntityDef& def, const Entity& entity) { const auto& script = entity.getScripting(); - if (script.funcsset.on_despawn) { + if (script.funcsset.on_grounded) { return process_entity_callback(script.env, "on_grounded", nullptr); } return true; } +void scripting::on_entities_update() { + auto L = lua::get_main_thread(); + lua::get_from(L, STDCOMP, "update", true); + lua::call_nothrow(L, 0, 0); +} + void scripting::on_ui_open( UiDocument* layout, std::vector args diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 75a36f14..98b9b9a2 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -79,6 +79,7 @@ namespace scripting { scriptenv on_entity_spawn(const EntityDef& def, entityid_t eid, entity_funcs_set&); bool on_entity_despawn(const EntityDef& def, const Entity& entity); bool on_entity_grounded(const EntityDef& def, const Entity& entity); + void on_entities_update(); /// @brief Called on UI view show void on_ui_open( diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 799d7f1a..1d1fc214 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -86,6 +86,10 @@ void Entities::updatePhysics(float delta){ } } +void Entities::update() { + scripting::on_entities_update(); +} + void Entities::renderDebug(LineBatch& batch) { batch.lineWidth(1.0f); auto view = registry.view(); diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 7cc49ab0..52cdcbde 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -103,6 +103,7 @@ public: void clean(); void updatePhysics(float delta); + void update(); void renderDebug(LineBatch& batch); void render(Assets* assets, ModelBatch& batch, Frustum& frustum);