add player.get/set_entity

This commit is contained in:
MihailRis 2024-07-09 04:27:18 +03:00
parent 9d98c40528
commit f4653878a2
4 changed files with 34 additions and 6 deletions

View File

@ -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)

View File

@ -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 = {}

View File

@ -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<l_player_get_selected_block>},
{"set_spawnpoint", lua::wrap<l_player_set_spawnpoint>},
{"get_spawnpoint", lua::wrap<l_player_get_spawnpoint>},
{"get_entity", lua::wrap<l_player_get_entity>},
{"set_entity", lua::wrap<l_player_set_entity>},
{NULL, NULL}
};

View File

@ -82,7 +82,8 @@ entityid_t Entities::spawn(
id = uid;
}
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id), def);
auto& tsf = registry.emplace<Transform>(entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true);
const auto& tsf = registry.emplace<Transform>(
entity, position, glm::vec3(1.0f), glm::mat3(1.0f), glm::mat4(1.0f), true);
auto& body = registry.emplace<Rigidbody>(
entity, true, Hitbox {position, def.hitbox}, std::vector<Trigger>{});