From 48b5be6bc9e8124432c22498f47f721bc9d9b4f5 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 3 Jul 2024 16:34:09 +0300 Subject: [PATCH] add drop-item model, new block functions --- .../base/models/{cube.obj => drop-block.obj} | 21 +++++++----- .../base/models/{item.obj => drop-item.obj} | 0 res/content/base/preload.json | 7 ++-- res/content/base/rigs/drop-item.json | 5 +++ res/content/base/rigs/drop.json | 2 +- res/content/base/scripts/components/drop.lua | 32 ++++++++++++------- res/modules/internal/stdcomp.lua | 1 + src/logic/scripting/lua/libblock.cpp | 14 ++++++++ src/logic/scripting/lua/libentity.cpp | 14 ++++++++ src/objects/Entities.cpp | 7 ++++ src/objects/Entities.hpp | 2 ++ 11 files changed, 81 insertions(+), 24 deletions(-) rename res/content/base/models/{cube.obj => drop-block.obj} (75%) rename res/content/base/models/{item.obj => drop-item.obj} (100%) create mode 100644 res/content/base/rigs/drop-item.json diff --git a/res/content/base/models/cube.obj b/res/content/base/models/drop-block.obj similarity index 75% rename from res/content/base/models/cube.obj rename to res/content/base/models/drop-block.obj index 969dad45..be4cf551 100644 --- a/res/content/base/models/cube.obj +++ b/res/content/base/models/drop-block.obj @@ -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 diff --git a/res/content/base/models/item.obj b/res/content/base/models/drop-item.obj similarity index 100% rename from res/content/base/models/item.obj rename to res/content/base/models/drop-item.obj diff --git a/res/content/base/preload.json b/res/content/base/preload.json index 6e9aa592..c8283103 100644 --- a/res/content/base/preload.json +++ b/res/content/base/preload.json @@ -5,10 +5,11 @@ "events/pickup" ], "models": [ - "cube", - "item" + "drop-block", + "drop-item" ], "rigs": [ - "drop" + "drop", + "drop-item" ] } diff --git a/res/content/base/rigs/drop-item.json b/res/content/base/rigs/drop-item.json new file mode 100644 index 00000000..18afe6a2 --- /dev/null +++ b/res/content/base/rigs/drop-item.json @@ -0,0 +1,5 @@ +{ + "root": { + "model": "drop-item" + } +} diff --git a/res/content/base/rigs/drop.json b/res/content/base/rigs/drop.json index 675759f7..530b400d 100644 --- a/res/content/base/rigs/drop.json +++ b/res/content/base/rigs/drop.json @@ -1,5 +1,5 @@ { "root": { - "model": "cube" + "model": "drop-block" } } diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index b55ff80b..8d579cf2 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -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) diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 0e9e3baf..985604ee 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -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 = {} diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index c9efe92e..5e70ba65 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -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}, {"name", lua::wrap}, @@ -295,5 +308,6 @@ const luaL_Reg blocklib [] = { {"is_segment", lua::wrap}, {"seek_origin", lua::wrap}, {"get_textures", lua::wrap}, + {"get_model", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 5d55bd6a..337d53ac 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -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(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}, {"spawn", lua::wrap}, {"despawn", lua::wrap}, + {"set_rig", lua::wrap}, {NULL, NULL} }; diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 0152063d..b1d1356a 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -32,6 +32,13 @@ rigging::Rig& Entity::getModeltree() const { return registry.get(entity); } +void Entity::setRig(rigging::RigConfig* rigConfig) { + auto& rig = registry.get(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) { } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 953470fa..9b484fe1 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -115,6 +115,8 @@ public: rigging::Rig& getModeltree() const; + void setRig(rigging::RigConfig* rigConfig); + entityid_t getUID() const { return registry.get(entity).uid; }