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