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