]> git.lizzy.rs Git - crafter_client.git/blob - aether.lua
Fix dtime aether portal timing
[crafter_client.git] / aether.lua
1 --nether teleporters are animation based
2 --the animation must finish before the teleport is initialized
3 local hud_bg_id = nil --aether portal bg
4 local aether_cool_off_timer = 0 --use this to stop players teleporting back and forth
5 local init_sound = nil
6 local teleport_sound = nil
7 local opacity = 0
8
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 aether_cool_off_timer > 0 then
15                 aether_cool_off_timer = aether_cool_off_timer - dtime
16                 if aether_cool_off_timer <= 0 then
17                         aether_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 == "aether:portal" and aether_cool_off_timer == 0 then
27                 if init_sound == nil then
28                         init_sound = minetest.sound_play("aether_teleport",{gain=0})
29                         minetest.sound_fade(init_sound, 0.34, 1)
30                 end
31                 if hud_bg_id == nil then
32                         hud_bg_id = minetest.localplayer:hud_add({
33                                 hud_elem_type = "image", -- see HUD element types, default "text"
34                                 position = {x=0.5, y=0.5},
35                                 name = "",    -- default ""
36                                 scale = {x=-100, y=-100}, -- default {x=0,y=0}
37                                 text = "aether_portal_gui.png^[opacity:"..opacity,    -- default ""
38                         })
39                         
40                 elseif opacity < 255 then
41                         --make the hud fade in
42                         opacity = opacity + (dtime*100)
43                         print(opacity)
44                         
45                         minetest.localplayer:hud_change(hud_bg_id, "text", "aether_portal_gui.png^[opacity:"..opacity)
46                 end
47         elseif hud_bg_id then
48                 --play heavenly sounds
49                 
50                 if init_sound then
51                         minetest.sound_fade(init_sound, -0.4, 0)
52                         init_sound = nil
53                         teleport_sound = minetest.sound_play("aether_teleport_complete",{gain=1})
54                         minetest.sound_fade(teleport_sound, -0.1, 0)
55                         teleport_sound = nil
56                 end
57                 
58                 --player left portal before teleporting
59                 if aether_cool_off_timer == 0 then
60                         opacity = opacity  - (dtime*100)                        
61                         minetest.localplayer:hud_change(hud_bg_id, "text", "aether_portal_gui.png^[opacity:"..opacity)
62                         
63                         if opacity <= 0 then
64                                 minetest.localplayer:hud_remove(hud_bg_id)
65                                 hud_bg_id = nil
66                                 opacity = 0
67                         end
68                 --teleport complete animation
69                 elseif aether_cool_off_timer > 0 then
70                 
71                         opacity = opacity  - (dtime*100)                        
72                         minetest.localplayer:hud_change(hud_bg_id, "text", "aether_portal_gui.png^[opacity:"..opacity)
73                         
74                         if opacity <= 0 then
75                                 minetest.localplayer:hud_remove(hud_bg_id)
76                                 hud_bg_id = nil
77                                 opacity = 0
78                         end
79                 else
80                         init_sound = nil
81                 end
82         elseif hud_bg_id then
83                 minetest.localplayer:hud_remove(hud_bg_id)
84                 hud_bg_id = nil
85                 opacity = 0
86         end
87         
88         --initialize teleport command to server
89         if hud_bg_id and aether_cool_off_timer == 0 then
90                 if opacity >= 255 then
91                         aether:send_all("teleport me")
92                         --can't use any portal for 7 seconds
93                         aether_cool_off_timer = 7  --if you read this, you'll notice the nether cool off timer is 6 and this is 7 ;)
94                 end
95         end
96 end)