]> git.lizzy.rs Git - crafter_client.git/blob - nether.lua
Attempt 57 at stopping error 23
[crafter_client.git] / nether.lua
1 --nether teleporters are animation based
2 --the animation must finish before the teleport is initialized
3
4 local hud_id = nil --foreground (portal)
5 local hud_bg_id = nil --black background
6 local nether_id = nil --the nether incoming
7 local 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 cool_off_timer > 0 then
18                 cool_off_timer = cool_off_timer - dtime
19                 if cool_off_timer <= 0 then
20                         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 cool_off_timer == 0 then
30                 if init_sound == nil then
31                         init_sound = minetest.sound_play("portal_initialize",{gain=0,pitch=math.random(70,90)/100})
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 = "darkness.png^[opacity:"..opacity,    -- default ""
41                         })
42                         hud_id = minetest.localplayer:hud_add({
43                                 hud_elem_type = "image", -- see HUD element types, default "text"
44                                 position = {x=0.5, y=0.5},
45                                 name = "",    -- default ""
46                                 scale = {x=-1, y=-1}, -- default {x=0,y=0}
47                                 text = "nether_portal_gui.png^[opacity:"..opacity,    -- default ""
48                         })
49                         nether_id = minetest.localplayer:hud_add({
50                                 hud_elem_type = "image", -- see HUD element types, default "text"
51                                 position = {x=0.5, y=0.5},
52                                 name = "",    -- default ""
53                                 scale = {x=0, y=0}, -- default {x=0,y=0}
54                                 text = "darkness.png^[opacity:"..opacity,    -- default ""
55                         })
56                 else
57                         --make the hud zoom in
58                         local scale = minetest.localplayer:hud_get(hud_id).scale.x
59                         if scale > -100 then
60                                 scale = scale - ((scale/-(1/dtime))*2)
61                         elseif scale < -100 then        
62                                 scale = -100
63                         end
64                         opacity = -scale * 2.55
65                         minetest.localplayer:hud_change(hud_id, "scale", {x=scale,y=scale})
66                         minetest.localplayer:hud_change(hud_id, "text", "nether_portal_gui.png^[opacity:"..opacity)
67                         
68                         minetest.localplayer:hud_change(hud_bg_id, "text", "darkness.png^[opacity:"..opacity)
69                         
70                         minetest.localplayer:hud_change(nether_id, "text", "darkness.png^[opacity:"..opacity)
71                 end
72         elseif hud_bg_id and hud_id then
73                 --play spooky sounds
74                 if init_sound then
75                         minetest.sound_fade(init_sound, -0.25, 0)
76                         init_sound = nil
77                         teleport_sound = minetest.sound_play("portal_teleported",{gain=1,pitch=math.random(70,90)/100})
78                         minetest.sound_fade(teleport_sound, -0.1, 0)
79                         teleport_sound = nil
80                 end
81                 --player left portal before teleporting
82                 if cool_off_timer == 0 then
83                         --make the hud zoom out
84                         local scale = minetest.localplayer:hud_get(hud_id).scale.x
85                         if scale < -1 then
86                                 scale = scale + ((scale/-(1/dtime))*2)
87                         elseif scale > -1 then  
88                                 scale = -1
89                         end
90                         opacity = -scale * 2.55
91                         minetest.localplayer:hud_change(hud_id, "scale", {x=scale,y=scale})
92                         
93                         minetest.localplayer:hud_change(hud_id, "text", "nether_portal_gui.png^[opacity:"..opacity)
94                         
95                         minetest.localplayer:hud_change(hud_bg_id, "text", "darkness.png^[opacity:"..opacity)
96                         
97                         minetest.localplayer:hud_change(nether_id, "text", "darkness.png^[opacity:"..opacity)
98                         
99                         if scale == -1 then
100                                 minetest.localplayer:hud_remove(hud_bg_id)
101                                 minetest.localplayer:hud_remove(hud_id)
102                                 minetest.localplayer:hud_remove(nether_id)
103                                 nether_id = nil
104                                 hud_bg_id = nil
105                                 hud_id = nil
106                                 opacity = 0
107                         end
108                 --teleport complete animation
109                 elseif cool_off_timer > 0 then
110                         local scale = minetest.localplayer:hud_get(nether_id).scale.x
111                         if scale == 0 then scale = -1 end
112                         if scale > -100 then
113                                 scale = scale - ((scale/-(1/dtime))*2)
114                         elseif scale < -100 then        
115                                 scale = -100
116                         end
117                         
118                         opacity = (100+scale) * 2.55
119                         minetest.localplayer:hud_change(nether_id, "scale", {x=scale,y=scale})
120                         
121                         
122                         minetest.localplayer:hud_change(hud_id, "text", "nether_portal_gui.png^[opacity:"..opacity)
123                         
124                         minetest.localplayer:hud_change(hud_bg_id, "text", "darkness.png^[opacity:"..opacity)
125                         
126                         minetest.localplayer:hud_change(nether_id, "text", "darkness.png^[opacity:"..opacity)
127                         
128                         
129                         if scale == -100 then
130                                 minetest.localplayer:hud_remove(hud_bg_id)
131                                 minetest.localplayer:hud_remove(hud_id)
132                                 minetest.localplayer:hud_remove(nether_id)
133                                 nether_id = nil
134                                 hud_bg_id = nil
135                                 hud_id = nil
136                                 opacity = 0
137                         end
138                 else
139                         init_sound = nil
140                 end
141         elseif hud_bg_id and hud_id then
142                 minetest.localplayer:hud_remove(hud_bg_id)
143                 minetest.localplayer:hud_remove(hud_id)
144                 minetest.localplayer:hud_remove(nether_id)
145                 nether_id = nil
146                 hud_bg_id = nil
147                 hud_id = nil
148         end
149         
150         --initialize teleport command to server
151         if hud_bg_id and hud_id and cool_off_timer == 0 then
152                 local scale = minetest.localplayer:hud_get(hud_id).scale.x
153                 if scale == -100 then
154                         nether:send_all("teleport me")
155                         --can't use any portal for 6 seconds
156                         cool_off_timer = 6
157                 end
158         end
159 end)