summaryrefslogtreecommitdiff
path: root/homedecor/handlers/water_particles.lua
blob: a3334b03cb73ea04a8175b977341cb0270888fb1 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- variables taken by the start... function
--
-- pos and node are as usual, from e.g. on_rightclick.
--
-- in the { particledef } table:
--
-- outletx/y/z are the exact coords of the starting point
--     for the spawner, relative to the center of the node
--
-- velocityx/y/z are the speed of the particles,
--    (x and z are relative to a node placed while looking north/facedir 0)
--    negative Y values flow downward.
--
-- spread is the radius from the starting point,
-- along X and Z only, to randomly spawn particles.
--
-- soundname is the filename (without .ogg) of the sound file
-- to be played along with the particle stream

function homedecor.start_particle_spawner(pos, node, particledef, soundname)

	local this_spawner_meta = minetest.get_meta(pos)
	local id = this_spawner_meta:get_int("active")
	local s_handle = this_spawner_meta:get_int("sound")

	if id ~= 0 then
		if s_handle then
			minetest.after(0, function(s_handle)
				minetest.sound_stop(s_handle)
			end, s_handle)
		end
		minetest.delete_particlespawner(id)
		this_spawner_meta:set_int("active", nil)
		this_spawner_meta:set_int("sound", nil)
		return
	end

	local fdir = node.param2

	if fdir and fdir < 4 and (not id or id == 0) then

		local outletx    = particledef.outlet.x
		local outlety    = particledef.outlet.y
		local outletz    = particledef.outlet.z
		local velocityx  = particledef.velocity_x
		local velocityy  = particledef.velocity_y
		local velocityz  = particledef.velocity_z
		local spread     = particledef.spread

		local minx_t = {  outletx - spread, -outletz - spread, outletx - spread, outletz - spread }
		local maxx_t = {  outletx + spread, -outletz + spread, outletx + spread, outletz + spread }
		local minz_t = { -outletz - spread,  outletx - spread, outletz - spread, outletx - spread }
		local maxz_t = { -outletz + spread,  outletx + spread, outletz + spread, outletx + spread }

		local minvelx_t = { velocityx.min, velocityz.min, -velocityx.max, -velocityz.max }
		local maxvelx_t = { velocityx.max, velocityz.max, -velocityx.min, -velocityz.min }
		local minvelz_t = { velocityz.min, velocityx.min, -velocityz.max,  velocityx.min }
		local maxvelz_t = { velocityz.max, velocityx.max, -velocityz.min,  velocityx.max }

		local minx = minx_t[fdir + 1]
		local maxx = maxx_t[fdir + 1]
		local minz = minz_t[fdir + 1]
		local maxz = maxz_t[fdir + 1]

		local minvelx = minvelx_t[fdir + 1]
		local minvelz = minvelz_t[fdir + 1]
		local maxvelx = maxvelx_t[fdir + 1]
		local maxvelz = maxvelz_t[fdir + 1]

		id = minetest.add_particlespawner({
			amount = 60,
			time = 0,
			collisiondetection = true,
			minpos = {x=pos.x - minx, y=pos.y + outlety, z=pos.z - minz},
			maxpos = {x=pos.x - maxx, y=pos.y + outlety, z=pos.z - maxz},
			minvel = {x = minvelx, y = velocityy, z = minvelz},
			maxvel = {x = maxvelx, y = velocityy, z = maxvelz},
			minacc = {x=0, y=0, z=0},
			maxacc = {x=0, y=-0.05, z=0},
			minexptime = 2,
			maxexptime = 4,
			minsize = 0.5,
			maxsize = 1,
			texture = "homedecor_water_particle.png",
		})
		s_handle = minetest.sound_play(soundname, {
			pos = pos,
			max_hear_distance = 5,
			loop = true
		})
		this_spawner_meta:set_int("active", id)
		this_spawner_meta:set_int("sound", s_handle)
		return
	end
end

function homedecor.stop_particle_spawner(pos)
	local this_spawner_meta = minetest.get_meta(pos)
	local id = this_spawner_meta:get_int("active")
	local s_handle = this_spawner_meta:get_int("sound")

	if id ~= 0 then
		minetest.delete_particlespawner(id)
	end

	if s_handle then
		minetest.after(0, function(s_handle)
			minetest.sound_stop(s_handle)
		end, s_handle)
	end

	this_spawner_meta:set_int("active", nil)
	this_spawner_meta:set_int("sound", nil)
end