Add functionality for inverting door swing

This commit is contained in:
Jordan Irwin 2021-08-24 14:39:06 -07:00
parent edfbdbceda
commit 3a1593dda3
2 changed files with 73 additions and 23 deletions

View File

@ -1,4 +1,3 @@
TODO:
- add methods for opening doors in opposite directions
- save meta data when swapping outward opening doors

View File

@ -72,19 +72,35 @@ if not core.global_exists("smodel") then
end
--- API Methods
--
-- @section methods
--- Helper method for inward opening door-like nodes.
--
-- @function simple_models:door_inward_open
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
simple_models.door_inward_open = function(self, pos, new_node)
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_inward_open = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local rot = node.param2-1
if rot < 0 then
rot = 3
local rot
if not invert then
rot = node.param2-1
if rot < 0 then
rot = 3
end
else
rot = node.param2+1
if rot > 3 then
rot = 0
end
end
core.swap_node(pos, {
name = new_node,
param1 = node.param1,
@ -97,14 +113,24 @@ end
-- @function simple_models:door_inward_close
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
simple_models.door_inward_close = function(self, pos, new_node)
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_inward_close = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local rot = node.param2+1
if rot > 3 then
rot = 0
local rot
if not invert then
rot = node.param2+1
if rot > 3 then
rot = 0
end
else
rot = node.param2-1
if rot < 0 then
rot = 3
end
end
core.swap_node(pos, {
name = new_node,
param1 = node.param1,
@ -128,17 +154,22 @@ local get_pos_front = function(pos, param2)
return new_pos
end
local get_pos_behind = function(pos, param2)
local get_pos_behind = function(pos, param2, invert)
local new_pos = table.copy(pos)
local addto = 1
if invert then
addto = -addto
end
if param2 == 0 then
new_pos.x = new_pos.x-1
new_pos.x = new_pos.x - addto
elseif param2 == 2 then
new_pos.x = new_pos.x+1
new_pos.x = new_pos.x + addto
elseif param2 == 1 then
new_pos.z = new_pos.z+1
new_pos.z = new_pos.z + addto
elseif param2 == 3 then
new_pos.z = new_pos.z-1
new_pos.z = new_pos.z - addto
end
return new_pos
@ -149,7 +180,8 @@ end
-- @function simple_models:door_outward_open
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
simple_models.door_outward_open = function(self, pos, new_node)
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_outward_open = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
@ -159,10 +191,19 @@ simple_models.door_outward_open = function(self, pos, new_node)
-- something is blocking door or new_pos is same as old
if blocker and blocker.name ~= "air" then return end
local rot = node.param2+1
if rot > 3 then
rot = 0
local rot
if not invert then
rot = node.param2+1
if rot > 3 then
rot = 0
end
else
rot = node.param2-1
if rot < 0 then
rot = 3
end
end
core.remove_node(pos)
core.set_node(new_pos, {
name = new_node,
@ -176,20 +217,30 @@ end
-- @function simple_models:door_outward_close
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
simple_models.door_outward_close = function(self, pos, new_node)
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_outward_close = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local new_pos = get_pos_behind(pos, node.param2)
local new_pos = get_pos_behind(pos, node.param2, invert)
local blocker = core.get_node(new_pos)
-- something is blocking door or new_pos is same as old
if blocker and blocker.name ~= "air" then return end
local rot = node.param2-1
if rot < 0 then
rot = 3
local rot
if not invert then
rot = node.param2-1
if rot < 0 then
rot = 3
end
else
rot = node.param2+1
if rot > 3 then
rot = 0
end
end
core.remove_node(pos)
core.set_node(new_pos, {
name = new_node,