From f8907f7db1bfffba1843e75995fe043a872825df Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 1 Aug 2024 16:04:27 +0300 Subject: [PATCH] add on_update, on_render to docs & change entities tps to 20 --- doc/en/scripting/ecs.md | 12 ++++++++++++ doc/ru/scripting/ecs.md | 12 ++++++++++++ res/modules/internal/stdcomp.lua | 14 +++++++++----- src/graphics/render/WorldRenderer.cpp | 5 +++-- src/graphics/render/WorldRenderer.hpp | 1 + src/logic/LevelController.cpp | 2 +- src/logic/scripting/scripting.cpp | 12 ++++++++---- src/logic/scripting/scripting.hpp | 4 ++-- src/objects/Entities.cpp | 16 +++++++++++----- src/objects/Entities.hpp | 5 +++-- 10 files changed, 62 insertions(+), 21 deletions(-) diff --git a/doc/en/scripting/ecs.md b/doc/en/scripting/ecs.md index db298a42..df24663b 100644 --- a/doc/en/scripting/ecs.md +++ b/doc/en/scripting/ecs.md @@ -176,6 +176,18 @@ function on_save() Called before component data is saved. Here you can write the data you want to save into the *SAVED_DATA* table, which is available for the entire life of the component. +```lua +function on_update(tps: int) +``` + +Called every entities tick (currently 20 times per second). + +```lua +function on_render(delta: number) +``` + +Called every frame before the entity is rendered. + ```lua function on_sensor_enter(index: int, entity: int) ``` diff --git a/doc/ru/scripting/ecs.md b/doc/ru/scripting/ecs.md index 0e9c2524..fef88b01 100644 --- a/doc/ru/scripting/ecs.md +++ b/doc/ru/scripting/ecs.md @@ -176,6 +176,18 @@ function on_save() Вызывается перед сохранением данных компонентов. Здесь можно записать данные, которые нужно сохранить, в таблицу *SAVED_DATA*, которая доступна весь срок жизни компонента. +```lua +function on_update(tps: int) +``` + +Вызывается каждый такт сущностей (на данный момент - 20 раз в секунду). + +```lua +function on_render(delta: number) +``` + +Вызывается каждый кадр перед рендером сущности. + ```lua function on_sensor_enter(index: int, entity: int) ``` diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 3d5c7194..c1a2d27b 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -88,25 +88,29 @@ return { entities[eid] = nil; end end, - update = function() - for _,entity in pairs(entities) do + update = function(tps, parts, part) + for uid, entity in pairs(entities) do + if uid % parts ~= part then + goto continue + end for _, component in pairs(entity.components) do local callback = component.on_update if callback then - local result, err = pcall(callback) + local result, err = pcall(callback, tps) if err then debug.error(err) end end end + ::continue:: end end, - render = function() + render = function(delta) for _,entity in pairs(entities) do for _, component in pairs(entity.components) do local callback = component.on_render if callback then - local result, err = pcall(callback) + local result, err = pcall(callback, delta) if err then debug.error(err) end diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index b0b08714..73a10047 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -188,6 +188,7 @@ void WorldRenderer::renderLevel( const DrawContext&, Camera* camera, const EngineSettings& settings, + float delta, bool pause ) { auto assets = engine->getAssets(); @@ -206,7 +207,7 @@ void WorldRenderer::renderLevel( auto entityShader = assets->get("entity"); setupWorldShader(entityShader, camera, settings, fogFactor); - level->entities->render(assets, *modelBatch, *frustumCulling, pause); + level->entities->render(assets, *modelBatch, *frustumCulling, delta, pause); if (!pause) { scripting::on_frontend_render(); @@ -339,7 +340,7 @@ void WorldRenderer::draw( DrawContext ctx = wctx.sub(); ctx.setDepthTest(true); ctx.setCullFace(true); - renderLevel(ctx, camera, settings, pause); + renderLevel(ctx, camera, settings, delta, pause); // Debug lines if (hudVisible){ renderLines(camera, linesShader, ctx); diff --git a/src/graphics/render/WorldRenderer.hpp b/src/graphics/render/WorldRenderer.hpp index 29d4bfe4..9a199ce6 100644 --- a/src/graphics/render/WorldRenderer.hpp +++ b/src/graphics/render/WorldRenderer.hpp @@ -94,6 +94,7 @@ public: const DrawContext& context, Camera* camera, const EngineSettings& settings, + float delta, bool pause ); }; diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index a69d7e70..ffb53590 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -41,7 +41,7 @@ void LevelController::update(float delta, bool input, bool pause) { blocks->update(delta); player->update(delta, input, pause); level->entities->updatePhysics(delta); - level->entities->update(); + level->entities->update(delta); } level->entities->clean(); player->postUpdate(delta, input, pause); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 30f5ae53..cb248f72 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -475,17 +475,21 @@ void scripting::on_entity_used(const Entity& entity, Player* player) { }); } -void scripting::on_entities_update() { +void scripting::on_entities_update(int tps, int parts, int part) { auto L = lua::get_main_thread(); lua::get_from(L, STDCOMP, "update", true); - lua::call_nothrow(L, 0, 0); + lua::pushinteger(L, tps); + lua::pushinteger(L, parts); + lua::pushinteger(L, part); + lua::call_nothrow(L, 3, 0); lua::pop(L); } -void scripting::on_entities_render() { +void scripting::on_entities_render(float delta) { auto L = lua::get_main_thread(); lua::get_from(L, STDCOMP, "render", true); - lua::call_nothrow(L, 0, 0); + lua::pushnumber(L, delta); + lua::call_nothrow(L, 1, 0); lua::pop(L); } diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index d4cd4090..d0c96d90 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -89,8 +89,8 @@ namespace scripting { void on_entity_grounded(const Entity& entity, float force); void on_entity_fall(const Entity& entity); void on_entity_save(const Entity& entity); - void on_entities_update(); - void on_entities_render(); + void on_entities_update(int tps, int parts, int part); + void on_entities_render(float delta); void on_sensor_enter(const Entity& entity, size_t index, entityid_t oid); void on_sensor_exit(const Entity& entity, size_t index, entityid_t oid); void on_aim_on(const Entity& entity, Player* player); diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index e2b16aaa..f28031b3 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -59,7 +59,8 @@ void Entity::setRig(const rigging::SkeletonConfig* rigConfig) { ); } -Entities::Entities(Level* level) : level(level), sensorsTickClock(20, 3) { +Entities::Entities(Level* level) +: level(level), sensorsTickClock(20, 3), updateTickClock(20, 3) { } template @@ -440,8 +441,13 @@ void Entities::updatePhysics(float delta) { } } -void Entities::update() { - scripting::on_entities_update(); +void Entities::update(float delta) { + if (updateTickClock.update(delta)) { + scripting::on_entities_update( + updateTickClock.getTickRate(), + updateTickClock.getParts(), + updateTickClock.getPart()); + } } static void debug_render_skeleton( @@ -505,10 +511,10 @@ void Entities::renderDebug( } void Entities::render( - Assets* assets, ModelBatch& batch, const Frustum& frustum, bool pause + Assets* assets, ModelBatch& batch, const Frustum& frustum, float delta, bool pause ) { if (!pause) { - scripting::on_entities_render(); + scripting::on_entities_render(delta); } auto view = registry.view(); diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 6892a7f5..65bad419 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -167,6 +167,7 @@ class Entities { std::unordered_map uids; entityid_t nextID = 1; util::Clock sensorsTickClock; + util::Clock updateTickClock; void updateSensors( Rigidbody& body, const Transform& tsf, std::vector& sensors @@ -183,10 +184,10 @@ public: void clean(); void updatePhysics(float delta); - void update(); + void update(float delta); void renderDebug(LineBatch& batch, const Frustum& frustum, const DrawContext& ctx); - void render(Assets* assets, ModelBatch& batch, const Frustum& frustum, bool pause); + void render(Assets* assets, ModelBatch& batch, const Frustum& frustum, float delta, bool pause); entityid_t spawn( EntityDef& def,