summaryrefslogtreecommitdiff
path: root/mods/vzxv_itemstacks/loose.lua
blob: 48488dedd46cb40d9258cfd6203ccaa4145efd4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local surrounding = {
	{0,0,0},
	{0,1,0},
	{0,-1,0},
	{1,0,0},
	{0,0,1},
	{-1,0,0},
	{0,0,-1}
}

minetest.register_entity("vzxv_itemstacks:loose_itemstack",{
    initial_properties = {
		physical = true,
		pointable = false,
        visual = "wielditem",
		visual_size = {x = 0.4, y = 0.4},
		collision_box = {
			type = "fixed",
			fixed = {
				{-0.001,-0.5,-0.001,0.001,0.10,0.001},
			},
		},
	},
	on_activate = function(self,staticdata)
		local luaent = self
		staticdata = staticdata~="" and staticdata or "vzxv:apioform"
		luaent._stack = staticdata
		self.object:set_properties {
			wield_item=ItemStack(luaent._stack):get_name()
		}
	end,
	on_step = function(self,dt)
		local npos = vzxv.round_pos(self.object:get_pos())
		npos.y = npos.y - 1
		if minetest.get_node(npos).name ~= "air" then
			npos.y = npos.y + 1
			for i=1, #surrounding do
				local s = surrounding[i]
				local tpos = {
					x=npos.x+s[1],
					y=npos.y+s[2],
					z=npos.z+s[3]
				}
				if minetest.get_node(tpos).name == "air" then
					vzxv.drop(tpos, ItemStack(self._stack))
					self.object:remove()
					break
				end
			end
		end
	end,
	get_staticdata = function(self)
		local luaent = self
		return luaent._stack
	end
})

vzxv.register_gravity("vzxv_itemstacks:loose_itemstack")

function vzxv.drop_loose(pos,stack)
	minetest.add_entity(pos,"vzxv_itemstacks:loose_itemstack",stack:to_string())
end