forked from Mirrorlandia_minetest/minetest
91 lines
2.2 KiB
Lua
91 lines
2.2 KiB
Lua
|
-- Minetest: builtin/item_entity.lua
|
||
|
|
||
|
function minetest.spawn_item(pos, item)
|
||
|
-- Take item in any format
|
||
|
local stack = ItemStack(item)
|
||
|
local obj = minetest.env:add_entity(pos, "__builtin:item")
|
||
|
obj:get_luaentity():set_item(stack:to_string())
|
||
|
return obj
|
||
|
end
|
||
|
|
||
|
minetest.register_entity("__builtin:item", {
|
||
|
initial_properties = {
|
||
|
hp_max = 1,
|
||
|
physical = true,
|
||
|
collisionbox = {-0.25,-0.25,-0.25, 0.25,0.25,0.25},
|
||
|
visual = "sprite",
|
||
|
visual_size = {x=0.5, y=0.5},
|
||
|
textures = {""},
|
||
|
spritediv = {x=1, y=1},
|
||
|
initial_sprite_basepos = {x=0, y=0},
|
||
|
is_visible = false,
|
||
|
},
|
||
|
|
||
|
itemstring = '',
|
||
|
physical_state = true,
|
||
|
|
||
|
set_item = function(self, itemstring)
|
||
|
self.itemstring = itemstring
|
||
|
local stack = ItemStack(itemstring)
|
||
|
local itemtable = stack:to_table()
|
||
|
local itemname = nil
|
||
|
if itemtable then
|
||
|
itemname = stack:to_table().name
|
||
|
end
|
||
|
local item_texture = nil
|
||
|
if minetest.registered_items[itemname] then
|
||
|
item_texture = minetest.registered_items[itemname].inventory_image
|
||
|
end
|
||
|
item_texture = item_texture or "unknown_item.png"
|
||
|
self.object:set_properties({
|
||
|
textures = {item_texture},
|
||
|
is_visible = true,
|
||
|
})
|
||
|
end,
|
||
|
|
||
|
get_staticdata = function(self)
|
||
|
return self.itemstring
|
||
|
end,
|
||
|
|
||
|
on_activate = function(self, staticdata)
|
||
|
self.itemstring = staticdata
|
||
|
self.object:set_armor_groups({immortal=1})
|
||
|
self.object:setvelocity({x=0, y=2, z=0})
|
||
|
self.object:setacceleration({x=0, y=-10, z=0})
|
||
|
self:set_item(self.itemstring)
|
||
|
end,
|
||
|
|
||
|
on_step = function(self, dtime)
|
||
|
local p = self.object:getpos()
|
||
|
p.y = p.y - 0.3
|
||
|
local nn = minetest.env:get_node(p).name
|
||
|
if minetest.registered_nodes[nn].walkable then
|
||
|
if self.physical_state then
|
||
|
self.object:setvelocity({x=0,y=0,z=0})
|
||
|
self.object:setacceleration({x=0, y=0, z=0})
|
||
|
self.physical_state = false
|
||
|
self.object:set_properties({
|
||
|
physical = false
|
||
|
})
|
||
|
end
|
||
|
else
|
||
|
if not self.physical_state then
|
||
|
self.object:setvelocity({x=0,y=0,z=0})
|
||
|
self.object:setacceleration({x=0, y=-10, z=0})
|
||
|
self.physical_state = true
|
||
|
self.object:set_properties({
|
||
|
physical = true
|
||
|
})
|
||
|
end
|
||
|
end
|
||
|
end,
|
||
|
|
||
|
on_punch = function(self, hitter)
|
||
|
if self.itemstring ~= '' then
|
||
|
hitter:get_inventory():add_item("main", self.itemstring)
|
||
|
end
|
||
|
self.object:remove()
|
||
|
end,
|
||
|
})
|
||
|
|