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