Improve gaze sensor

This commit is contained in:
Joachim Stolberg 2023-06-23 21:54:39 +02:00
parent 356e8310d8
commit b1efedb55f

View File

@ -73,20 +73,22 @@ local function get_players(pos)
return t return t
end end
local function player_focuses_block(pos, name) local function player_focuses_block(pos, name, obj)
local obj = minetest.get_player_by_name(name) obj = obj or minetest.get_player_by_name(name)
if obj then if obj then
local player_pos = obj:get_pos() local player_pos = obj:get_pos()
player_pos.y = player_pos.y + 1.5 player_pos.y = player_pos.y + 1.5
local dist = vector.distance(pos, player_pos) local dist = vector.distance(pos, player_pos)
if dist < MAX_PLAYER_DIST then if dist < MAX_PLAYER_DIST then
local dir = obj:get_look_dir() local dir = obj:get_look_dir()
local vec1 = vector.multiply(dir, dist) if dir then
local pos1 = vector.round(vector.add(player_pos, vec1)) local vec1 = vector.multiply(dir, dist)
if vector.equals(pos, pos1) then local pos1 = vector.round(vector.add(player_pos, vec1))
local item = obj:get_wielded_item() if vector.equals(pos, pos1) then
if not item or item:get_name() ~= "techage:end_wrench" then local item = obj:get_wielded_item()
return true if not item or item:get_name() ~= "techage:end_wrench" then
return true
end
end end
end end
end end
@ -97,16 +99,32 @@ end
local function scan_for_player(pos) local function scan_for_player(pos)
local mem = techage.get_mem(pos) local mem = techage.get_mem(pos)
mem.players = mem.players or get_players(pos) mem.players = mem.players or get_players(pos)
for _, name in ipairs(mem.players) do if mem.players[1] == "***" then
if player_focuses_block(pos, name) then for _, obj in ipairs(minetest.get_objects_inside_radius(pos, MAX_PLAYER_DIST)) do
mem.player_name = name if player_focuses_block(pos, "", obj) then
return true mem.player_name = obj:get_player_name()
return true
end
end
else
for _, name in ipairs(mem.players) do
if player_focuses_block(pos, name, nil) then
mem.player_name = name
return true
end
end end
end end
mem.player_name = "" mem.player_name = ""
return false return false
end end
local function ta_after_formspec(pos, fields, playername)
if fields.save then
local mem = techage.get_mem(pos)
mem.players = nil
end
end
minetest.register_node("techage:ta4_gaze_sensor_off", { minetest.register_node("techage:ta4_gaze_sensor_off", {
description = DESCR, description = DESCR,
tiles = { tiles = {
@ -139,6 +157,7 @@ minetest.register_node("techage:ta4_gaze_sensor_off", {
end, end,
ta4_formspec = WRENCH_MENU, ta4_formspec = WRENCH_MENU,
ta_after_formspec = ta_after_formspec,
groups = {choppy=2, cracky=2, crumbly=2}, groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false, is_ground_content = false,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
@ -170,6 +189,7 @@ minetest.register_node("techage:ta4_gaze_sensor_on", {
end, end,
ta4_formspec = WRENCH_MENU, ta4_formspec = WRENCH_MENU,
ta_after_formspec = ta_after_formspec,
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1}, groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
is_ground_content = false, is_ground_content = false,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),