refactor: create general player interface.
create utility function for acting on player properties, and refactor code for running and step height to make use of this.
This commit is contained in:
parent
732a8cb88a
commit
1aecdeb752
3 changed files with 50 additions and 33 deletions
|
@ -1,3 +1,5 @@
|
|||
local utils = load_file("util_misc")
|
||||
|
||||
local walk_speed = 1
|
||||
local walk_time = 2
|
||||
local speedup_time = 6
|
||||
|
@ -9,40 +11,34 @@ local jump_factor = 1.5
|
|||
local speed_data = {}
|
||||
|
||||
local function handle_speed (player, delta_time)
|
||||
speed_data[player] = speed_data[player] or {}
|
||||
local data = speed_data[player]
|
||||
utils.handle_player(player, function (player, p_data)
|
||||
speed_data[player] = speed_data[player] or {}
|
||||
local data = speed_data[player]
|
||||
|
||||
-- data.start = data.start or minetest.get_gametime()
|
||||
data.duration = (data.duration or 0) + delta_time
|
||||
data.duration = (data.duration or 0) + delta_time
|
||||
|
||||
local control = player:get_player_control()
|
||||
local control = p_data.control
|
||||
local moving = control.up or control.down or control.left or control.right
|
||||
if not moving then
|
||||
data.duration = data.duration - delta_time * (slowdown_factor + 1)
|
||||
end
|
||||
if data.duration < 0 then data.duration = 0 end
|
||||
if data.duration > walk_time + speedup_time then data.duration = walk_time + speedup_time end
|
||||
|
||||
local moving = control.up or control.down or control.left or control.right
|
||||
if not moving then
|
||||
data.duration = data.duration - delta_time * (slowdown_factor + 1)
|
||||
end
|
||||
if data.duration < 0 then data.duration = 0 end
|
||||
if data.duration > walk_time + speedup_time then data.duration = walk_time + speedup_time end
|
||||
if data.duration < walk_time then
|
||||
p_data.physics.speed = walk_speed
|
||||
p_data.physics.jump = jump_height
|
||||
else
|
||||
local run_time = data.duration - walk_time
|
||||
local t = run_time / speedup_time
|
||||
|
||||
local duration = data.duration
|
||||
local factor = (run_factor - 1) * t + 1
|
||||
if factor > run_factor then factor = run_factor end
|
||||
|
||||
local physics = player:get_physics_override()
|
||||
|
||||
if duration < walk_time then
|
||||
physics.speed = walk_speed
|
||||
physics.jump = jump_height
|
||||
else
|
||||
local run_time = duration - walk_time
|
||||
local t = run_time / speedup_time
|
||||
|
||||
local factor = (run_factor - 1) * t + 1
|
||||
if factor > run_factor then factor = run_factor end
|
||||
|
||||
physics.speed = walk_speed * factor
|
||||
physics.jump = jump_height + (jump_height * jump_factor - 1) * t
|
||||
end
|
||||
|
||||
player:set_physics_override(physics)
|
||||
p_data.physics.speed = walk_speed * factor
|
||||
p_data.physics.jump = jump_height + (jump_height * jump_factor - 1) * t
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
minetest.register_globalstep(function(delta_time)
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
local utils = load_file("util_misc")
|
||||
|
||||
minetest.register_globalstep(function(delta_time)
|
||||
for _, player in pairs(minetest.get_connected_players()) do
|
||||
local control = player:get_player_control()
|
||||
local properties = player:get_properties()
|
||||
properties.stepheight = control.sneak and 0.01 or 1.05
|
||||
player:set_properties(properties)
|
||||
utils.handle_player(player, function (_, data)
|
||||
data.properties.stepheight = data.control.sneak and 0.01 or 1.05
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
|
20
mods/bg_player/util_misc.lua
Normal file
20
mods/bg_player/util_misc.lua
Normal file
|
@ -0,0 +1,20 @@
|
|||
local function handle_player (player, fn)
|
||||
local data = {}
|
||||
|
||||
data.control = player:get_player_control()
|
||||
data.properties = player:get_properties()
|
||||
data.physics = player:get_physics_override()
|
||||
|
||||
local result = fn(player, data)
|
||||
|
||||
player:set_properties(data.properties)
|
||||
player:set_physics_override(data.physics)
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
|
||||
return {
|
||||
handle_player = handle_player,
|
||||
}
|
Loading…
Reference in a new issue