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