summaryrefslogtreecommitdiff
path: root/music_player.lua
blob: 0a6474a2a2b53c94f6a6a2e7d5f11a6d3e6d0bd8 (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
116
117
118
119
120
121
122
123
124
125
126
minetest.register_alias("music_player", "technic:music_player")
minetest.register_craft({
	output = 'technic:music_player',
	recipe = {
		{'default:wood', 'default:wood', 'default:wood'},
		{'technic:diamond', 'technic:diamond', 'technic:diamond'},
		{'default:stone', 'moreores:copper_ingot', 'default:stone'},
	}
})

minetest.register_craftitem("technic:music_player", {
	description = "Music Player",
	stack_max = 99,
}) 

music_player_formspec =
	"invsize[8,9;]"..
	"image[1,1;1,2;technic_power_meter_bg.png]"..
	"label[0,0;Music Player]"..
	"label[1,3;Power level]"..
	"button[5,2;1,1;track1;1]"..
	"button[6,2;1,1;track2;2]"
	

minetest.register_node("technic:music_player", {
	description = "Music Player",
	tiles = {"technic_music_player_top.png", "technic_machine_bottom.png", "technic_music_player_side.png",
		"technic_music_player_side.png", "technic_music_player_side.png", "technic_music_player_side.png"},
	groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
	sounds = default.node_sound_wood_defaults(),
	technic_power_machine=1,
	internal_EU_buffer=0,
	internal_EU_buffer_size=5000,
	music_player_on=0,
	music_playing =0,
	music_handle = 0,
	music_player_current_track =1,
	on_construct = function(pos)
		local meta = minetest.env:get_meta(pos)
		meta:set_string("infotext", "Music Player")
		meta:set_float("technic_power_machine", 1)
		meta:set_float("internal_EU_buffer", 1)
		meta:set_float("internal_EU_buffer_size", 5000)
		meta:set_string("formspec", music_player_formspec)
		meta:set_float("music_player_on", 0)
		meta:set_float("music_player_current_track", 1)
		end,	

	on_receive_fields = function(pos, formanme, fields, sender)
	
	local meta = minetest.env:get_meta(pos)
	player_on=meta:get_float("music_player_on")
	music_handle=meta:get_float("music_handle")
	music_player_current_track=meta:get_float("music_player_current_track")
	if fields.track1 then music_player_current_track=1 end
	if fields.track2 then music_player_current_track=2 end
	if fields.track3 then music_player_current_track=3 end
	if fields.track4 then music_player_current_track=4 end
	if fields.track5 then music_player_current_track=5 end
	if fields.track6 then music_player_current_track=6 end
	if fields.track7 then music_player_current_track=7 end
	if fields.track8 then music_player_current_track=8 end
	if fields.track9 then music_player_current_track=9 end
	meta:set_float("music_player_current_track",music_player_current_track)
	if fields.play and player_on==1 then  
	if music_handle then minetest.sound_stop(music_handle) end
	music_handle=minetest.sound_play("technic_track"..music_player_current_track, {pos = pos, gain = 1.0,loop = true, max_hear_distance = 72,}) 	
	meta:set_float("music_playing",1)
	end
	if fields.stop then  
	meta:set_float("music_playing",0)
	if music_handle then minetest.sound_stop(music_handle) end
	end
	meta:set_float("music_handle",music_handle)
	end,
})

minetest.register_abm({
	nodenames = {"technic:music_player"},
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
	local meta = minetest.env:get_meta(pos)
	local charge= meta:get_float("internal_EU_buffer")
	local max_charge= meta:get_float("internal_EU_buffer_size")
	player_on=meta:get_float("music_player_on")
	music_player_current_track=meta:get_float("music_player_current_track")
	local play_cost=80
	
	if charge>play_cost then 
		if meta:get_float("music_playing")==1 then charge=charge-play_cost end
			meta:set_float("internal_EU_buffer",charge)
		meta:set_float("music_player_on",1)
	else 
		meta:set_float("music_playing",0)
		meta:set_float("music_player_on",0)
		if music_handle then minetest.sound_stop(music_handle) end
	end
	local load = math.floor((charge/max_charge)*100)
	meta:set_string("formspec",
				"invsize[8,9;]"..
				"image[1,1;1,2;technic_power_meter_bg.png^[lowpart:"..
						(load)..":technic_power_meter_fg.png]"..
				"label[0,0;Music Player]"..
				"label[1,3;Power level]"..
				"button[4,1;1,1;track1;1]"..
				"button[5,1;1,1;track2;2]"..
				"button[6,1;1,1;track3;3]"..
				"button[4,2;1,1;track4;4]"..
				"button[5,2;1,1;track5;5]"..
				"button[6,2;1,1;track6;6]"..	
				"button[4,3;1,1;track7;7]"..
				"button[5,3;1,1;track8;8]"..
				"button[6,3;1,1;track9;9]"..
				"button[4,4;1,2;play;Play]"..
				"button[6,4;1,2;stop;Stop]"..
				"label[4,0;Current track "..tostring(music_player_current_track).."]"

				)
				
	
	
	
	end
})