From 6d7f950410719ab971d76fc105e16e98e82fb6f5 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 27 Jun 2024 08:23:01 +0300 Subject: [PATCH] add Rigidbody.is_enabled, .set_enabled --- src/logic/scripting/lua/libentity.cpp | 20 ++++++++++++++++++-- src/objects/Entities.cpp | 10 +++++++--- src/objects/Entities.hpp | 9 +++++++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 894e17c9..1a7cf027 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -47,14 +47,14 @@ static int l_set_pos(lua::State* L) { static int l_get_vel(lua::State* L) { if (auto entity = get_entity(L, 1)) { - return lua::pushvec3_arr(L, entity->getHitbox().velocity); + return lua::pushvec3_arr(L, entity->getRigidbody().hitbox.velocity); } return 0; } static int l_set_vel(lua::State* L) { if (auto entity = get_entity(L, 1)) { - entity->getHitbox().velocity = lua::tovec3(L, 2); + entity->getRigidbody().hitbox.velocity = lua::tovec3(L, 2); } return 0; } @@ -73,6 +73,20 @@ static int l_set_rot(lua::State* L) { return 0; } +static int l_is_enabled(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + lua::pushboolean(L, entity->getRigidbody().enabled); + } + return 0; +} + +static int l_set_enabled(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->getRigidbody().enabled = lua::toboolean(L, 2); + } + return 0; +} + const luaL_Reg entitylib [] = { {"spawn", lua::wrap}, {NULL, NULL} @@ -87,6 +101,8 @@ const luaL_Reg entitylib [] = { }; const luaL_Reg rigidbodylib [] = { + {"is_enabled", lua::wrap}, + {"set_enabled", lua::wrap}, {"get_vel", lua::wrap}, {"set_vel", lua::wrap}, {NULL, NULL} diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 6d886afc..9261db2a 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -28,15 +28,19 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) { auto id = nextID++; registry.emplace(entity, static_cast(id)); registry.emplace(entity, pos, size/4.0f, glm::mat3(1.0f)); - registry.emplace(entity, pos, def.hitbox); + registry.emplace(entity, true, Hitbox {pos, def.hitbox}); entities[id] = entity; return id; } void Entities::updatePhysics(float delta){ - auto view = registry.view(); + auto view = registry.view(); auto physics = level->physics.get(); - for (auto [entity, transform, hitbox] : view.each()) { + for (auto [entity, transform, rigidbody] : view.each()) { + if (!rigidbody.enabled) { + continue; + } + auto& hitbox = rigidbody.hitbox; physics->step( level->chunks.get(), &hitbox, diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 3613f860..376462df 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -22,6 +22,11 @@ struct Transform { void refresh(); }; +struct Rigidbody { + bool enabled = true; + Hitbox hitbox; +}; + class Level; class Assets; class LineBatch; @@ -45,8 +50,8 @@ public: return registry.get(entity); } - Hitbox& getHitbox() const { - return registry.get(entity); + Rigidbody& getRigidbody() const { + return registry.get(entity); } entityid_t getUID() const {