diff --git a/res/content/base/scripts/drop.lua b/res/content/base/scripts/drop.lua index 9a691298..155e401b 100644 --- a/res/content/base/scripts/drop.lua +++ b/res/content/base/scripts/drop.lua @@ -5,3 +5,7 @@ end function on_despawn(eid) print("despawn", eid) end + +function on_grounded(eid) + Transform.set_rot(eid, mat4.rotate({0, 1, 0}, math.random()*360)) +end diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 710a8e5e..94103056 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -9,6 +9,8 @@ function on_hud_open() local eid = entity.spawn("base:drop", ppos) local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE) Rigidbody.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) - Transform.set_rot(eid, mat4.rotate({0, 1, 0}, math.random() * 360)) + Transform.set_rot(eid, + mat4.rotate(mat4.rotate(mat4.rotate({0, 1, 0}, math.random() * 360), + {1, 0, 0}, math.random() * 360), {0, 0, 1}, math.random() * 360)) end) end diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 8d017d5b..085c7ac2 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -243,6 +243,14 @@ bool scripting::on_entity_despawn(const EntityDef& def, entityid_t eid) { }); } +bool scripting::on_entity_grounded(const EntityDef& def, entityid_t eid) { + std::string name = def.name + ".grounded"; + return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) { + lua::pushinteger(L, eid); + return 1; + }); +} + void scripting::on_ui_open( UiDocument* layout, std::vector args @@ -332,6 +340,7 @@ void scripting::load_entity_script(const scriptenv& senv, const std::string& pre funcsset.init = register_event(env, "init", prefix+".init"); funcsset.on_spawn = register_event(env, "on_spawn", prefix+".spawn"); funcsset.on_despawn = register_event(env, "on_despawn", prefix+".despawn"); + funcsset.on_grounded = register_event(env, "on_grounded", prefix+".grounded"); } void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) { diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 6ad140c8..7bbd59d1 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -77,8 +77,8 @@ namespace scripting { bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z); bool on_entity_spawn(const EntityDef& def, entityid_t eid); - bool on_entity_despawn(const EntityDef& def, entityid_t eid); + bool on_entity_grounded(const EntityDef& def, entityid_t eid); /// @brief Called on UI view show void on_ui_open( diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index a6915b67..1fed9c82 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -61,13 +61,14 @@ void Entities::clean() { } void Entities::updatePhysics(float delta){ - auto view = registry.view(); + auto view = registry.view(); auto physics = level->physics.get(); - for (auto [entity, transform, rigidbody] : view.each()) { + for (auto [entity, eid, transform, rigidbody] : view.each()) { if (!rigidbody.enabled) { continue; } auto& hitbox = rigidbody.hitbox; + bool grounded = hitbox.grounded; physics->step( level->chunks.get(), &hitbox, @@ -80,8 +81,8 @@ void Entities::updatePhysics(float delta){ hitbox.linearDamping = hitbox.grounded * 12; transform.pos = hitbox.position; //transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0)); - if (hitbox.grounded) { - //hitbox.velocity.y = 10; + if (hitbox.grounded && !grounded) { + scripting::on_entity_grounded(eid.def, eid.uid); } } } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index ed4427cb..79df3e49 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -85,6 +85,7 @@ class Entities { entityid_t nextID = 1; public: Entities(Level* level); + void clean(); void updatePhysics(float delta); diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index b5dbdc80..c481e4ef 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -10,6 +10,7 @@ struct entity_funcs_set { bool init : 1; bool on_spawn : 1; bool on_despawn : 1; + bool on_grounded : 1; }; struct EntityDef {