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