]> git.lizzy.rs Git - crafter_client.git/blob - nether.lua
7489fe3d979e82ab8b014c3d9c58410e597d6347
[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 cool_off_timer = 0 --use this to stop players teleporting back and forth
7 local init_sound = nil
8 local teleport_sound = nil
9 minetest.register_globalstep(function(dtime)
10         if not minetest.localplayer or not minetest.camera then
11                 return
12         end
13         --use this for player cooloff timer also to not overload server
14         if cool_off_timer > 0 then
15                 cool_off_timer = cool_off_timer - dtime
16                 if cool_off_timer <= 0 then
17                         cool_off_timer = 0
18                 end
19         end
20         
21         local pos = minetest.localplayer:get_pos()
22         pos.y = pos.y + 0.1
23         
24         local node = minetest.get_node_or_nil(pos)
25         
26         if node and node.name == "nether:portal" and cool_off_timer == 0 then
27                 if hud_bg_id == nil then
28                         hud_bg_id = minetest.localplayer:hud_add({
29                                 hud_elem_type = "image", -- see HUD element types, default "text"
30                                 position = {x=0.5, y=0.5},
31                                 name = "",    -- default ""
32                                 scale = {x=-100, y=-100}, -- default {x=0,y=0}
33                                 text = "darkness.png",    -- default ""
34                         })
35                         hud_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=-1, y=-1}, -- default {x=0,y=0}
40                                 text = "nether_portal_gui.png",    -- default ""
41                         })
42                         init_sound = minetest.sound_play("portal_initialize",{gain=0,pitch=math.random(70,90)/100})
43                         minetest.sound_fade(init_sound, 0.34, 1)
44                         
45                 else
46                         --make the hud zoom in
47                         
48                         local scale = minetest.localplayer:hud_get(hud_id).scale.x
49                         if scale > -100 then
50                                 scale = scale - ((scale/-(1/dtime))*2)
51                         elseif scale < -100 then        
52                                 scale = -100
53                         end
54                         minetest.localplayer:hud_change(hud_id, "scale", {x=scale,y=scale})
55                 end
56         elseif hud_bg_id and hud_id then
57                 --play spooky sounds
58                 if init_sound then
59                         minetest.sound_fade(init_sound, -0.25, 0)
60                         init_sound = nil
61                         teleport_sound = minetest.sound_play("portal_teleported",{gain=1,pitch=math.random(70,90)/100})
62                         minetest.sound_fade(teleport_sound, -0.1, 0)
63                         teleport_sound = nil
64                 end
65                 --make the hud zoom out
66                 local scale = minetest.localplayer:hud_get(hud_id).scale.x
67                 if scale < -1 then
68                         scale = scale + ((scale/-(1/dtime))*2)
69                 elseif scale > -1 then  
70                         scale = -1
71                 end
72                 minetest.localplayer:hud_change(hud_id, "scale", {x=scale,y=scale})
73                 
74                 if scale == -1 then
75                         minetest.localplayer:hud_remove(hud_bg_id)
76                         minetest.localplayer:hud_remove(hud_id)
77                         hud_bg_id = nil
78                         hud_id = nil
79                 end
80         elseif hud_bg_id and hud_id then
81                 minetest.localplayer:hud_remove(hud_bg_id)
82                 minetest.localplayer:hud_remove(hud_id)
83                 hud_bg_id = nil
84                 hud_id = nil
85         end
86         
87         --initialize teleport command to server
88         if hud_bg_id and hud_id and cool_off_timer == 0 then
89                 local scale = minetest.localplayer:hud_get(hud_id).scale.x
90                 if scale == -100 then
91                         nether:send_all("teleport me")
92                         --can't use any portal for 6 seconds
93                         cool_off_timer = 6
94                 end
95         end
96 end)