mirror of
https://github.com/SmallJoker/boost_cart.git
synced 2024-11-30 18:13:44 +01:00
Allow to stop cart by punching against the moving direction
This commit is contained in:
parent
573d56de1a
commit
b6d17c1b34
92
init.lua
92
init.lua
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
boost_cart = {}
|
boost_cart = {}
|
||||||
boost_cart.modpath = minetest.get_modpath("boost_cart")
|
boost_cart.modpath = minetest.get_modpath("boost_cart")
|
||||||
boost_cart.speed_max = 11
|
boost_cart.speed_max = 10
|
||||||
|
|
||||||
function vector.floor(v)
|
function vector.floor(v)
|
||||||
return {
|
return {
|
||||||
@ -25,7 +25,7 @@ boost_cart.cart = {
|
|||||||
textures = {"cart.png"},
|
textures = {"cart.png"},
|
||||||
|
|
||||||
driver = nil,
|
driver = nil,
|
||||||
punch = false, -- used to re-send velocity and position
|
update = 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=0, y=0, z=0},
|
||||||
old_pos = nil,
|
old_pos = nil,
|
||||||
@ -81,62 +81,47 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
|
|||||||
|
|
||||||
local vel = self.object:getvelocity()
|
local vel = self.object:getvelocity()
|
||||||
if puncher:get_player_name() == self.driver then
|
if puncher:get_player_name() == self.driver then
|
||||||
if math.abs(vel.x) + math.abs(vel.z) > 6 then
|
if math.abs(vel.x + vel.z) > 7 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local cart_dir = boost_cart:velocity_to_dir(direction)
|
local punch_dir = boost_cart:velocity_to_dir(puncher:get_look_dir())
|
||||||
if cart_dir.x == 0 and cart_dir.z == 0 then
|
punch_dir.y = 0
|
||||||
local fd = minetest.dir_to_facedir(puncher:get_look_dir())
|
local cart_dir = boost_cart:get_rail_direction(self.object:getpos(), punch_dir)
|
||||||
if fd == 0 then
|
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
|
||||||
cart_dir.x = 1
|
|
||||||
elseif fd == 1 then
|
|
||||||
cart_dir.z = -1
|
|
||||||
elseif fd == 2 then
|
|
||||||
cart_dir.x = -1
|
|
||||||
elseif fd == 3 then
|
|
||||||
cart_dir.z = 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if time_from_last_punch > tool_capabilities.full_punch_interval then
|
|
||||||
time_from_last_punch = tool_capabilities.full_punch_interval
|
|
||||||
end
|
|
||||||
local dir = boost_cart:get_rail_direction(self.object:getpos(), cart_dir)
|
|
||||||
|
|
||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local f = 4 * (time_from_last_punch / tool_capabilities.full_punch_interval)
|
time_from_last_punch = math.min(time_from_last_punch, tool_capabilities.full_punch_interval)
|
||||||
vel.x = dir.x * f
|
local f = 3 * (time_from_last_punch / tool_capabilities.full_punch_interval)
|
||||||
vel.y = dir.y * f
|
|
||||||
vel.z = dir.z * f
|
self.velocity = vector.multiply(cart_dir, f)
|
||||||
self.velocity = vel
|
|
||||||
self.old_pos = nil
|
self.old_pos = nil
|
||||||
self.punch = true
|
self.update = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart.cart:on_step(dtime)
|
function boost_cart.cart:on_step(dtime)
|
||||||
local vel = self.object:getvelocity()
|
local vel = self.object:getvelocity()
|
||||||
local is_realpunch = self.punch
|
local send_pos = (not self.update)
|
||||||
if self.punch then
|
if self.update then
|
||||||
vel = vector.add(vel, self.velocity)
|
vel = vector.add(vel, self.velocity)
|
||||||
self.velocity = {x=0, y=0, z=0}
|
self.object:setvelocity(vel)
|
||||||
elseif vector.equals(vel, {x=0, y=0, z=0}) then
|
elseif vector.equals(vel, {x=0, y=0, z=0}) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local dir, last_switch = nil, nil
|
local dir, last_switch = nil, nil
|
||||||
local pos = self.object:getpos()
|
local pos = self.object:getpos()
|
||||||
if self.old_pos and not self.punch then
|
if self.old_pos and not self.update then
|
||||||
local flo_pos = vector.floor(pos)
|
local flo_pos = vector.floor(pos)
|
||||||
local flo_old = vector.floor(self.old_pos)
|
local flo_old = vector.floor(self.old_pos)
|
||||||
if vector.equals(flo_pos, flo_old) then
|
if vector.equals(flo_pos, flo_old) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local update = false
|
||||||
local ctrl = nil
|
local ctrl = nil
|
||||||
if self.driver then
|
if self.driver then
|
||||||
local player = minetest.get_player_by_name(self.driver)
|
local player = minetest.get_player_by_name(self.driver)
|
||||||
@ -153,7 +138,7 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
||||||
dir = false
|
dir = false
|
||||||
pos = vector.new(expected_pos)
|
pos = vector.new(expected_pos)
|
||||||
self.punch = true
|
update = true
|
||||||
end
|
end
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -164,17 +149,12 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
for _,v in ipairs({"x", "z"}) do
|
for _,v in ipairs({"x", "z"}) do
|
||||||
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
|
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
|
||||||
vel[v] = 0
|
vel[v] = 0
|
||||||
self.punch = true
|
update = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local cart_dir = {
|
local cart_dir = boost_cart:velocity_to_dir(vel)
|
||||||
x = boost_cart:get_sign(vel.x),
|
|
||||||
y = boost_cart:get_sign(vel.y),
|
|
||||||
z = boost_cart:get_sign(vel.z)
|
|
||||||
}
|
|
||||||
|
|
||||||
local max_vel = boost_cart.speed_max
|
local max_vel = boost_cart.speed_max
|
||||||
if not dir then
|
if not dir then
|
||||||
dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch)
|
dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch)
|
||||||
@ -183,26 +163,26 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
local new_acc = {x=0, y=0, z=0}
|
local new_acc = {x=0, y=0, z=0}
|
||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
||||||
vel = {x=0, y=0, z=0}
|
vel = {x=0, y=0, z=0}
|
||||||
self.punch = true
|
update = true
|
||||||
else
|
else
|
||||||
-- If the direction changed
|
-- If the direction changed
|
||||||
if dir.x ~= 0 and self.old_dir.z ~= 0 then
|
if dir.x ~= 0 and self.old_dir.z ~= 0 then
|
||||||
vel.x = dir.x * math.abs(vel.z)
|
vel.x = dir.x * math.abs(vel.z)
|
||||||
vel.z = 0
|
vel.z = 0
|
||||||
pos.z = math.floor(pos.z + 0.5)
|
pos.z = math.floor(pos.z + 0.5)
|
||||||
self.punch = true
|
update = true
|
||||||
end
|
end
|
||||||
if dir.z ~= 0 and self.old_dir.x ~= 0 then
|
if dir.z ~= 0 and self.old_dir.x ~= 0 then
|
||||||
vel.z = dir.z * math.abs(vel.x)
|
vel.z = dir.z * math.abs(vel.x)
|
||||||
vel.x = 0
|
vel.x = 0
|
||||||
pos.x = math.floor(pos.x + 0.5)
|
pos.x = math.floor(pos.x + 0.5)
|
||||||
self.punch = true
|
update = true
|
||||||
end
|
end
|
||||||
-- Up, down?
|
-- Up, down?
|
||||||
if dir.y ~= self.old_dir.y then
|
if dir.y ~= self.old_dir.y then
|
||||||
vel.y = dir.y * (math.abs(vel.x) + math.abs(vel.z))
|
vel.y = dir.y * math.abs(vel.x + vel.z)
|
||||||
pos = vector.round(pos)
|
pos = vector.round(pos)
|
||||||
self.punch = true
|
update = true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Slow down or speed up..
|
-- Slow down or speed up..
|
||||||
@ -222,17 +202,12 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
else
|
else
|
||||||
acc = acc - 0.4
|
acc = acc - 0.4
|
||||||
-- Handbrake
|
-- Handbrake
|
||||||
if ctrl and ctrl.down and math.abs(vel.x) + math.abs(vel.z) > 1.2 then
|
if ctrl and ctrl.down and math.abs(vel.x + vel.z) > 1.2 then
|
||||||
acc = acc - 1.2
|
acc = acc - 1.2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
new_acc = {
|
new_acc = vector.multiply(dir, acc)
|
||||||
x = dir.x * acc,
|
|
||||||
y = dir.y * acc,
|
|
||||||
z = dir.z * acc
|
|
||||||
}
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.object:setacceleration(new_acc)
|
self.object:setacceleration(new_acc)
|
||||||
@ -244,11 +219,12 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
for _,v in ipairs({"x","y","z"}) do
|
for _,v in ipairs({"x","y","z"}) do
|
||||||
if math.abs(vel[v]) > max_vel then
|
if math.abs(vel[v]) > max_vel then
|
||||||
vel[v] = boost_cart:get_sign(vel[v]) * max_vel
|
vel[v] = boost_cart:get_sign(vel[v]) * max_vel
|
||||||
self.punch = true
|
update = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not self.punch then
|
self.update = false
|
||||||
|
if not update then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -271,9 +247,10 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
self.object:set_animation(anim, 1, 0)
|
self.object:set_animation(anim, 1, 0)
|
||||||
|
|
||||||
self.object:setvelocity(vel)
|
self.object:setvelocity(vel)
|
||||||
|
if send_pos then
|
||||||
self.object:setpos(pos)
|
self.object:setpos(pos)
|
||||||
|
else
|
||||||
if is_realpunch then
|
-- Collect dropped items
|
||||||
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
|
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
|
||||||
if not obj_:is_player() and
|
if not obj_:is_player() and
|
||||||
obj_:get_luaentity() and
|
obj_:get_luaentity() and
|
||||||
@ -284,7 +261,6 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.punch = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_entity(":carts:cart", boost_cart.cart)
|
minetest.register_entity(":carts:cart", boost_cart.cart)
|
||||||
|
Loading…
Reference in New Issue
Block a user