]> git.lizzy.rs Git - crafter_client.git/blob - aether.lua
Update README.md
[crafter_client.git] / aether.lua
1 local minetest,name = minetest,minetest.localplayer:get_name()
2 local aether = minetest.mod_channel_join(name..":aether_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 aether_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 aether_cool_off_timer > 0 then
18                 aether_cool_off_timer = aether_cool_off_timer - dtime
19                 if aether_cool_off_timer <= 0 then
20                         aether_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 == "aether:portal" and aether_cool_off_timer == 0 then
30                 if init_sound == nil then
31                         init_sound = minetest.sound_play("aether_teleport",{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 = "aether_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", "aether_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 == "aether:portal" then
53                         minetest.sound_fade(init_sound, -0.4, 0)
54                         init_sound = nil
55                         teleport_sound = minetest.sound_play("aether_teleport_complete",{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 aether_cool_off_timer == 0 then
62                         opacity = opacity  - (dtime*100)                        
63                         minetest.localplayer:hud_change(hud_bg_id, "text", "aether_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 aether_cool_off_timer > 0 then
77                 
78                         opacity = opacity  - (dtime*100)                        
79                         minetest.localplayer:hud_change(hud_bg_id, "text", "aether_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 aether_cool_off_timer == 0 then
97                 if opacity >= 255 then
98                         aether:send_all("teleport me")
99                         --can't use any portal for 7 seconds
100                         aether_cool_off_timer = 7  --if you read this, you'll notice the nether cool off timer is 6 and this is 7 ;)
101                 end
102         end
103 end)