Pressure plates check for entity contact

This commit is contained in:
Johannes Fritz 2023-01-25 14:01:32 -06:00
parent 54de66b3e1
commit 9d276033dc
1 changed files with 49 additions and 10 deletions

View File

@ -18,19 +18,58 @@ local function pp_on_timer(pos)
-- For some reason the first time on_timer is called, the pos is wrong
if not basename then return end
local objs = minetest.get_objects_inside_radius(pos, 1)
local function obj_touching_plate_pos(obj_ref, plate_pos)
local obj_pos = obj_ref:get_pos()
local props = obj_ref:get_properties()
local parent = obj_ref:get_attach()
if props and obj_pos and not parent then
local collisionbox = props.collisionbox
local physical = props.physical
local is_player = obj_ref:is_player()
local luaentity = obj_ref:get_luaentity()
local is_item = luaentity and luaentity.name == "__builtin:item"
if collisionbox and physical or is_player or is_item then
local plate_x_min = plate_pos.x - 7 / 16
local plate_x_max = plate_pos.x + 7 / 16
local plate_z_min = plate_pos.z - 7 / 16
local plate_z_max = plate_pos.z + 7 / 16
local plate_y_max = plate_pos.y - 6.5 / 16
if objs[1] == nil and node.name == basename .. "_on" then
minetest.set_node(pos, {name = basename .. "_off"})
mesecon.receptor_off(pos, mesecon.rules.pplate)
elseif node.name == basename .. "_off" then
for k, obj in pairs(objs) do
local objpos = obj:get_pos()
if objpos.y > pos.y-1 and objpos.y < pos.y then
minetest.set_node(pos, {name = basename .. "_on"})
mesecon.receptor_on(pos, mesecon.rules.pplate )
local obj_x_min = obj_pos.x + collisionbox[1]
local obj_x_max = obj_pos.x + collisionbox[4]
local obj_z_min = obj_pos.z + collisionbox[3]
local obj_z_max = obj_pos.z + collisionbox[6]
local obj_y_min = obj_pos.y + collisionbox[2]
if
obj_y_min <= plate_y_max and
obj_x_min < plate_x_max and
obj_x_max > plate_x_min and
obj_z_min < plate_z_max and
obj_z_max > plate_z_min
then
return true
end
end
end
return false
end
local objs = minetest.get_objects_inside_radius(pos, 1)
local obj_touching = false
for k, obj in pairs(objs) do
if obj_touching_plate_pos(obj, pos) then
obj_touching = true
break
end
end
if not obj_touching and node.name == basename .. "_on" then
minetest.set_node(pos, {name = basename .. "_off"})
mesecon.receptor_off(pos, mesecon.rules.pplate)
elseif obj_touching and node.name == basename .. "_off" then
minetest.set_node(pos, {name = basename .. "_on"})
mesecon.receptor_on(pos, mesecon.rules.pplate )
end
return true
end