implement actual items dropping

This commit is contained in:
MihailRis
2024-07-02 23:36:38 +03:00
parent 89afcd15db
commit 5b154b57b3
4 changed files with 22 additions and 15 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ local rig = entity.modeltree
inair = true
ready = false
local item = ARGS.item
local rotation = mat4.rotate({0, 1, 0}, math.random() * 360)
mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation)
@@ -24,7 +25,7 @@ end
function on_trigger_enter(index, oid)
if ready and oid == 0 then
entity:despawn()
inventory.add(player.get_inventory(oid), item.index("base:stone.item"), 1)
inventory.add(player.get_inventory(oid), item.id, item.count)
audio.play_sound_2d("events/pickup", 0.5, 0.8+math.random()*0.4, "regular")
end
end
+16 -13
View File
@@ -3,19 +3,22 @@ local DROP_INIT_VEL = {0, 3, 0}
function on_hud_open()
input.add_callback("player.drop", function ()
for i=1,80 do
local pid = hud.get_player()
local pvel = {player.get_vel(pid)}
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
local throw_force = vec3.mul(vec3.add(player.get_dir(pid),
{
(math.random() - 0.5) * 1,
(math.random() - 0.5) * 1,
(math.random() - 0.5) * 1
}), DROP_FORCE)
local drop = entities.spawn("base:drop", ppos)
drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
local pid = hud.get_player()
local invid, slot = player.get_inventory(pid)
local itemid, itemcount = inventory.get(invid, slot)
if itemid == 0 then
return
end
inventory.set(invid, slot, itemid, itemcount-1)
local pvel = {player.get_vel(pid)}
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
local drop = entities.spawn("base:drop", ppos, {item={
id=itemid,
count=1
}})
drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
end)
end