add standard components test

This commit is contained in:
MihailRis 2024-06-28 18:57:40 +03:00
parent e0e5faa4a8
commit c54be7b2b1
6 changed files with 46 additions and 11 deletions

View File

@ -7,5 +7,6 @@ function on_despawn(eid)
end
function on_grounded(eid)
Transform.set_rot(eid, mat4.rotate({0, 1, 0}, math.random()*360))
local entity = stdcomp.new_Entity(eid) -- test
entity.transform:set_rot(mat4.rotate({0, 1, 0}, math.random()*360))
end

View File

@ -8,8 +8,8 @@ function on_hud_open()
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
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,
__rigidbody.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
__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)

View File

@ -0,0 +1,35 @@
-- Standard components OOP wrappers (__index tables of metatables)
local Transform = {__index={
get_pos=function(self) return __transform.get_pos(self.eid) end,
set_pos=function(self, v) return __transform.set_pos(self.eid, v) end,
get_rot=function(self) return __transform.get_rot(self.eid) end,
set_rot=function(self, m) return __transform.set_rot(self.eid, m) end,
}}
function new_Transform(eid)
return setmetatable({eid=eid}, Transform)
end
local Rigidbody = {__index={
is_enabled=function(self) return __rigidbody.is_enabled(self.eid) end,
set_enabled=function(self, f) return __rigidbody.set_enabled(self.eid, f) end,
get_vel=function(self) return __rigidbody.get_vel(self.eid) end,
set_vel=function(self, v) return __rigidbody.set_vel(self.eid, v) end,
get_size=function(self) return __rigidbody.get_size(self.eid) end,
}}
function new_Rigidbody(eid)
return setmetatable({eid=eid}, Rigidbody)
end
-- Entity class
local Entity = {__index={}}
return {new_Entity = function(eid)
local entity = setmetatable({eid=eid}, Entity)
entity.transform = new_Transform(eid)
entity.rigidbody = new_Rigidbody(eid)
return entity
end}

View File

@ -303,6 +303,8 @@ function file.readlines(path)
return lines
end
stdcomp = require "core:internal/stdcomp"
-- Deprecated functions
block_index = block.index
block_name = block.name

View File

@ -49,8 +49,8 @@ static void create_libs(lua::State* L) {
openlib(L, "world", worldlib);
// components
openlib(L, "Rigidbody", rigidbodylib);
openlib(L, "Transform", transformlib);
openlib(L, "__rigidbody", rigidbodylib);
openlib(L, "__transform", transformlib);
addfunc(L, "print", lua::wrap<l_print>);
}

View File

@ -230,24 +230,21 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
bool scripting::on_entity_spawn(const EntityDef& def, entityid_t eid) {
std::string name = def.name + ".spawn";
return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) {
lua::pushinteger(L, eid);
return 1;
return lua::pushinteger(L, eid);
});
}
bool scripting::on_entity_despawn(const EntityDef& def, entityid_t eid) {
std::string name = def.name + ".despawn";
return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) {
lua::pushinteger(L, eid);
return 1;
return lua::pushinteger(L, 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;
return lua::pushinteger(L, eid);
});
}