From 50c714692af86e2d16015004f9c48dd223ad9fe4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 30 Jun 2024 00:32:02 +0300 Subject: [PATCH] add entity event: on_fall --- res/content/base/scripts/drop.lua | 22 +++++++++++++++++----- src/logic/scripting/scripting.cpp | 15 +++++++++++++-- src/logic/scripting/scripting.hpp | 3 ++- src/objects/Entities.cpp | 8 ++++++-- src/objects/Entities.hpp | 1 + 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/res/content/base/scripts/drop.lua b/res/content/base/scripts/drop.lua index dbdace91..b228ec59 100644 --- a/res/content/base/scripts/drop.lua +++ b/res/content/base/scripts/drop.lua @@ -1,11 +1,23 @@ inair = true -function on_grounded() + +function on_grounded(force) entity.transform:set_rot(mat4.rotate({0, 1, 0}, math.random()*360)) 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 +function on_fall() + inair = true +end + +function on_update() + local tsf = entity.transform + local body = entity.rigidbody + if inair then + tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, math.random()*12)) + tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, math.random()*12)) + end + local dir = vec3.sub({player.get_pos(hud.get_player())}, tsf:get_pos()) + vec3.normalize(dir, dir) + vec3.mul(dir, time.delta()*50.0, dir) + --body:set_vel(vec3.add(rigidbody:get_vel(), dir)) end diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index cc50014b..720e2083 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -271,6 +271,7 @@ scriptenv scripting::on_entity_spawn(const EntityDef& def, entityid_t eid, entit lua::pushenv(L, *entityenv); funcsset.on_grounded = lua::hasfield(L, "on_grounded"); + funcsset.on_fall = lua::hasfield(L, "on_fall"); funcsset.on_despawn = lua::hasfield(L, "on_despawn"); lua::pop(L, 2); return entityenv; @@ -306,10 +307,20 @@ bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) { return true; } -bool scripting::on_entity_grounded(const EntityDef& def, const Entity& entity) { +bool scripting::on_entity_grounded(const Entity& entity, float force) { const auto& script = entity.getScripting(); if (script.funcsset.on_grounded) { - return process_entity_callback(script.env, "on_grounded", nullptr); + return process_entity_callback(script.env, "on_grounded", [force](auto L){ + return lua::pushnumber(L, force); + }); + } + return true; +} + +bool scripting::on_entity_fall(const Entity& entity) { + const auto& script = entity.getScripting(); + if (script.funcsset.on_fall) { + return process_entity_callback(script.env, "on_fall", nullptr); } return true; } diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 98b9b9a2..a46d7500 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -78,7 +78,8 @@ 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); + bool on_entity_grounded(const Entity& entity, float force); + bool on_entity_fall(const Entity& entity); void on_entities_update(); /// @brief Called on UI view show diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 1d1fc214..52f914a6 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -67,6 +67,7 @@ void Entities::updatePhysics(float delta){ continue; } auto& hitbox = rigidbody.hitbox; + auto prevVel = hitbox.velocity; bool grounded = hitbox.grounded; physics->step( level->chunks.get(), @@ -77,11 +78,14 @@ void Entities::updatePhysics(float delta){ 1.0f, true ); - hitbox.linearDamping = hitbox.grounded * 12; + hitbox.linearDamping = hitbox.grounded * 24; transform.pos = hitbox.position; //transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0)); if (hitbox.grounded && !grounded) { - scripting::on_entity_grounded(eid.def, *get(eid.uid)); + scripting::on_entity_grounded(*get(eid.uid), glm::length(prevVel-hitbox.velocity)); + } + if (!hitbox.grounded && grounded) { + scripting::on_entity_fall(*get(eid.uid)); } } } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 52cdcbde..80bf7e27 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -13,6 +13,7 @@ struct entity_funcs_set { bool init : 1; bool on_despawn : 1; bool on_grounded : 1; + bool on_fall : 1; }; struct EntityDef;