move hud away from utils 2 & add new argument

This commit is contained in:
neveix 2026-08-17 23:47:51 +05:00
parent 99ce42fdc5
commit c0b73998ed
2 changed files with 20 additions and 19 deletions

View file

@ -1,26 +1,22 @@
local util = {} local util = {}
local DROP_FORCE = 8 util.DROP_FORCE = 8
local DROP_INIT_VEL = { 0, 3, 0 } util.DROP_INIT_VEL = { 0, 3, 0 }
local DROP_MAX_ITEM_DIR_SHIFT_DEGREES = 65 util.DROP_MAX_ITEM_DIR_SHIFT_DEGREES = 65
local function calculate_item_drop_dir(pid) ---@param dir_shift? vec2 direction shift for drop; x and y must be clamped to [-0.5, 0.5]
local yaw, pitch = player.get_rot(pid) local function calculate_item_drop_dir(pid, dir_shift)
local vp = gui.get_viewport() if not dir_shift then
local mpos = table.map(input.get_mouse_pos(), function(i, v) dir_shift = { 0, 0 }
return v / vp[i] - 0.5
end)
if not hud.is_inventory_open() and not hud.is_player_inventory_open() then
mpos = { 0, 0 }
end end
local yaw, pitch = player.get_rot(pid)
local q_yaw = quat.from_euler({ 0, yaw, 0 }) local q_yaw = quat.from_euler({ 0, yaw, 0 })
local q_pitch = quat.from_euler({ pitch, 0, 0 }) local q_pitch = quat.from_euler({ pitch, 0, 0 })
local q_rotation = quat.mul(q_yaw, q_pitch) local q_rotation = quat.mul(q_yaw, q_pitch)
local q_shift = quat.from_euler({ local q_shift = quat.from_euler({
-mpos[2] * DROP_MAX_ITEM_DIR_SHIFT_DEGREES * 2, -dir_shift[2] * util.DROP_MAX_ITEM_DIR_SHIFT_DEGREES * 2,
-mpos[1] * DROP_MAX_ITEM_DIR_SHIFT_DEGREES * 2, -dir_shift[1] * util.DROP_MAX_ITEM_DIR_SHIFT_DEGREES * 2,
0, 0,
}) })
quat.mul(q_rotation, q_shift, q_rotation) quat.mul(q_rotation, q_shift, q_rotation)
@ -29,8 +25,9 @@ end
--- Create a drop from slot, set it velocity, and return it --- Create a drop from slot, set it velocity, and return it
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack ---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
---@param dir_shift? vec2 direction shift for drop; x and y must be clamped to [-0.5, 0.5]
---@return table? entity, string? error ---@return table? entity, string? error
function util.drop_from_slot(pid, invid, slot, mode) function util.drop_from_slot(pid, invid, slot, mode, dir_shift)
if invid == 0 then if invid == 0 then
return nil, "invid cannot be 0" return nil, "invid cannot be 0"
end end
@ -55,13 +52,13 @@ function util.drop_from_slot(pid, invid, slot, mode)
local data = inventory.get_all_data(invid, slot) local data = inventory.get_all_data(invid, slot)
local pvel = { player.get_vel(pid) } local pvel = { player.get_vel(pid) }
local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 }) local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 })
local dir = calculate_item_drop_dir(pid) local dir = calculate_item_drop_dir(pid, dir_shift)
local throw_force = vec3.mul(dir, DROP_FORCE) local throw_force = vec3.mul(dir, util.DROP_FORCE)
local drop, err = util.drop(ppos, itemid, drop_itemcount, data, 1.5) local drop, err = util.drop(ppos, itemid, drop_itemcount, data, 1.5)
if not drop then if not drop then
return nil, err return nil, err
end end
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)) local velocity = vec3.add(throw_force, vec3.add(pvel, util.DROP_INIT_VEL))
drop.rigidbody:set_vel(velocity) drop.rigidbody:set_vel(velocity)
return drop return drop
end end

View file

@ -4,7 +4,11 @@ function on_hud_open()
events.on("core:drop_outside_inventory", function(mode) events.on("core:drop_outside_inventory", function(mode)
local pid = hud.get_player() local pid = hud.get_player()
local invid = hud.get_exchange_inventory() local invid = hud.get_exchange_inventory()
base_util.drop_from_slot(pid, invid, 0, mode) local vp = gui.get_viewport()
local dir_shift = table.map(input.get_mouse_pos(), function(i, v)
return v / vp[i] - 0.5
end)
base_util.drop_from_slot(pid, invid, 0, mode, dir_shift)
end) end)
input.add_callback("player.drop", function() input.add_callback("player.drop", function()
if hud.is_paused() or hud.is_inventory_open() then if hud.is_paused() or hud.is_inventory_open() then