Stop carts from reversing when they stop, make stopped carts try to start moving in the direction the player is facing

This commit is contained in:
teknomunk 2024-04-23 16:34:39 +00:00
parent a7d8b52510
commit ffd3dbc7b1
2 changed files with 20 additions and 10 deletions

View File

@ -21,6 +21,7 @@ assert(handle_cart_enter)
-- Constants -- Constants
local max_step_distance = 0.5 local max_step_distance = 0.5
local MINECART_MAX_HP = 4 local MINECART_MAX_HP = 4
local TWO_OVER_PI = 2 / math.pi
local function detach_driver(self) local function detach_driver(self)
local staticdata = self._staticdata local staticdata = self._staticdata
@ -244,6 +245,7 @@ function DEFAULT_CART_DEF:on_step(dtime)
local controls = {} local controls = {}
if ctrl.up then controls.forward = now_time end if ctrl.up then controls.forward = now_time end
if ctrl.down then controls.brake = now_time end if ctrl.down then controls.brake = now_time end
controls.look = math.round(player:get_look_horizontal() * TWO_OVER_PI) % 4
staticdata.controls = controls staticdata.controls = controls
end end

View File

@ -38,6 +38,16 @@ local function try_detach_minecart(staticdata)
end end
end end
local function reverse_direction(staticdata)
if staticdata.behind or staticdata.ahead then
reverse_train(staticdata)
return
end
mod.reverse_cart_direction(staticdata)
end
--[[ --[[
Array of hooks { {u,v,w}, name } Array of hooks { {u,v,w}, name }
Actual position is pos + u * dir + v * right + w * up Actual position is pos + u * dir + v * right + w * up
@ -248,6 +258,13 @@ local function calculate_acceleration(staticdata)
local time_active = minetest.get_gametime() - 0.25 local time_active = minetest.get_gametime() - 0.25
if (ctrl.forward or 0) > time_active then if (ctrl.forward or 0) > time_active then
if staticdata.velocity == 0 then
local look_dir = minetest.facedir_to_dir(ctrl.look or 0)
local dot = vector.dot(staticdata.dir, look_dir)
if dot < 0 then
reverse_direction(staticdata)
end
end
acceleration = 4 acceleration = 4
elseif (ctrl.brake or 0) > time_active then elseif (ctrl.brake or 0) > time_active then
acceleration = -1.5 acceleration = -1.5
@ -270,15 +287,6 @@ local function calculate_acceleration(staticdata)
return acceleration return acceleration
end end
local function reverse_direction(staticdata)
if staticdata.behind or staticdata.ahead then
reverse_train(staticdata)
return
end
mod.reverse_cart_direction(staticdata)
end
local function do_movement_step(staticdata, dtime) local function do_movement_step(staticdata, dtime)
if not staticdata.connected_at then return 0 end if not staticdata.connected_at then return 0 end
@ -405,7 +413,7 @@ local function do_movement_step(staticdata, dtime)
-- Update cart direction -- Update cart direction
staticdata.dir = next_dir staticdata.dir = next_dir
elseif stops_in_block and v_1 < (FRICTION/5) and a <= 0 then elseif stops_in_block and v_1 < (FRICTION/5) and a <= 0 and staticdata.dir.y > 0 then
-- Handle direction flip due to gravity -- Handle direction flip due to gravity
if DEBUG then mcl_debug("Gravity flipped direction") end if DEBUG then mcl_debug("Gravity flipped direction") end