add 'on_physics_update' entity event

This commit is contained in:
MihailRis 2025-08-13 21:29:20 +03:00
parent b60f0f0ea2
commit 6d3dac9106
6 changed files with 42 additions and 3 deletions

View File

@ -125,6 +125,23 @@ return {
::continue::
end
end,
physics_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_physics_update
if not component.__disabled and callback then
local result, err = pcall(callback, tps)
if err then
debug.error(err)
end
end
end
::continue::
end
end,
render = function(delta)
for _,entity in pairs(entities) do
for _, component in pairs(entity.components) do

View File

@ -18,7 +18,7 @@ end
def_prop("jump_force", 0.0)
def_prop("air_damping", 1.0)
def_prop("ground_damping", 1.0)
def_prop("movement_speed", 4.0)
def_prop("movement_speed", 3.0)
def_prop("run_speed_mul", 1.5)
def_prop("crouch_speed_mul", 0.35)
def_prop("flight_speed_mul", 4.0)
@ -56,7 +56,7 @@ function move_horizontal(speed, dir, vel)
body:set_vel(vel)
end
function on_update(tps)
function on_physics_update(tps)
local delta = (1.0 / tps)
local pid = entity:get_player()
if pid and hud and not hud.is_inventory_open() and not menu.page ~= "" then

View File

@ -130,6 +130,7 @@ namespace scripting {
void on_entity_fall(const Entity& entity);
void on_entity_save(const Entity& entity);
void on_entities_update(int tps, int parts, int part);
void on_entities_physics_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);

View File

@ -279,6 +279,16 @@ void scripting::on_entities_update(int tps, int parts, int part) {
lua::pop(L);
}
void scripting::on_entities_physics_update(int tps, int parts, int part) {
auto L = lua::get_main_state();
lua::get_from(L, STDCOMP, "physics_update", true);
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(float delta) {
auto L = lua::get_main_state();
lua::get_from(L, STDCOMP, "render", true);

View File

@ -25,7 +25,10 @@
static debug::Logger logger("entities");
Entities::Entities(Level& level)
: level(level), sensorsTickClock(20, 3), updateTickClock(20, 3) {
: level(level),
sensorsTickClock(20, 3),
updateTickClock(20, 3),
physicsTickClock(60, 1) {
}
std::optional<Entity> Entities::get(entityid_t id) {
@ -320,6 +323,13 @@ void Entities::update(float delta) {
updateTickClock.getPart()
);
}
if (physicsTickClock.update(delta)) {
scripting::on_entities_physics_update(
physicsTickClock.getTickRate(),
physicsTickClock.getParts(),
physicsTickClock.getPart()
);
}
}
static void debug_render_skeleton(

View File

@ -39,6 +39,7 @@ class Entities {
entityid_t nextID = 1;
util::Clock sensorsTickClock;
util::Clock updateTickClock;
util::Clock physicsTickClock;
void updateSensors(
Rigidbody& body, const Transform& tsf, std::vector<Sensor*>& sensors