add on_update, on_render to docs & change entities tps to 20

This commit is contained in:
MihailRis 2024-08-01 16:04:27 +03:00
parent 3f67944cc2
commit f8907f7db1
10 changed files with 62 additions and 21 deletions

View File

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

View File

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

View File

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

View File

@ -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<Shader>("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);

View File

@ -94,6 +94,7 @@ public:
const DrawContext& context,
Camera* camera,
const EngineSettings& settings,
float delta,
bool pause
);
};

View File

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

View File

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

View File

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

View File

@ -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<void(*callback)(const Entity&, size_t, entityid_t)>
@ -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<Transform, rigging::Skeleton>();

View File

@ -167,6 +167,7 @@ class Entities {
std::unordered_map<entt::entity, entityid_t> uids;
entityid_t nextID = 1;
util::Clock sensorsTickClock;
util::Clock updateTickClock;
void updateSensors(
Rigidbody& body, const Transform& tsf, std::vector<Sensor*>& 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,