78 lines
2.5 KiB
Plaintext
78 lines
2.5 KiB
Plaintext
local oldvel = self.object:getvelocity()
|
|
local olddir = vector.normalize(oldvel)
|
|
local x = math.abs(oldvel.x)
|
|
local z = math.abs(oldvel.z)
|
|
local pos = self.object:getpos()
|
|
pos.y = pos.y - 0.5
|
|
local pos2 = self.object:getpos()
|
|
local pos3 = self.object:getpos()
|
|
pos3.y = pos3.y + 0.5
|
|
|
|
local xer
|
|
local zer
|
|
local dir = {x=0,y=0,z=0}
|
|
|
|
--check direction
|
|
--x axis
|
|
if x > z then
|
|
if olddir.x>0 then xer=1 elseif olddir.x<0 then xer=-1 end
|
|
--check up
|
|
if minetest.get_node_group(minetest.get_node(vector.new(pos.x+xer,pos.y+1,pos.z)).name,"rail")>0 then
|
|
dir.x = xer
|
|
dir.y = 1
|
|
--check down
|
|
elseif minetest.get_node_group(minetest.get_node(vector.new(pos.x+xer,pos.y-1,pos.z)).name,"rail")>0 then
|
|
dir.x = xer
|
|
dir.y = -1
|
|
--check flat
|
|
elseif minetest.get_node_group(minetest.get_node(vector.new(pos.x+xer,pos.y,pos.z)).name,"rail")>0 then
|
|
dir.x = xer
|
|
end
|
|
--z axis
|
|
elseif z > x then
|
|
if olddir.z>0 then zer=1 elseif olddir.z<0 then zer=-1 end
|
|
--check up
|
|
if minetest.get_node_group(minetest.get_node(vector.new(pos.x,pos.y+1,pos.z+zer)).name,"rail")>0 then
|
|
dir.z = zer
|
|
dir.y = 1
|
|
--check down
|
|
elseif minetest.get_node_group(minetest.get_node(vector.new(pos.x,pos.y-1,pos.z+zer)).name,"rail")>0 then
|
|
dir.z = zer
|
|
dir.y = -1
|
|
--check flat
|
|
elseif minetest.get_node_group(minetest.get_node(vector.new(pos.x,pos.y,pos.z+zer)).name,"rail")>0 then
|
|
dir.z = zer
|
|
end
|
|
end
|
|
|
|
--turn
|
|
if vector.equals(dir,vector.new(0,0,0)) then
|
|
if x > z then
|
|
if minetest.get_node_group(minetest.get_node(vector.new(pos.x,pos.y,pos.z+1)).name,"rail")>0 then
|
|
dir.z = 1
|
|
--recenter on the rail
|
|
self.object:moveto(vector.new(math.floor(pos2.x+0.5),pos2.y,pos2.z))
|
|
elseif minetest.get_node_group(minetest.get_node(vector.new(pos.x,pos.y,pos.z-1)).name,"rail")>0 then
|
|
dir.z = -1
|
|
--recenter on the rail
|
|
self.object:moveto(vector.new(math.floor(pos2.x+0.5),pos2.y,pos2.z))
|
|
end
|
|
elseif z > x then
|
|
print("zzzz")
|
|
if minetest.get_node_group(minetest.get_node(vector.new(pos.x+1,pos.y,pos.z)).name,"rail")>0 then
|
|
dir.x = 1
|
|
--recenter on the rail
|
|
self.object:moveto(vector.new(pos2.x,pos2.y,math.floor(pos2.z+0.5)))
|
|
elseif minetest.get_node_group(minetest.get_node(vector.new(pos.x-1,pos.y,pos.z)).name,"rail")>0 then
|
|
dir.x = -1
|
|
--recenter on the rail
|
|
self.object:moveto(vector.new(pos2.x,pos2.y,math.floor(pos2.z+0.5)))
|
|
end
|
|
end
|
|
end
|
|
--apply
|
|
--if not vector.equals(dir,vector.new(0,0,0)) then
|
|
self.object:setvelocity(dir)
|
|
--end
|
|
|