forked from hkc/cc-stuff
1
0
Fork 0
cc-stuff/augment/modules/wh.lua

84 lines
2.9 KiB
Lua
Raw Normal View History

return function()
local playerLookVector = nil
local function updateLookVector()
-- pitch:
-- 0 => positive Z
-- 90 => negative X
local yawr = math.rad(player.yaw)
local pitchr = math.rad(player.pitch)
local cosPitch = math.cos(pitchr)
local x = cosPitch * -math.sin(yawr)
local z = cosPitch * math.cos(yawr)
local y = math.sin(pitchr)
playerLookVector = {x, y, z, x = x, y = y, z = z}
return playerLookVector
end
local function dotProduct3(u, v)
return u.x * v.x + u.y * v.y + u.z * v.z
end
local function vec3Scale(k, v)
return {k * v.x, k * v.y, k * v.z}
end
local function vec3Add(u, v)
return {u.x + v.x, u.y + v.y, u.z + v.z}
end
local function toFrontCoords(entity)
local dotResult = dotProduct3(entity, playerLookVector)
if dotResult >= 0.0 then
return {x = entity.x, y = entity.y, z = entity.z, flipped = false}
end
local result = vec3Add(entity, vec3Scale(-2 * dotResult, playerLookVector))
result.flipped = true
return result
end
2023-10-12 21:25:20 +03:00
local cache = {}
while _G._running do
updateLookVector()
2023-10-12 21:36:32 +03:00
for id, entry in pairs(cache) do
if nearbyEntitiesByUUID[id] == nil then
entry.cube.remove()
entry.frame.remove()
2023-10-12 21:37:31 +03:00
cache[id] = nil
end
end
2023-10-12 21:25:20 +03:00
2023-10-12 21:36:32 +03:00
for id, entity in pairs(nearbyEntitiesByUUID) do
if id ~= player.id then
if cache[id] == nil then
cache[id] = {}
cache[id].cube = canvas3d.addBox(0, 0, 0)
2023-10-12 21:48:08 +03:00
cache[id].cube.setSize(0.5, 0.5, 0.5)
2023-10-12 21:36:32 +03:00
cache[id].frame = canvas3d.addFrame({ 0, 0, 0 })
cache[id].text = cache[id].frame.addText({ 0, 0 }, "")
end
local entityFront = toFrontCoords(entity)
if entityFront.flipped then
cache[id].cube.setColor(0x004040FF)
else
cache[id].cube.setColor(0xFFC0C0FF)
end
cache[id].cube.setAlpha(0x20)
2023-10-12 21:36:32 +03:00
cache[id].cube.setDepthTested(false)
cache[id].frame.setDepthTested(false)
cache[id].cube.setPosition(entityFront.x - 0.25, entityFront.y - 0.25, entityFront.z - 0.25)
cache[id].frame.setPosition(entityFront.x, entityFront.y, entityFront.z)
2023-10-12 21:36:32 +03:00
cache[id].text.setAlpha(0xFF)
2023-10-12 21:59:29 +03:00
cache[id].text.setText(entity.name .. "\n" .. textutils.serialize(entity))
if entityFront.flipped then
cache[id].text.setColor(0x00FFFFFF)
else
cache[id].text.setColor(0xFF0000FF)
end
end
end
os.sleep(0.05)
end
end