From 8e931d8f538f5c822758cb05242a46e724a4064a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 25 Jun 2024 16:35:06 +0300 Subject: [PATCH] add entity library --- res/content/base/scripts/hud.lua | 5 ++++- src/logic/scripting/lua/libentity.cpp | 28 +++++++++++++++++++++++++-- src/logic/scripting/lua/lua_util.hpp | 10 ++++++++++ src/objects/Entities.cpp | 10 +++++----- src/objects/Entities.hpp | 21 +++++++++++++++++--- 5 files changed, 63 insertions(+), 11 deletions(-) diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 8e605bb8..e33f5a0f 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -1,5 +1,8 @@ function on_hud_open() input.add_callback("player.drop", function () - entity.test() + local pid = hud.get_player() + local pvel = {player.get_vel(pid)} + local eid = entity.test() + entity.set_vel(eid, pvel) end) end diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 5ab0181b..859364a8 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -8,19 +8,43 @@ #include "../../../window/Camera.hpp" #include "../../../frontend/hud.hpp" +#include + namespace scripting { extern Hud* hud; } using namespace scripting; +static std::optional get_entity(lua::State* L, int idx) { + auto id = lua::tointeger(L, idx); + auto level = controller->getLevel(); + return level->entities->get(id); +} + static int l_test(lua::State* L) { auto level = controller->getLevel(); auto player = hud->getPlayer(); - level->entities->drop(player->camera->position, player->camera->front*8.0f+glm::vec3(0, 2, 0)+player->hitbox->velocity); - return 0; + auto id = level->entities->drop(player->camera->position); + return lua::pushinteger(L, id); +} + +static int l_get_vel(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushvec3_arr(L, entity->getHitbox().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); + } + return 0; } const luaL_Reg entitylib [] = { {"test", lua::wrap}, + {"get_vel", lua::wrap}, + {"set_vel", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index a774d283..2095526b 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -379,6 +379,16 @@ namespace lua { pop(L); return glm::vec4(x, y, z, w); } + inline glm::vec3 tovec3_stack(lua::State* L, int idx) { + return glm::vec3( + tonumber(L, idx), tonumber(L, idx+1), tonumber(L, idx+2) + ); + } + inline glm::vec4 tovec4_stack(lua::State* L, int idx) { + return glm::vec4( + tonumber(L, idx), tonumber(L, idx+1), tonumber(L, idx+2), tonumber(L, idx+3) + ); + } inline glm::mat4 tomat4(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, idx) || objlen(L, idx) < 16) { diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index fd4fe76c..a608dded 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -20,16 +20,16 @@ void Transform::refresh() { Entities::Entities(Level* level) : level(level) { } -void Entities::drop(glm::vec3 pos, glm::vec3 vel) { +entityid_t Entities::drop(glm::vec3 pos) { auto entity = registry.create(); glm::vec3 size(1); - registry.emplace(entity, static_cast(1)); + auto id = nextID++; + registry.emplace(entity, static_cast(id)); registry.emplace(entity, pos, size/4.0f, glm::mat3(1.0f)); registry.emplace(entity, pos, glm::vec3(size.x*0.2f, size.y*0.5f, size.z*0.2f)); - - auto& hitbox = registry.get(entity); - hitbox.velocity = vel; + entities[id] = entity; + return id; } void Entities::updatePhysics(float delta){ diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 8efc7197..f51f59bb 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -2,7 +2,9 @@ #define OBJECTS_ENTITIES_HPP_ #include "../typedefs.hpp" +#include "../physics/Hitbox.hpp" +#include #include #include #include @@ -28,9 +30,9 @@ class Rig; class Entity { entt::registry& registry; - entt::entity entity; + const entt::entity entity; public: - Entity(entt::registry& registry, entt::entity entity) + Entity(entt::registry& registry, const entt::entity entity) : registry(registry), entity(entity) {} bool isValid() const { @@ -41,6 +43,10 @@ public: return registry.get(entity); } + Hitbox& getHitbox() const { + return registry.get(entity); + } + entityid_t getUID() const { return registry.get(entity).uid; } @@ -50,12 +56,21 @@ class Entities { entt::registry registry; Level* level; std::unordered_map entities; + entityid_t nextID = 1; public: Entities(Level* level); void updatePhysics(float delta); void render(Assets* assets, ModelBatch& batch, Frustum& frustum); - void drop(glm::vec3 pos, glm::vec3 vel); + entityid_t drop(glm::vec3 pos); + + std::optional get(entityid_t id) { + const auto& found = entities.find(id); + if (found != entities.end()) { + return Entity(registry, found->second); + } + return std::nullopt; + } }; #endif // OBJECTS_ENTITIES_HPP_