Refactor mcl_boats

This commit is contained in:
Wuzzy 2017-06-13 00:26:17 +02:00
parent 57c17b1a00
commit 4729c3cd89

View file

@ -49,10 +49,11 @@ local boat = {
run_start = 0, run_end = 40, run_start = 0, run_end = 40,
}, },
driver = nil, _driver = nil, -- Attached driver (player) or nil if none
v = 0, _v = 0, -- Speed
last_v = 0, _last_v = 0, -- Temporary speed variable
removed = false _removed = false, -- If true, boat entity is considered removed (e.g. after punch) and should be ignored
_itemstring = "mcl_boats:boat", -- Itemstring of the boat item (implies boat type)
} }
function boat.on_rightclick(self, clicker) function boat.on_rightclick(self, clicker)
@ -60,24 +61,24 @@ function boat.on_rightclick(self, clicker)
return return
end end
local name = clicker:get_player_name() local name = clicker:get_player_name()
if self.driver and clicker == self.driver then if self._driver and clicker == self._driver then
self.driver = nil self._driver = nil
clicker:set_detach() clicker:set_detach()
mcl_player.player_attached[name] = false mcl_player.player_attached[name] = false
mcl_player.player_set_animation(clicker, "stand" , 30) mcl_player.player_set_animation(clicker, "stand" , 30)
local pos = clicker:getpos() local pos = clicker:getpos()
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z} pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
clicker:setpos(pos) clicker:setpos(pos)
elseif not self.driver then elseif not self._driver then
local attach = clicker:get_attach() local attach = clicker:get_attach()
if attach and attach:get_luaentity() then if attach and attach:get_luaentity() then
local luaentity = attach:get_luaentity() local luaentity = attach:get_luaentity()
if luaentity.driver then if luaentity._driver then
luaentity.driver = nil luaentity._driver = nil
end end
clicker:set_detach() clicker:set_detach()
end end
self.driver = clicker self._driver = clicker
clicker:set_attach(self.object, "", clicker:set_attach(self.object, "",
{x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0}) {x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
mcl_player.player_attached[name] = true mcl_player.player_attached[name] = true
@ -95,8 +96,8 @@ function boat.on_activate(self, staticdata, dtime_s)
self.object:set_armor_groups({immortal = 1}) self.object:set_armor_groups({immortal = 1})
local data = minetest.deserialize(staticdata) local data = minetest.deserialize(staticdata)
if type(data) == "table" then if type(data) == "table" then
self.v = data.v self._v = data.v
self.last_v = self.v self._last_v = self._v
self._itemstring = data.itemstring self._itemstring = data.itemstring
self.object:set_properties({textures=data.textures}) self.object:set_properties({textures=data.textures})
end end
@ -105,7 +106,7 @@ end
function boat.get_staticdata(self) function boat.get_staticdata(self)
return minetest.serialize({ return minetest.serialize({
v = self.v, v = self._v,
itemstring = self._itemstring, itemstring = self._itemstring,
textures = self.object:get_properties().textures textures = self.object:get_properties().textures
}) })
@ -113,16 +114,16 @@ end
function boat.on_punch(self, puncher) function boat.on_punch(self, puncher)
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
if self.driver and puncher == self.driver then if self._driver and puncher == self._driver then
self.driver = nil self._driver = nil
puncher:set_detach() puncher:set_detach()
mcl_player.player_attached[puncher:get_player_name()] = false mcl_player.player_attached[puncher:get_player_name()] = false
end end
if not self.driver then if not self._driver then
self.removed = true self._removed = true
-- Drop boat as item on the ground after punching -- Drop boat as item on the ground after punching
if not minetest.setting_getbool("creative_mode") then if not minetest.setting_getbool("creative_mode") then
minetest.add_item(self.object:getpos(), self._itemstring) minetest.add_item(self.object:getpos(), self._itemstring)
@ -133,23 +134,23 @@ 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
local ctrl = self.driver:get_player_control() local ctrl = self._driver:get_player_control()
local yaw = self.object:getyaw() 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
elseif ctrl.down then elseif ctrl.down then
self.v = self.v - 0.1 self._v = self._v - 0.1
end end
if ctrl.left then if ctrl.left then
if self.v < 0 then if self._v < 0 then
self.object:setyaw(yaw - (1 + dtime) * 0.03) self.object:setyaw(yaw - (1 + dtime) * 0.03)
else else
self.object:setyaw(yaw + (1 + dtime) * 0.03) self.object:setyaw(yaw + (1 + dtime) * 0.03)
end end
elseif ctrl.right then elseif ctrl.right then
if self.v < 0 then if self._v < 0 then
self.object:setyaw(yaw + (1 + dtime) * 0.03) self.object:setyaw(yaw + (1 + dtime) * 0.03)
else else
self.object:setyaw(yaw - (1 + dtime) * 0.03) self.object:setyaw(yaw - (1 + dtime) * 0.03)
@ -157,19 +158,19 @@ function boat.on_step(self, dtime)
end end
end end
local velo = self.object:getvelocity() local velo = self.object:getvelocity()
if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then if self._v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
self.object:setpos(self.object:getpos()) self.object:setpos(self.object:getpos())
return return
end 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
self.object:setvelocity({x = 0, y = 0, z = 0}) self.object:setvelocity({x = 0, y = 0, z = 0})
self.v = 0 self._v = 0
return return
end end
if math.abs(self.v) > 5 then if math.abs(self._v) > 5 then
self.v = 5 * get_sign(self.v) self._v = 5 * get_sign(self._v)
end end
local p = self.object:getpos() local p = self.object:getpos()
@ -179,12 +180,12 @@ function boat.on_step(self, dtime)
if not is_water(p) then if not is_water(p) then
local nodedef = minetest.registered_nodes[minetest.get_node(p).name] local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
if (not nodedef) or nodedef.walkable then if (not nodedef) or nodedef.walkable then
self.v = 0 self._v = 0
new_acce = {x = 0, y = 1, z = 0} new_acce = {x = 0, y = 1, z = 0}
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(), new_velo = get_velocity(self._v, self.object:getyaw(),
self.object:getvelocity().y) self.object:getvelocity().y)
self.object:setpos(self.object:getpos()) self.object:setpos(self.object:getpos())
else else
@ -198,7 +199,7 @@ function boat.on_step(self, dtime)
else else
new_acce = {x = 0, y = 5, z = 0} new_acce = {x = 0, y = 5, z = 0}
end end
new_velo = get_velocity(self.v, self.object:getyaw(), y) new_velo = get_velocity(self._v, self.object:getyaw(), y)
self.object:setpos(self.object:getpos()) self.object:setpos(self.object:getpos())
else else
new_acce = {x = 0, y = 0, z = 0} new_acce = {x = 0, y = 0, z = 0}
@ -206,9 +207,9 @@ function boat.on_step(self, dtime)
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)
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(), new_velo = get_velocity(self._v, self.object:getyaw(),
self.object:getvelocity().y) self.object:getvelocity().y)
self.object:setpos(self.object:getpos()) self.object:setpos(self.object:getpos())
end end