add drop-item model, new block functions

This commit is contained in:
MihailRis 2024-07-03 16:34:09 +03:00
parent 1c4e13dc67
commit 48b5be6bc9
11 changed files with 81 additions and 24 deletions

View File

@ -17,27 +17,32 @@ vt 1.0 1.0
vt 0.0 1.0
vt 1.0 0.0
vt 1.0 1.0
vt 0.0 1.0
vt 0.0 0.0
vt 1.0 0.0
vt 0.0 1.0
vt 0.0 0.0
vt 0.0 1.0
vt 1.0 0.0
vt 1.0 1.0
vt 1.0 0.0
vt 1.0 1.0
vt 0.0 1.0
vt 0.0 0.0
vn 0.0 -1.0 0.0
vn 0.0 1.0 0.0
vn 1.0 -0.0 0.0
vn -0.0 -0.0 1.0
vn -1.0 -0.0 -0.0
vn 0.0 0.0 -1.0
usemtl $0
vn -0.0 -0.0 1.0
usemtl $2
s off
f 1/1/1 2/2/1 3/3/1 4/4/1
usemtl $3
f 5/5/2 8/6/2 7/7/2 6/8/2
f 1/1/3 5/9/3 6/10/3 2/11/3
f 2/12/4 6/13/4 7/7/4 3/14/4
f 3/15/5 7/16/5 8/17/5 4/4/5
f 5/5/6 1/18/6 4/19/6 8/20/6
usemtl $0
f 1/9/3 5/10/3 6/8/3 2/11/3
usemtl $1
f 3/12/4 7/7/4 8/13/4 4/14/4
usemtl $4
f 5/15/5 1/1/5 4/16/5 8/17/5
usemtl $5
f 2/2/6 6/18/6 7/19/6 3/20/6

View File

@ -5,10 +5,11 @@
"events/pickup"
],
"models": [
"cube",
"item"
"drop-block",
"drop-item"
],
"rigs": [
"drop"
"drop",
"drop-item"
]
}

View File

@ -0,0 +1,5 @@
{
"root": {
"model": "drop-item"
}
}

View File

@ -1,5 +1,5 @@
{
"root": {
"model": "cube"
"model": "drop-block"
}
}

View File

@ -7,19 +7,27 @@ ready = false
local dropitem = ARGS.item
local rotation = mat4.rotate({0, 1, 0}, math.random() * 360)
mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation)
mat4.rotate(rotation, {0, 0, 1}, math.random() * 360, rotation)
rig:set_matrix(0, rotation)
local icon = item.icon(dropitem.id)
if icon:find("^block%-previews%:") then
local bid = block.index(icon:sub(16))
local textures = block.get_textures(bid)
for i,t in ipairs(textures) do
rig:set_texture("$"..tostring(i-1), "blocks:"..textures[i])
do -- setup visuals
local rotation = mat4.rotate({0, 1, 0}, math.random() * 360)
mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation)
mat4.rotate(rotation, {0, 0, 1}, math.random() * 360, rotation)
rig:set_matrix(0, rotation)
local icon = item.icon(dropitem.id)
if icon:find("^block%-previews%:") then
local bid = block.index(icon:sub(16))
if block.get_model(bid) == "X" then
entity:set_rig("drop-item")
rig:set_texture("$0", icon)
else
local textures = block.get_textures(bid)
for i,t in ipairs(textures) do
rig:set_texture("$"..tostring(i-1), "blocks:"..textures[i])
end
end
else
entity:set_rig("drop-item")
rig:set_texture("$0", icon)
end
else
rig:set_texture("$0", icon)
end
function on_grounded(force)

View File

@ -38,6 +38,7 @@ end
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,
}}
local entities = {}

View File

@ -271,6 +271,19 @@ static int l_get_textures(lua::State* L) {
return 0;
}
static int l_get_model(lua::State* L) {
if (auto def = require_block(L)) {
switch (def->model) {
case BlockModel::block: return lua::pushstring(L, "block");
case BlockModel::aabb: return lua::pushstring(L, "aabb");
case BlockModel::xsprite: return lua::pushstring(L, "X");
case BlockModel::custom: return lua::pushstring(L, "custom");
case BlockModel::none: return lua::pushstring(L, "none");
}
return 0;
}
}
const luaL_Reg blocklib [] = {
{"index", lua::wrap<l_index>},
{"name", lua::wrap<l_name>},
@ -295,5 +308,6 @@ const luaL_Reg blocklib [] = {
{"is_segment", lua::wrap<l_is_segment>},
{"seek_origin", lua::wrap<l_seek_origin>},
{"get_textures", lua::wrap<l_get_textures>},
{"get_model", lua::wrap<l_get_model>},
{NULL, NULL}
};

View File

@ -48,6 +48,19 @@ static int l_despawn(lua::State* L) {
return 0;
}
static int l_set_rig(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
auto assets = scripting::engine->getAssets();
std::string rigName = lua::require_string(L, 2);
auto rigConfig = assets->get<rigging::RigConfig>(rigName);
if (rigConfig == nullptr) {
throw std::runtime_error("rig not found '"+rigName+"'");
}
entity->setRig(rigConfig);
}
return 0;
}
static int l_get_pos(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushvec3_arr(L, entity->getTransform().pos);
@ -160,6 +173,7 @@ const luaL_Reg entitylib [] = {
{"exists", lua::wrap<l_exists>},
{"spawn", lua::wrap<l_spawn>},
{"despawn", lua::wrap<l_despawn>},
{"set_rig", lua::wrap<l_set_rig>},
{NULL, NULL}
};

View File

@ -32,6 +32,13 @@ rigging::Rig& Entity::getModeltree() const {
return registry.get<rigging::Rig>(entity);
}
void Entity::setRig(rigging::RigConfig* rigConfig) {
auto& rig = registry.get<rigging::Rig>(entity);
rig.config = rigConfig;
rig.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
rig.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
}
Entities::Entities(Level* level) : level(level) {
}

View File

@ -115,6 +115,8 @@ public:
rigging::Rig& getModeltree() const;
void setRig(rigging::RigConfig* rigConfig);
entityid_t getUID() const {
return registry.get<EntityId>(entity).uid;
}