From f4653878a24f86b8c895f0574a8cfc838480a347 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 9 Jul 2024 04:27:18 +0300 Subject: [PATCH] add player.get/set_entity --- res/content/base/scripts/components/drop.lua | 7 ++--- res/modules/internal/stdcomp.lua | 2 ++ src/logic/scripting/lua/libplayer.cpp | 28 ++++++++++++++++++-- src/objects/Entities.cpp | 3 ++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 63be68cc..494a5345 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -72,12 +72,13 @@ function on_fall() end function on_trigger_enter(index, oid) - if ready and oid == 0 and index == 0 then + local playerentity = player.get_entity(hud.get_player()):get_uid() + if ready and oid == playerentity and index == 0 then entity:despawn() inventory.add(player.get_inventory(oid), dropitem.id, dropitem.count) audio.play_sound_2d("events/pickup", 0.5, 0.8+math.random()*0.4, "regular") end - if index == 1 and ready and oid == 0 then + if index == 1 and ready and oid == playerentity then target = oid end end @@ -104,7 +105,7 @@ end function on_update() if target ~= -1 then - local dir = vec3.sub({player.get_pos(target)}, tsf:get_pos()) + local dir = vec3.sub(entities.get(target).transform:get_pos(), tsf:get_pos()) vec3.normalize(dir, dir) vec3.mul(dir, 10.0, dir) body:set_vel(dir) diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 9753b297..fd6a8039 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -47,6 +47,8 @@ local Entity = {__index={ despawn=function(self) return entities.despawn(self.eid) end, set_rig=function(self, s) return entities.set_rig(self.eid, s) end, get_component=function(self, name) return self.components[name] end, + has_component=function(self, name) return self.components[name] ~= nil end, + get_uid=function(self) return self.eid end, }} local entities = {} diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index 8f91ce3b..dd004215 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -1,4 +1,4 @@ -#include "api_lua.hpp" +#include "libentity.hpp" #include "../../../world/Level.hpp" #include "../../../objects/Player.hpp" @@ -155,7 +155,29 @@ static int l_player_set_spawnpoint(lua::State* L) { auto z = lua::tonumber(L, 4); player->setSpawnPoint(glm::vec3(x, y, z)); } - + return 0; +} + +static int l_player_get_entity(lua::State* L) { + auto player = get_player(L, 1); + if (player == nullptr) { + return 0; + } + if (lua::get_from(L, "entities", "get")) { + lua::pushinteger(L, player->getEntity()); + return lua::call(L, 1); + } + return 0; +} + +static int l_player_set_entity(lua::State* L) { + auto player = get_player(L, 1); + if (player == nullptr) { + return 0; + } + if (auto entity = get_entity(L, 2)) { + player->setEntity(entity->getUID()); + } return 0; } @@ -175,5 +197,7 @@ const luaL_Reg playerlib [] = { {"get_selected_block", lua::wrap}, {"set_spawnpoint", lua::wrap}, {"get_spawnpoint", lua::wrap}, + {"get_entity", lua::wrap}, + {"set_entity", lua::wrap}, {NULL, NULL} }; diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index e02bab96..b11106d0 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -82,7 +82,8 @@ entityid_t Entities::spawn( id = uid; } registry.emplace(entity, static_cast(id), def); - auto& tsf = registry.emplace(entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true); + const auto& tsf = registry.emplace( + entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true); auto& body = registry.emplace( entity, true, Hitbox {position, def.hitbox}, std::vector{});