Fix undefined global warning, move player off to the side of a cart when dismounting so trains don't get pushed apart when getting out

This commit is contained in:
teknomunk 2024-04-09 08:14:30 +00:00
parent a68e973cac
commit 435cd88ceb
3 changed files with 21 additions and 2 deletions

View File

@ -21,9 +21,10 @@ assert(handle_cart_enter)
-- Constants
local max_step_distance = 0.5
local MINECART_MAX_HP = 4
local PASSENGER_ATTACH_POSITION = vector.new(0, -1.75, 0)
local function detach_driver(self)
local staticdata = self._staticdata
if not self._driver then
return
end
@ -45,9 +46,22 @@ local function detach_driver(self)
-- Detatch the player object from the minecart
local player = minetest.get_player_by_name(driver_name)
if player then
local dir = staticdata.dir or vector.new(1,0,0)
local cart_pos = mod.get_cart_position(staticdata) or self.object:get_pos()
local new_pos = vector.offset(cart_pos, -dir.z, 0, dir.x)
player:set_detach()
print("placing player at "..tostring(new_pos).." from cart at "..tostring(cart_pos)..", old_pos="..tostring(player:get_pos()).."dir="..tostring(dir))
-- There needs to be a delay here or the player's position won't update
minetest.after(0.1,function(driver_name,new_pos)
local player = minetest.get_player_by_name(driver_name)
player:moveto(new_pos, false)
end, driver_name, new_pos)
player:set_eye_offset(vector.new(0,0,0),vector.new(0,0,0))
mcl_player.player_set_animation(player, "stand" , 30)
else
print("No player object found for "..driver_name)
end
end

View File

@ -1,6 +1,10 @@
local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)
local mcl_log = mcl_util.make_mcl_logger("mcl_logging_minecarts", "Minecarts")
local mod = mcl_minecarts
-- Imports
local PASSENGER_ATTACH_POSITION = mod.PASSENGER_ATTACH_POSITION
local function activate_normal_minecart(self)
detach_driver(self)
@ -12,7 +16,7 @@ local function activate_normal_minecart(self)
end
end
mcl_minecarts.register_minecart({
mod.register_minecart({
itemstring = "mcl_minecarts:minecart",
craft = {
output = "mcl_minecarts:minecart",

View File

@ -10,6 +10,7 @@ mod.check_float_time = 15
mod.FRICTION = 0.4
mod.MAX_TRAIN_LENGTH = 4
mod.CART_BLOCK_SIZE = 64
mod.PASSENGER_ATTACH_POSITION = vector.new(0, -1.75, 0)
for _,filename in pairs({"storage","functions","rails","train","carts"}) do
dofile(modpath.."/"..filename..".lua")