1
0
mirror of https://github.com/minetest/minetest_game.git synced 2024-12-22 23:10:17 +01:00

Update only moving boats

This commit is contained in:
BlockMen 2014-07-01 14:25:43 +02:00
parent 7ea81f0906
commit e0673908bf

View File

@ -4,7 +4,7 @@
-- --
local function is_water(pos) local function is_water(pos)
local nn = minetest.env:get_node(pos).name local nn = minetest.get_node(pos).name
return minetest.get_item_group(nn, "water") ~= 0 return minetest.get_item_group(nn, "water") ~= 0
end end
@ -39,6 +39,7 @@ local boat = {
driver = nil, driver = nil,
v = 0, v = 0,
last_v = 0,
} }
function boat:on_rightclick(clicker) function boat:on_rightclick(clicker)
@ -67,6 +68,7 @@ function boat:on_activate(staticdata, dtime_s)
if staticdata then if staticdata then
self.v = tonumber(staticdata) self.v = tonumber(staticdata)
end end
self.last_v = self.v
end end
function boat:get_staticdata() function boat:get_staticdata()
@ -75,7 +77,7 @@ end
function boat:on_punch(puncher, time_from_last_punch, tool_capabilities, direction) function boat:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
self.object:remove() self.object:remove()
if puncher and puncher:is_player() then if puncher and puncher:is_player() and not minetest.setting_getbool("creative_mode") then
puncher:get_inventory():add_item("main", "boats:boat") puncher:get_inventory():add_item("main", "boats:boat")
end end
end end
@ -84,6 +86,7 @@ function boat:on_step(dtime)
self.v = get_v(self.object:getvelocity())*get_sign(self.v) self.v = get_v(self.object:getvelocity())*get_sign(self.v)
if self.driver then if self.driver then
local ctrl = self.driver:get_player_control() local ctrl = self.driver:get_player_control()
local yaw = self.object:getyaw()
if ctrl.up then if ctrl.up then
self.v = self.v+0.1 self.v = self.v+0.1
end end
@ -92,19 +95,23 @@ function boat:on_step(dtime)
end end
if ctrl.left then if ctrl.left then
if ctrl.down then if ctrl.down then
self.object:setyaw(self.object:getyaw()-math.pi/120-dtime*math.pi/120) self.object:setyaw(yaw-math.pi/120-dtime*math.pi/120)
else else
self.object:setyaw(self.object:getyaw()+math.pi/120+dtime*math.pi/120) self.object:setyaw(yaw+math.pi/120+dtime*math.pi/120)
end end
end end
if ctrl.right then if ctrl.right then
if ctrl.down then if ctrl.down then
self.object:setyaw(self.object:getyaw()+math.pi/120+dtime*math.pi/120) self.object:setyaw(yaw+math.pi/120+dtime*math.pi/120)
else else
self.object:setyaw(self.object:getyaw()-math.pi/120-dtime*math.pi/120) self.object:setyaw(yaw-math.pi/120-dtime*math.pi/120)
end end
end end
end end
local velo = self.object:getvelocity()
if self.v == 0 and velo.x == 0 and velo.z == 0 then
return
end
local s = get_sign(self.v) local s = get_sign(self.v)
self.v = self.v - 0.02*s self.v = self.v - 0.02*s
if s ~= get_sign(self.v) then if s ~= get_sign(self.v) then
@ -118,16 +125,18 @@ function boat:on_step(dtime)
local p = self.object:getpos() local p = self.object:getpos()
p.y = p.y-0.5 p.y = p.y-0.5
local new_velo = {x=0,y=0,z=0}
local new_acce = {x=0,y=0,z=0}
if not is_water(p) then if not is_water(p) then
if minetest.registered_nodes[minetest.env:get_node(p).name].walkable then if minetest.registered_nodes[minetest.env:get_node(p).name].walkable then
self.v = 0 self.v = 0
end end
self.object:setacceleration({x=0, y=-10, z=0}) new_acce = {x=0, y=-10, z=0}
self.object:setvelocity(get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)) new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)
else else
p.y = p.y+1 p.y = p.y+1
if is_water(p) then if is_water(p) then
self.object:setacceleration({x=0, y=3, z=0}) new_acce = {x=0, y=3, z=0}
local y = self.object:getvelocity().y local y = self.object:getvelocity().y
if y > 2 then if y > 2 then
y = 2 y = 2
@ -135,19 +144,21 @@ function boat:on_step(dtime)
if y < 0 then if y < 0 then
self.object:setacceleration({x=0, y=10, z=0}) self.object:setacceleration({x=0, y=10, z=0})
end end
self.object:setvelocity(get_velocity(self.v, self.object:getyaw(), y)) new_velo = get_velocity(self.v, self.object:getyaw(), y)
else else
self.object:setacceleration({x=0, y=0, z=0}) new_acce = {x=0, y=0, z=0}
if math.abs(self.object:getvelocity().y) < 1 then if math.abs(self.object:getvelocity().y) < 1 then
local pos = self.object:getpos() local pos = self.object:getpos()
pos.y = math.floor(pos.y)+0.5 pos.y = math.floor(pos.y)+0.5
self.object:setpos(pos) self.object:setpos(pos)
self.object:setvelocity(get_velocity(self.v, self.object:getyaw(), 0)) new_velo = get_velocity(self.v, self.object:getyaw(), 0)
else else
self.object:setvelocity(get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)) new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)
end end
end end
end end
self.object:setvelocity(new_velo)
self.object:setacceleration(new_acce)
end end
minetest.register_entity("boats:boat", boat) minetest.register_entity("boats:boat", boat)
@ -168,8 +179,10 @@ minetest.register_craftitem("boats:boat", {
return return
end end
pointed_thing.under.y = pointed_thing.under.y+0.5 pointed_thing.under.y = pointed_thing.under.y+0.5
minetest.env:add_entity(pointed_thing.under, "boats:boat") minetest.add_entity(pointed_thing.under, "boats:boat")
itemstack:take_item() if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
return itemstack return itemstack
end, end,
}) })