From cd622783368ad4a9c803a221301a0a03d0ce20b1 Mon Sep 17 00:00:00 2001 From: Sumyjkl Date: Mon, 1 Aug 2022 23:01:27 +1000 Subject: [PATCH] improved code quality --- mods/PLAYER/mcl_playerplus/init.lua | 38 +++++++++++++++++------------ 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/mods/PLAYER/mcl_playerplus/init.lua b/mods/PLAYER/mcl_playerplus/init.lua index bf5143271..c6c83dd9a 100644 --- a/mods/PLAYER/mcl_playerplus/init.lua +++ b/mods/PLAYER/mcl_playerplus/init.lua @@ -3,6 +3,7 @@ mcl_playerplus = { } local player_velocity_old = {x=0, y=0, z=0} +local player_position_old = {x=0, y=0, z=0} local get_connected_players = minetest.get_connected_players local dir_to_yaw = minetest.dir_to_yaw local get_item_group = minetest.get_item_group @@ -286,18 +287,20 @@ minetest.register_globalstep(function(dtime) if elytra.active then mcl_player.player_set_animation(player, "fly") - local slowdown_mult = 0.2 -- amount of vel to take per sec - local speedup_mult = 10 -- amount of speed to add based on look dir + local slowdown_mult = 1 -- amount of vel to take per sec + local fall_speed = 40 -- amount to fall down per sec in nodes + local speedup_mult = 15 -- amount of speed to add based on look dir local max_speed = 120 local direction = player:get_look_dir() - local v = player:get_velocity() + local player_vel = player:get_velocity() local direction_mult = clamp(-direction.y*2, -0.5, 0.5) - local speed_mult = clamp(elytra.speed + direction_mult * speedup_mult * dtime, -max_speed, max_speed) - speed_mult = speed_mult - slowdown_mult * speed_mult * dtime -- slow down + + local speed_mult = elytra.speed + direction_mult * speedup_mult * dtime + speed_mult = speed_mult - slowdown_mult * dtime -- slow down speed_mult = math.max(speed_mult, -1) speed_mult = math.min(speed_mult, max_speed) - elytra.speed = speed_mult - vel = direction + elytra.speed = speed_mult -- set the speed so you can keep track of it and add to it + new_vel = direction -- use the facing direction as a base playerphysics.add_physics_factor(player, "gravity", "mcl_playerplus:elytra", 0.1) if elytra.rocketing > 0 then @@ -323,18 +326,20 @@ minetest.register_globalstep(function(dtime) playerphysics.remove_physics_factor(player, "gravity", "mcl_playerplus:elytra") end - vel = vector.multiply(vel, speed_mult) - vel = { - x = clamp(vel.x, -max_speed, max_speed), - y = clamp(vel.y, -max_speed, max_speed), - z = clamp(vel.z, -max_speed, max_speed)} + new_vel = vector.multiply(new_vel, speed_mult) + new_vel = { + x = clamp(new_vel.x, -max_speed, max_speed), + y = clamp(new_vel.y, -max_speed, max_speed), + z = clamp(new_vel.z, -max_speed, max_speed)} -- slow the player down so less spongy movement by applying half the inverse vel -- NOTE: do not set this higher than about 0.7 or the game will get the wrong vel and it will be broken - v = vector.multiply(v, -0.4) - player:add_velocity(v) - vel.y = vel.y - (200 / math.max(speed_mult+1, 1)) * dtime - player:add_velocity(vel) + -- this is far from ideal, but there's no good way to set_velocity on the player + player_vel = vector.multiply(player_vel, -0.4) + player:add_velocity(player_vel) + new_vel.y = new_vel.y - (400 / math.max(speed_mult, 2)) * dtime + new_vel.y = new_vel.y - fall_speed * dtime + player:add_velocity(new_vel) end if wielded_def and wielded_def._mcl_toollike_wield then @@ -350,6 +355,7 @@ minetest.register_globalstep(function(dtime) end player_velocity_old = player:get_velocity() or player:get_player_velocity() + player_position_old = player:get_pos() -- controls right and left arms pitch when shooting a bow or blocking