]> git.lizzy.rs Git - crafter_client.git/blob - nether.lua
Add slightly better nether teleport animation
[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 minetest.register_globalstep(function(dtime)
11         if not minetest.localplayer or not minetest.camera then
12                 return
13         end
14         --use this for player cooloff timer also to not overload server
15         if cool_off_timer > 0 then
16                 cool_off_timer = cool_off_timer - dtime
17                 if cool_off_timer <= 0 then
18                         cool_off_timer = 0
19                 end
20         end
21         
22         local pos = minetest.localplayer:get_pos()
23         pos.y = pos.y + 0.1
24         
25         local node = minetest.get_node_or_nil(pos)
26         
27         if node and node.name == "nether:portal" and cool_off_timer == 0 then
28                 if hud_bg_id == nil then
29                         hud_bg_id = minetest.localplayer:hud_add({
30                                 hud_elem_type = "image", -- see HUD element types, default "text"
31                                 position = {x=0.5, y=0.5},
32                                 name = "",    -- default ""
33                                 scale = {x=-100, y=-100}, -- default {x=0,y=0}
34                                 text = "darkness.png",    -- default ""
35                         })
36                         hud_id = minetest.localplayer:hud_add({
37                                 hud_elem_type = "image", -- see HUD element types, default "text"
38                                 position = {x=0.5, y=0.5},
39                                 name = "",    -- default ""
40                                 scale = {x=-1, y=-1}, -- default {x=0,y=0}
41                                 text = "nether_portal_gui.png",    -- default ""
42                         })
43                         nether_id = minetest.localplayer:hud_add({
44                                 hud_elem_type = "image", -- see HUD element types, default "text"
45                                 position = {x=0.5, y=0.5},
46                                 name = "",    -- default ""
47                                 scale = {x=0, y=0}, -- default {x=0,y=0}
48                                 text = "darkness.png",    -- default ""
49                         })
50                         init_sound = minetest.sound_play("portal_initialize",{gain=0,pitch=math.random(70,90)/100})
51                         minetest.sound_fade(init_sound, 0.34, 1)
52                         
53                 else
54                         --make the hud zoom in
55                         local scale = minetest.localplayer:hud_get(hud_id).scale.x
56                         if scale > -100 then
57                                 scale = scale - ((scale/-(1/dtime))*2)
58                         elseif scale < -100 then        
59                                 scale = -100
60                         end
61                         minetest.localplayer:hud_change(hud_id, "scale", {x=scale,y=scale})
62                 end
63         elseif hud_bg_id and hud_id then
64                 --play spooky sounds
65                 if init_sound then
66                         minetest.sound_fade(init_sound, -0.25, 0)
67                         init_sound = nil
68                         teleport_sound = minetest.sound_play("portal_teleported",{gain=1,pitch=math.random(70,90)/100})
69                         minetest.sound_fade(teleport_sound, -0.1, 0)
70                         teleport_sound = nil
71                 end
72                 --player left portal before teleporting
73                 if cool_off_timer == 0 then
74                         --make the hud zoom out
75                         local scale = minetest.localplayer:hud_get(hud_id).scale.x
76                         if scale < -1 then
77                                 scale = scale + ((scale/-(1/dtime))*2)
78                         elseif scale > -1 then  
79                                 scale = -1
80                         end
81                         minetest.localplayer:hud_change(hud_id, "scale", {x=scale,y=scale})
82                         
83                         if scale == -1 then
84                                 minetest.localplayer:hud_remove(hud_bg_id)
85                                 minetest.localplayer:hud_remove(hud_id)
86                                 minetest.localplayer:hud_remove(nether_id)
87                                 nether_id = nil
88                                 hud_bg_id = nil
89                                 hud_id = nil
90                         end
91                 --teleport complete animation
92                 else
93                         local scale = minetest.localplayer:hud_get(nether_id).scale.x
94                         if scale == 0 then scale = -1 end
95                         if scale > -100 then
96                                 scale = scale - ((scale/-(1/dtime))*2)
97                         elseif scale < -100 then        
98                                 scale = -100
99                         end
100                         minetest.localplayer:hud_change(nether_id, "scale", {x=scale,y=scale})
101                         if scale == -100 then
102                                 minetest.localplayer:hud_remove(hud_bg_id)
103                                 minetest.localplayer:hud_remove(hud_id)
104                                 minetest.localplayer:hud_remove(nether_id)
105                                 nether_id = nil
106                                 hud_bg_id = nil
107                                 hud_id = nil
108                         end
109                 end
110         elseif hud_bg_id and hud_id then
111                 minetest.localplayer:hud_remove(hud_bg_id)
112                 minetest.localplayer:hud_remove(hud_id)
113                 minetest.localplayer:hud_remove(nether_id)
114                 nether_id = nil
115                 hud_bg_id = nil
116                 hud_id = nil
117         end
118         
119         --initialize teleport command to server
120         if hud_bg_id and hud_id and cool_off_timer == 0 then
121                 local scale = minetest.localplayer:hud_get(hud_id).scale.x
122                 if scale == -100 then
123                         nether:send_all("teleport me")
124                         --can't use any portal for 6 seconds
125                         cool_off_timer = 6
126                 end
127         end
128 end)