From 27a7d7eb4465e9e4aa50360eaec3352ed224ed85 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 23 Jul 2024 22:23:25 +0300 Subject: [PATCH] add module 'item_models' --- res/content/base/scripts/components/drop.lua | 31 ++++--------------- res/modules/item_models.lua | 32 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 res/modules/item_models.lua diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 9fd7dc5e..77eb3ea3 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -1,3 +1,5 @@ +local item_models = require "core:item_models" + local tsf = entity.transform local body = entity.rigidbody local rig = entity.skeleton @@ -13,6 +15,7 @@ if SAVED_DATA.item then dropitem.count = SAVED_DATA.count end +local DROP_SCALE = 0.3 local scale = {1, 1, 1} local rotation = mat4.rotate({ math.random(), math.random(), math.random() @@ -25,31 +28,9 @@ end do -- setup visuals local matrix = mat4.idt() - local icon = item.icon(dropitem.id) - if icon:find("^block%-previews%:") then - local bid = block.index(icon:sub(16)) - model = block.get_model(bid) - if model == "X" then - body:set_size(vec3.mul(body:get_size(), {1.0, 0.3, 1.0})) - rig:set_model(0, "drop-item") - rig:set_texture("$0", icon) - else - if model == "aabb" then - local rot = block.get_rotation_profile(bid) == "pipe" and 4 or 0 - scale = block.get_hitbox(bid, rot)[2] - body:set_size(vec3.mul(body:get_size(), {1.0, 0.7, 1.0})) - vec3.mul(scale, 1.5, scale) - end - 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 - body:set_size(vec3.mul(body:get_size(), {1.0, 0.3, 1.0})) - rig:set_model(0, "drop-item") - rig:set_texture("$0", icon) - end + scale = item_models.setup(dropitem.id, rig) + local bodysize = math.min(scale[1], scale[2], scale[3]) * DROP_SCALE + body:set_size({scale[1] * DROP_SCALE, bodysize, scale[3] * DROP_SCALE}) mat4.mul(matrix, rotation, matrix) mat4.scale(matrix, scale, matrix) rig:set_matrix(0, matrix) diff --git a/res/modules/item_models.lua b/res/modules/item_models.lua new file mode 100644 index 00000000..9a6e326f --- /dev/null +++ b/res/modules/item_models.lua @@ -0,0 +1,32 @@ +local function setup(id, rig) + local icon = item.icon(id) + local size = {1.0, 1.0, 1.0} + if icon:find("^block%-previews%:") then + local bid = block.index(icon:sub(16)) + model = block.get_model(bid) + if model == "X" then + size = {1.0, 0.3, 1.0} + rig:set_model(0, "drop-item") + rig:set_texture("$0", icon) + else + if model == "aabb" then + local rot = block.get_rotation_profile(bid) == "pipe" and 4 or 0 + size = block.get_hitbox(bid, rot)[2] + vec3.mul(size, 2.0, size) + end + 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 + size = {1.0, 0.3, 1.0} + rig:set_model(0, "drop-item") + rig:set_texture("$0", icon) + end + return size +end + +return { + setup=setup, +}