Shorter codes, allow left/right-down

This commit is contained in:
SmallJoker 2014-08-09 18:42:54 +02:00
parent c07c92a1fb
commit 21e309c414
1 changed files with 43 additions and 31 deletions

View File

@ -19,6 +19,32 @@ function boost_cart:is_rail(pos)
return minetest.get_item_group(node, "rail") ~= 0 return minetest.get_item_group(node, "rail") ~= 0
end end
function boost_cart:check_front_up_down(pos, dir, onlyDown)
local cur = nil
-- Front
dir.y = 0
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur) then
return dir
end
-- Up
if not onlyDown then
dir.y = 1
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur) then
return dir
end
end
-- Down
dir.y = -1
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur) then
return dir
end
return nil
end
function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch) function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch)
local pos = vector.round(pos_) local pos = vector.round(pos_)
local dir = vector.new(dir_) local dir = vector.new(dir_)
@ -44,57 +70,43 @@ function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch)
right_check = false right_check = false
end end
if ctrl.left and left_check then if ctrl.left and left_check then
cur = vector.add(pos, left) cur = boost_cart:check_front_up_down(pos, left, true)
if boost_cart:is_rail(cur) then if cur then
return left, 1 return cur, 1
end end
left_check = false left_check = false
end end
if ctrl.right and right_check then if ctrl.right and right_check then
cur = vector.add(pos, right) cur = boost_cart:check_front_up_down(pos, right, true)
if boost_cart:is_rail(cur) then if cur then
return right, 2 return cur, 2
end end
right_check = true right_check = true
end end
end end
-- Front -- Normal
dir.y = 0 cur = boost_cart:check_front_up_down(pos, dir)
cur = vector.add(pos, dir) if cur then
if boost_cart:is_rail(cur) then return cur
return dir
end
-- Down
dir.y = -1
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur) then
return dir
end
-- Up
dir.y = 1
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur) then
return dir
end end
-- Left, if not already checked -- Left, if not already checked
if left_check then if left_check then
cur = vector.add(pos, left) cur = boost_cart:check_front_up_down(pos, left, true)
if boost_cart:is_rail(cur) then if cur then
return left return cur
end end
end end
-- Right, if not already checked -- Right, if not already checked
if right_check then if right_check then
cur = vector.add(pos, right) cur = boost_cart:check_front_up_down(pos, right, true)
if boost_cart:is_rail(cur) then if cur then
return right return cur
end end
end end
return {x=0, y=0, z=0} return {x=0, y=0, z=0}
end end