Call on_punch to pick items (DS)

This commit is contained in:
Hybrid Dog 2017-09-11 14:44:31 +02:00
parent 271a577881
commit 739a62a6c6

@ -29,14 +29,12 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
end end
-- adds the item to the inventory and removes the object -- adds the item to the inventory and removes the object
local function collect_item(inv, item, ent, object, pos) local function collect_item(ent, pos, player)
inv:add_item("main", item)
minetest.sound_play("item_drop_pickup", { minetest.sound_play("item_drop_pickup", {
pos = pos, pos = pos,
gain = pickup_gain, gain = pickup_gain,
}) })
ent.itemstring = "" ent:on_punch(player)
object:remove()
end end
-- opt_get_ent gets the object's luaentity if it can be collected -- opt_get_ent gets the object's luaentity if it can be collected
@ -74,7 +72,8 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
local afterflight local afterflight
if magnet_mode then if magnet_mode then
-- take item or reset velocity after flying a second -- take item or reset velocity after flying a second
function afterflight(object, inv) function afterflight(object, inv, player)
-- TODO: test what happens if player left the game
local ent = opt_get_ent(object) local ent = opt_get_ent(object)
if not ent then if not ent then
return return
@ -82,7 +81,7 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
local item = ItemStack(ent.itemstring) local item = ItemStack(ent.itemstring)
if inv if inv
and inv:room_for_item("main", item) then and inv:room_for_item("main", item) then
collect_item(inv, item, ent, object, object:get_pos()) collect_item(ent, object:get_pos(), player)
else else
object:setvelocity({x=0,y=0,z=0}) object:setvelocity({x=0,y=0,z=0})
ent.physical_state = true ent.physical_state = true
@ -156,14 +155,14 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
if zero_velocity_mode then if zero_velocity_mode then
-- collect one item at a time in zero velocity mode -- collect one item at a time in zero velocity mode
-- to avoid the loud pop -- to avoid the loud pop
collect_item(inv, item, ent, object, pos) collect_item(ent, pos, player)
return true return true
end end
local pos2 = object:getpos() local pos2 = object:getpos()
local distance = vector.distance(pos, pos2) local distance = vector.distance(pos, pos2)
got_item = true got_item = true
if distance <= pickup_radius then if distance <= pickup_radius then
collect_item(inv, item, ent, object, pos) collect_item(ent, pos, player)
else else
local vel = vector.multiply( local vel = vector.multiply(
vector.subtract(pos, pos2), 3) vector.subtract(pos, pos2), 3)
@ -176,7 +175,7 @@ if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
}) })
minetest.after(magnet_time, afterflight, minetest.after(magnet_time, afterflight,
object, inv) object, inv, player)
end end
end end
end end