Boats: Fix sinking through boat when detaching

By CProgrammerRU
Also, by paramat:
Slightly raise base of collision box
Improve code style
This commit is contained in:
paramat 2015-08-16 23:24:41 +01:00
parent b24049950e
commit 99d0442cc9

View File

@ -1,4 +1,3 @@
-- --
-- Helper functions -- Helper functions
-- --
@ -8,6 +7,7 @@ local function is_water(pos)
return minetest.get_item_group(nn, "water") ~= 0 return minetest.get_item_group(nn, "water") ~= 0
end end
local function get_sign(i) local function get_sign(i)
if i == 0 then if i == 0 then
return 0 return 0
@ -16,12 +16,14 @@ local function get_sign(i)
end end
end end
local function get_velocity(v, yaw, y) local function get_velocity(v, yaw, y)
local x = -math.sin(yaw) * v local x = -math.sin(yaw) * v
local z = math.cos(yaw) * v local z = math.cos(yaw) * v
return {x = x, y = y, z = z} return {x = x, y = y, z = z}
end end
local function get_v(v) local function get_v(v)
return math.sqrt(v.x ^ 2 + v.z ^ 2) return math.sqrt(v.x ^ 2 + v.z ^ 2)
end end
@ -32,7 +34,7 @@ end
local boat = { local boat = {
physical = true, physical = true,
collisionbox = {-0.5, -0.4, -0.5, 0.5, 0.3, 0.5}, collisionbox = {-0.5, -0.35, -0.5, 0.5, 0.3, 0.5},
visual = "mesh", visual = "mesh",
mesh = "boat.obj", mesh = "boat.obj",
textures = {"default_wood.png"}, textures = {"default_wood.png"},
@ -43,6 +45,7 @@ local boat = {
removed = false removed = false
} }
function boat.on_rightclick(self, clicker) function boat.on_rightclick(self, clicker)
if not clicker or not clicker:is_player() then if not clicker or not clicker:is_player() then
return return
@ -53,9 +56,15 @@ function boat.on_rightclick(self, clicker)
clicker:set_detach() clicker:set_detach()
default.player_attached[name] = false default.player_attached[name] = false
default.player_set_animation(clicker, "stand" , 30) default.player_set_animation(clicker, "stand" , 30)
local pos = clicker:getpos()
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
minetest.after(0.1, function()
clicker:setpos(pos)
end)
elseif not self.driver then elseif not self.driver then
self.driver = clicker self.driver = clicker
clicker:set_attach(self.object, "", {x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0}) clicker:set_attach(self.object, "",
{x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
default.player_attached[name] = true default.player_attached[name] = true
minetest.after(0.2, function() minetest.after(0.2, function()
default.player_set_animation(clicker, "sit" , 30) default.player_set_animation(clicker, "sit" , 30)
@ -64,6 +73,7 @@ function boat.on_rightclick(self, clicker)
end end
end end
function boat.on_activate(self, staticdata, dtime_s) function boat.on_activate(self, staticdata, dtime_s)
self.object:set_armor_groups({immortal = 1}) self.object:set_armor_groups({immortal = 1})
if staticdata then if staticdata then
@ -72,11 +82,14 @@ function boat.on_activate(self, staticdata, dtime_s)
self.last_v = self.v self.last_v = self.v
end end
function boat.get_staticdata(self) function boat.get_staticdata(self)
return tostring(self.v) return tostring(self.v)
end end
function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, direction)
function boat.on_punch(self, puncher, time_from_last_punch,
tool_capabilities, direction)
if not puncher or not puncher:is_player() or self.removed then if not puncher or not puncher:is_player() or self.removed then
return return
end end
@ -97,6 +110,7 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d
end end
end end
function boat.on_step(self, dtime) function boat.on_step(self, 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
@ -149,7 +163,8 @@ function boat.on_step(self, dtime)
else else
new_acce = {x = 0, y = -9.8, z = 0} new_acce = {x = 0, y = -9.8, z = 0}
end end
new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y) new_velo = get_velocity(self.v, self.object:getyaw(),
self.object:getvelocity().y)
self.object:setpos(self.object:getpos()) self.object:setpos(self.object:getpos())
else else
p.y = p.y + 1 p.y = p.y + 1
@ -172,7 +187,8 @@ function boat.on_step(self, dtime)
self.object:setpos(pos) self.object:setpos(pos)
new_velo = get_velocity(self.v, self.object:getyaw(), 0) new_velo = get_velocity(self.v, self.object:getyaw(), 0)
else else
new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y) new_velo = get_velocity(self.v, self.object:getyaw(),
self.object:getvelocity().y)
self.object:setpos(self.object:getpos()) self.object:setpos(self.object:getpos())
end end
end end
@ -181,8 +197,10 @@ function boat.on_step(self, dtime)
self.object:setacceleration(new_acce) self.object:setacceleration(new_acce)
end end
minetest.register_entity("boats:boat", boat) minetest.register_entity("boats:boat", boat)
minetest.register_craftitem("boats:boat", { minetest.register_craftitem("boats:boat", {
description = "Boat", description = "Boat",
inventory_image = "boat_inventory.png", inventory_image = "boat_inventory.png",
@ -206,6 +224,7 @@ minetest.register_craftitem("boats:boat", {
end, end,
}) })
minetest.register_craft({ minetest.register_craft({
output = "boats:boat", output = "boats:boat",
recipe = { recipe = {
@ -214,4 +233,3 @@ minetest.register_craft({
{"group:wood", "group:wood", "group:wood"}, {"group:wood", "group:wood", "group:wood"},
}, },
}) })