add standard components test
This commit is contained in:
parent
e0e5faa4a8
commit
c54be7b2b1
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
35
res/modules/internal/stdcomp.lua
Normal file
35
res/modules/internal/stdcomp.lua
Normal 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}
|
||||
@ -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
|
||||
|
||||
@ -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>);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user