]> git.lizzy.rs Git - crafter_client.git/blob - nether.lua
Update README.md
[crafter_client.git] / nether.lua
1 local minetest,math,name = minetest,math,minetest.localplayer:get_name()
2 local nether = minetest.mod_channel_join(name..":nether_teleporters")
3
4 --nether teleporters are animation based
5 --the animation must finish before the teleport is initialized
6 local hud_bg_id = nil --aether portal bg
7 local nether_cool_off_timer = 0 --use this to stop players teleporting back and forth
8 local init_sound = nil
9 local teleport_sound = nil
10 local opacity = 0
11
12 minetest.register_globalstep(function(dtime)
13         if not minetest.localplayer or not minetest.camera then
14                 return
15         end
16         --use this for player cooloff timer also to not overload server
17         if nether_cool_off_timer > 0 then
18                 nether_cool_off_timer = nether_cool_off_timer - dtime
19                 if nether_cool_off_timer <= 0 then
20                         nether_cool_off_timer = 0
21                 end
22         end
23         
24         local pos = minetest.localplayer:get_pos()
25         pos.y = pos.y + 0.1
26         
27         local node = minetest.get_node_or_nil(pos)
28         
29         if node and node.name == "nether:portal" and nether_cool_off_timer == 0 then
30                 if init_sound == nil then
31                         init_sound = minetest.sound_play("portal_initialize",{gain=0})
32                         minetest.sound_fade(init_sound, 0.34, 1)
33                 end
34                 if hud_bg_id == nil then
35                         hud_bg_id = minetest.localplayer:hud_add({
36                                 hud_elem_type = "image", -- see HUD element types, default "text"
37                                 position = {x=0.5, y=0.5},
38                                 name = "",    -- default ""
39                                 scale = {x=-100, y=-100}, -- default {x=0,y=0}
40                                 text = "nether_portal_gui.png^[opacity:"..opacity,    -- default ""
41                         })
42                         
43                 elseif opacity < 255 then
44                         --make the hud fade in
45                         opacity = opacity + (dtime*100)
46                         
47                         minetest.localplayer:hud_change(hud_bg_id, "text", "nether_portal_gui.png^[opacity:"..opacity)
48                 end
49         elseif hud_bg_id then
50                 --play heavenly sounds
51                 
52                 if init_sound and node and node.name == "nether:portal" then
53                         minetest.sound_fade(init_sound, -0.4, 0)
54                         init_sound = nil
55                         teleport_sound = minetest.sound_play("portal_teleported",{gain=1})
56                         minetest.sound_fade(teleport_sound, -0.1, 0)
57                         teleport_sound = nil
58                 end
59                 
60                 --player left portal before teleporting
61                 if nether_cool_off_timer == 0 then
62                         opacity = opacity  - (dtime*100)                        
63                         minetest.localplayer:hud_change(hud_bg_id, "text", "nether_portal_gui.png^[opacity:"..opacity)
64                         
65                         if init_sound then
66                                 minetest.sound_fade(init_sound, -0.4, 0)
67                                 init_sound = nil
68                         end
69                         
70                         if opacity <= 0 then
71                                 minetest.localplayer:hud_remove(hud_bg_id)
72                                 hud_bg_id = nil
73                                 opacity = 0
74                         end
75                 --teleport complete animation
76                 elseif nether_cool_off_timer > 0 then
77                 
78                         opacity = opacity  - (dtime*100)                        
79                         minetest.localplayer:hud_change(hud_bg_id, "text", "nether_portal_gui.png^[opacity:"..opacity)
80                         
81                         if opacity <= 0 then
82                                 minetest.localplayer:hud_remove(hud_bg_id)
83                                 hud_bg_id = nil
84                                 opacity = 0
85                         end
86                 else
87                         init_sound = nil
88                 end
89         elseif hud_bg_id then
90                 minetest.localplayer:hud_remove(hud_bg_id)
91                 hud_bg_id = nil
92                 opacity = 0
93         end
94         
95         --initialize teleport command to server
96         if hud_bg_id and nether_cool_off_timer == 0 then
97                 if opacity >= 255 then
98                         nether:send_all("teleport me")
99                         --can't use any portal for 7 seconds
100                         nether_cool_off_timer = 6  --if you read this, you'll notice the nether cool off timer is 6 and this is 7 ;)
101                         minetest.after(1,function()
102                                 local after_newpos = minetest.localplayer:get_pos().y
103                                 if after_newpos < -10000 and after_newpos > -20000 then
104                                         --cancel old songs
105                                         if current_song then
106                                                 minetest.sound_fade(current_song,-0.4,0)
107                                         end
108
109                                         minetest.after(math.random(3,5)+math.random(),function()
110                                                 if after_newpos < -10000 and after_newpos > -20000 then
111                                                         --backup in case server lags out
112                                                         if current_song then
113                                                                 minetest.sound_fade(current_song,-0.4,0)
114                                                         end
115                                                         local song = 90000+math.random(0,1)
116                                                         --print(song)
117                                                         song_playing = song_table[song].name
118                                                         current_song = minetest.sound_play(song_table[song].name,{gain=song_volume})
119                                                         song_index = song
120                                                         
121                                                 end
122                                         end)
123                                 elseif song_playing and (song_index == 90000 or song_index == 90001) then
124                                         minetest.sound_fade(current_song,-0.4,0)
125                                         song_playing = nil
126                                         song_index = nil
127                                         song_tick = 0
128                                 end
129                         end)
130                 end
131         end
132 end)