]> git.lizzy.rs Git - crafter_client.git/blob - player_input.lua
Downgrade to stable version
[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 --0 is nothing
15 --1 is up
16 --2 is down
17 --4 is left
18 --8 is right
19 --16 is jump
20 --32 is auxilary
21 --64 is sneak
22 --128 is left click
23 --256 is right click
24
25 --make the data from get_key_pressed usable
26 --Thanks Thou shalt use my mods!
27 function minetest.get_control_bits(player)
28         local input = player:get_key_pressed()
29         local input_table = {}
30         --iterate through the table using the highest value first
31         local keys = {"rightclick","leftclick","sneak","aux","jump","right","left","down","up"}
32         for index,data in pairs(keys) do
33                 local modifier = math.pow(2, 9-index)
34                 if input >= modifier then
35                         input_table[data] = true
36                         input = input - modifier
37                 else
38                         input_table[data] = false
39                 end
40         end
41         return(input_table)
42 end
43
44
45
46 --attempt to tell the server to allow us to run
47 local send_server_movement_state = function(state)
48         player_movement_state:send_all(state)
49         --print(state)
50 end
51
52 --receive the server states
53 minetest.register_on_modchannel_message(function(channel_name, sender, message)
54         if channel_name == "player.player_movement_state" then
55                 running = message
56         end
57 end)
58
59 --check player's input on the "up" key
60 minetest.register_globalstep(function(dtime)
61
62         if not minetest.localplayer then
63                 return
64         end
65         
66         --save this for the 5.3.0 version
67         --local input = minetest.localplayer:get_control()
68         local input = minetest.get_control_bits(minetest.localplayer)
69         local vel = minetest.localplayer:get_velocity()
70         local oldvel = minetest.localplayer:get_last_velocity()
71         
72         --cancel running if the player bumps into something
73         --save this for 5.3.0
74         --[[
75         if running == true and ((vel.x == 0 and oldvel.x ~= 0) or (vel.z == 0 and oldvel.z ~= 0)) then
76                 running = false
77                 bunny_hop = false
78                 run_discharge_timer = 0
79                 state = 0
80         end
81         ]]--
82         
83         --reset the run flag
84         if running == true and (input.up == false or input.sneak == true or input.down == true) then
85                 running = false
86                 bunny_hop = false
87                 state = 0
88         end
89         
90         
91         --check if need to tell server to bunnyhop
92         if running == true and vel.y > 0 and input.jump == true and bunny_hop == false then
93                 state = 2
94                 bunny_hop = true
95         end
96         
97         --stop bunny hopping
98         if bunny_hop == true and input.jump == false and running == true and vel.y == 0 then
99                 bunny_hop = false
100                 state = 1
101         end
102         
103         
104         --half second window to double tap running
105         if run_discharge_timer > 0 then
106                 run_discharge_timer = run_discharge_timer - dtime
107                 if run_discharge_timer <= 0 then
108                         run_discharge_timer = 0
109                 end
110                 --initialize double tap run
111                 if old_up == false and input.up == true and vel.x ~= 0 and vel.z ~= 0 then
112                         run_discharge_timer = 0
113                         running = true
114                         state = 1
115                 end
116         end
117         
118         
119         --check if new input of walking forwards
120         if input.up and input.down == false and input.sneak == false and old_up == false and running == false and run_discharge_timer <= 0 then
121                 run_discharge_timer = 0.2
122         end
123         
124         --add this here so the player can sneak
125         if input.sneak == true then
126                 run_discharge_timer = 0
127                 sneak = true
128                 bunny_hop = false
129                 run = false
130         end
131         
132
133         --set the sneak state
134         if sneak == true and old_sneak == false then
135                 state = 3
136         elseif input.sneak == false and old_sneak == true then
137                 sneak = false
138                 state = 0
139         end
140         
141         --only send if state has changed
142         if state ~= old_state then
143                 send_server_movement_state(tostring(state))
144         end
145         
146         --save old value
147         old_up = input.up
148         old_sneak = input.sneak
149         old_bunny_hop = bunny_hop
150         old_state = state
151 end)