Make a stopped cart resume in its last direction

Largely based on PR #10. Thanks to @Hawk777 for this tweak

- Fix player rotation when cart stopped (or punched the first time)
This commit is contained in:
SmallJoker 2016-07-21 13:20:00 +02:00
parent 9d059b8e31
commit 947d8633ff

@ -44,7 +44,7 @@ boost_cart.cart = {
driver = nil, driver = nil,
punched = false, -- used to re-send velocity and position punched = false, -- used to re-send velocity and position
velocity = {x=0, y=0, z=0}, -- only used on punch velocity = {x=0, y=0, z=0}, -- only used on punch
old_dir = {x=0, y=0, z=0}, old_dir = {x=1, y=0, z=0}, -- random value to start the cart on punch
old_pos = nil, old_pos = nil,
old_switch = 0, old_switch = 0,
railtype = nil, railtype = nil,
@ -75,11 +75,15 @@ function boost_cart.cart:on_activate(staticdata, dtime_s)
return return
end end
self.railtype = data.railtype self.railtype = data.railtype
if data.old_dir then
self.old_dir = data.old_dir
end
end end
function boost_cart.cart:get_staticdata() function boost_cart.cart:get_staticdata()
return minetest.serialize({ return minetest.serialize({
railtype = self.railtype railtype = self.railtype,
old_dir = self.old_dir
}) })
end end
@ -92,7 +96,7 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
end end
if not puncher or not puncher:is_player() then if not puncher or not puncher:is_player() then
local cart_dir = boost_cart:get_rail_direction(pos, {x=1, y=0, z=0}, nil, nil, self.railtype) local cart_dir = boost_cart:get_rail_direction(pos, self.old_dir, nil, nil, self.railtype)
if vector.equals(cart_dir, {x=0, y=0, z=0}) then if vector.equals(cart_dir, {x=0, y=0, z=0}) then
return return
end end
@ -147,6 +151,7 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
local f = 3 * (time_from_last_punch / punch_interval) local f = 3 * (time_from_last_punch / punch_interval)
self.velocity = vector.multiply(cart_dir, f) self.velocity = vector.multiply(cart_dir, f)
self.old_dir = cart_dir
self.old_pos = nil self.old_pos = nil
self.punched = true self.punched = true
end end
@ -282,7 +287,9 @@ function boost_cart.cart:on_step(dtime)
self.object:setacceleration(new_acc) self.object:setacceleration(new_acc)
self.old_pos = vector.new(pos) self.old_pos = vector.new(pos)
if not vector.equals(dir, {x=0, y=0, z=0}) then
self.old_dir = vector.new(dir) self.old_dir = vector.new(dir)
end
self.old_switch = last_switch self.old_switch = last_switch
@ -298,6 +305,7 @@ function boost_cart.cart:on_step(dtime)
end end
end end
self.punched = false self.punched = false
update.vel = true -- update player animation
end end
if not (update.vel or update.pos) then if not (update.vel or update.pos) then
@ -305,11 +313,11 @@ function boost_cart.cart:on_step(dtime)
end end
local yaw = 0 local yaw = 0
if dir.x < 0 then if self.old_dir.x < 0 then
yaw = 0.5 yaw = 0.5
elseif dir.x > 0 then elseif self.old_dir.x > 0 then
yaw = 1.5 yaw = 1.5
elseif dir.z < 0 then elseif self.old_dir.z < 0 then
yaw = 1 yaw = 1
end end
self.object:setyaw(yaw * math.pi) self.object:setyaw(yaw * math.pi)