From 48a51a77cec8acae8355350d0e229782109f7d34 Mon Sep 17 00:00:00 2001 From: trans_soup <> Date: Thu, 12 Oct 2023 16:19:11 +0200 Subject: [PATCH] add running. create mod for player-related stuff and move hand setup from core mod to this new mod. add feature where, while moving, players gradually speed up up to a certain limit. their jump height is also increased while this happens. --- mods/bg_core/init.lua | 1 - mods/{bg_core => bg_player}/hand.lua | 0 mods/bg_player/init.lua | 2 ++ mods/bg_player/mod.conf | 3 ++ mods/bg_player/run.lua | 52 ++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) rename mods/{bg_core => bg_player}/hand.lua (100%) create mode 100644 mods/bg_player/init.lua create mode 100644 mods/bg_player/mod.conf create mode 100644 mods/bg_player/run.lua diff --git a/mods/bg_core/init.lua b/mods/bg_core/init.lua index 91aaf9b..b4a8a2c 100644 --- a/mods/bg_core/init.lua +++ b/mods/bg_core/init.lua @@ -2,4 +2,3 @@ local modname = minetest.get_current_modname() load_file("node") load_file("biome") -load_file("hand") diff --git a/mods/bg_core/hand.lua b/mods/bg_player/hand.lua similarity index 100% rename from mods/bg_core/hand.lua rename to mods/bg_player/hand.lua diff --git a/mods/bg_player/init.lua b/mods/bg_player/init.lua new file mode 100644 index 0000000..1af3873 --- /dev/null +++ b/mods/bg_player/init.lua @@ -0,0 +1,2 @@ +load_file("hand") +load_file("run") diff --git a/mods/bg_player/mod.conf b/mods/bg_player/mod.conf new file mode 100644 index 0000000..dd0566f --- /dev/null +++ b/mods/bg_player/mod.conf @@ -0,0 +1,3 @@ +name = player +description = blockgame player-related stuff. +depends = core, api diff --git a/mods/bg_player/run.lua b/mods/bg_player/run.lua new file mode 100644 index 0000000..46065c9 --- /dev/null +++ b/mods/bg_player/run.lua @@ -0,0 +1,52 @@ +local walk_speed = 1 +local walk_time = 2 +local speedup_time = 6 +local run_factor = 3 +local slowdown_factor = 8 +local jump_height = 1 +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] + + -- data.start = data.start or minetest.get_gametime() + data.duration = (data.duration or 0) + delta_time + + local control = player:get_player_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 + + local duration = data.duration + + 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 + if t > 1 then t = 1 end + + 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) +end + +minetest.register_globalstep(function(delta_time) + for _, player in pairs(minetest.get_connected_players()) do + handle_speed(player, delta_time) + end +end)