Skip to content

Commit

Permalink
Boats: Fix sinking through boat when detaching
Browse files Browse the repository at this point in the history
By CProgrammerRU
Also, by paramat:
Slightly raise base of collision box
Improve code style
  • Loading branch information
paramat committed Aug 18, 2015
1 parent b240499 commit 99d0442
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions mods/boats/init.lua
@@ -1,4 +1,3 @@

--
-- Helper functions
--
Expand All @@ -8,6 +7,7 @@ local function is_water(pos)
return minetest.get_item_group(nn, "water") ~= 0
end


local function get_sign(i)
if i == 0 then
return 0
Expand All @@ -16,12 +16,14 @@ local function get_sign(i)
end
end


local function get_velocity(v, yaw, y)
local x = -math.sin(yaw) * v
local z = math.cos(yaw) * v
return {x = x, y = y, z = z}
end


local function get_v(v)
return math.sqrt(v.x ^ 2 + v.z ^ 2)
end
Expand All @@ -32,7 +34,7 @@ end

local boat = {
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",
mesh = "boat.obj",
textures = {"default_wood.png"},
Expand All @@ -43,6 +45,7 @@ local boat = {
removed = false
}


function boat.on_rightclick(self, clicker)
if not clicker or not clicker:is_player() then
return
Expand All @@ -53,9 +56,15 @@ function boat.on_rightclick(self, clicker)
clicker:set_detach()
default.player_attached[name] = false
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
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
minetest.after(0.2, function()
default.player_set_animation(clicker, "sit" , 30)
Expand All @@ -64,6 +73,7 @@ function boat.on_rightclick(self, clicker)
end
end


function boat.on_activate(self, staticdata, dtime_s)
self.object:set_armor_groups({immortal = 1})
if staticdata then
Expand All @@ -72,11 +82,14 @@ function boat.on_activate(self, staticdata, dtime_s)
self.last_v = self.v
end


function boat.get_staticdata(self)
return tostring(self.v)
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
return
end
Expand All @@ -97,6 +110,7 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d
end
end


function boat.on_step(self, dtime)
self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
if self.driver then
Expand Down Expand Up @@ -149,7 +163,8 @@ function boat.on_step(self, dtime)
else
new_acce = {x = 0, y = -9.8, z = 0}
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())
else
p.y = p.y + 1
Expand All @@ -172,7 +187,8 @@ function boat.on_step(self, dtime)
self.object:setpos(pos)
new_velo = get_velocity(self.v, self.object:getyaw(), 0)
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())
end
end
Expand All @@ -181,8 +197,10 @@ function boat.on_step(self, dtime)
self.object:setacceleration(new_acce)
end


minetest.register_entity("boats:boat", boat)


minetest.register_craftitem("boats:boat", {
description = "Boat",
inventory_image = "boat_inventory.png",
Expand All @@ -206,6 +224,7 @@ minetest.register_craftitem("boats:boat", {
end,
})


minetest.register_craft({
output = "boats:boat",
recipe = {
Expand All @@ -214,4 +233,3 @@ minetest.register_craft({
{"group:wood", "group:wood", "group:wood"},
},
})

0 comments on commit 99d0442

Please sign in to comment.