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

91 lines
3.0 KiB
Lua

return function()
local playerLookVector = nil
local function updateLookVector()
-- yaw:
-- 0 => positive Z
-- 90 => negative X
-- pitch:
-- 0 => forward
-- 90 => down
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 {x = k * v.x, y = k * v.y, z = k * v.z}
end
local function vec3Add(u, v)
return {x = u.x + v.x, y = u.y + v.y, z = 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
while _G.player == nil do
os.sleep(0.05)
end
local cache = {}
while _G._running do
updateLookVector()
for id, entry in pairs(cache) do
if nearbyEntitiesByUUID[id] == nil then
entry.cube.remove()
entry.frame.remove()
cache[id] = nil
end
end
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)
cache[id].cube.setSize(0.5, 0.5, 0.5)
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)
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)
cache[id].text.setAlpha(0xFF)
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