]> git.lizzy.rs Git - crafter_client.git/blob - player_input.lua
Fix dtime aether portal timing
[crafter_client.git] / player_input.lua
1 --double tap running
2
3 --set up our initial values
4 local state = 0
5 local old_state = 0
6 local running = false
7 local run_discharge_timer = 0
8 local old_up = false
9 local sneak = false
10 local old_sneak = false
11 local bunny_hop = false
12 local old_bunny_hop = false
13
14
15
16 --attempt to tell the server to allow us to run
17 local send_server_movement_state = function(state)
18         player_movement_state:send_all(state)
19         --print(state)
20 end
21
22 --receive the server states
23 minetest.register_on_modchannel_message(function(channel_name, sender, message)
24         if channel_name == "player.player_movement_state" then
25                 running = message
26         end
27 end)
28
29 --check player's input on the "up" key
30 minetest.register_globalstep(function(dtime)
31
32         if not minetest.localplayer then
33                 return
34         end
35         
36         local input = minetest.localplayer:get_control()
37         local vel = minetest.localplayer:get_velocity()
38         local oldvel = minetest.localplayer:get_last_velocity()
39         
40         --cancel running if the player bumps into something
41         if running == true and ((vel.x == 0 and oldvel.x ~= 0) or (vel.z == 0 and oldvel.z ~= 0)) then
42                 running = false
43                 bunny_hop = false
44                 run_discharge_timer = 0
45                 state = 0
46         end
47         
48         --reset the run flag
49         if running == true and (input.up == false or input.sneak == true or input.down == true) then
50                 running = false
51                 bunny_hop = false
52                 state = 0
53         end
54         
55         
56         --check if need to tell server to bunnyhop
57         if running == true and vel.y > 0 and input.jump == true and bunny_hop == false then
58                 state = 2
59                 bunny_hop = true
60         end
61         
62         --stop bunny hopping
63         if bunny_hop == true and input.jump == false and running == true and vel.y == 0 then
64                 bunny_hop = false
65                 state = 1
66         end
67         
68         
69         --half second window to double tap running
70         if run_discharge_timer > 0 then
71                 run_discharge_timer = run_discharge_timer - dtime
72                 if run_discharge_timer <= 0 then
73                         run_discharge_timer = 0
74                 end
75                 --initialize double tap run
76                 if old_up == false and input.up == true and vel.x ~= 0 and vel.z ~= 0 then
77                         run_discharge_timer = 0
78                         running = true
79                         state = 1
80                 end
81         end
82         
83         
84         --check if new input of walking forwards
85         if input.up and input.down == false and input.sneak == false and old_up == false and running == false and run_discharge_timer <= 0 then
86                 run_discharge_timer = 0.2
87         end
88         
89         --add this here so the player can sneak
90         if input.sneak == true then
91                 run_discharge_timer = 0
92                 sneak = true
93                 bunny_hop = false
94                 run = false
95         end
96         
97
98         --set the sneak state
99         if sneak == true and old_sneak == false then
100                 state = 3
101         elseif input.sneak == false and old_sneak == true then
102                 sneak = false
103                 state = 0
104         end
105         
106         --only send if state has changed
107         if state ~= old_state then
108                 send_server_movement_state(tostring(state))
109         end
110         
111         --save old value
112         old_up = input.up
113         old_sneak = input.sneak
114         old_bunny_hop = bunny_hop
115         old_state = state
116 end)