add on_update event

This commit is contained in:
MihailRis 2024-06-29 22:19:42 +03:00
parent bb1c0b4b44
commit 982c8b132d
7 changed files with 41 additions and 10 deletions

View File

@ -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

View File

@ -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
}

View File

@ -50,6 +50,7 @@ void LevelController::update(float delta, bool input, bool pause) {
}
blocks->update(delta);
level->entities->updatePhysics(delta);
level->entities->update();
}
}

View File

@ -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<int>(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<dynamic::Value> args

View File

@ -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(

View File

@ -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<Transform, Rigidbody>();

View File

@ -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);