noita_entangled_worlds/quant.ew/files/system/telekenisis/telekenisis.lua

153 lines
5.2 KiB
Lua
Raw Normal View History

2025-01-19 18:58:51 -05:00
local rpc = net.new_rpc_namespace()
local tele = {}
local who_has_tele = {}
2025-01-25 15:13:03 -05:00
local is_holding
2025-01-19 18:58:51 -05:00
rpc.opts_reliable()
function rpc.end_tele()
local com = EntityGetFirstComponent(ctx.rpc_player_data.entity, "TelekinesisComponent")
if com ~= nil and ComponentGetValue2(com, "mState") ~= 0 then
ComponentSetValue2(com, "mInteract", true)
end
for i, p in ipairs(who_has_tele) do
if p == ctx.rpc_peer_id then
table.remove(who_has_tele, i)
break
end
end
end
rpc.opts_reliable()
function rpc.send_tele(body_gid, n, extent, aimangle, bodyangle, distance, mindistance)
local com = EntityGetFirstComponent(ctx.rpc_player_data.entity, "TelekinesisComponent")
if com ~= nil then
local ent = ewext.find_by_gid(body_gid)
2025-01-26 14:35:57 -05:00
local x, y = EntityGetTransform(ent)
local cx, cy = GameGetCameraPos()
2025-01-26 14:46:50 -05:00
if x ~= nil then
local dx, dy = math.abs(x - cx), math.abs(y - cy)
if
ent ~= nil
and dx <= MagicNumbersGetValue("VIRTUAL_RESOLUTION_X") / 2
and dy <= MagicNumbersGetValue("VIRTUAL_RESOLUTION_Y") / 2
then
local body_id = PhysicsBodyIDGetFromEntity(ent)[n]
if body_id == nil then
ComponentSetValue2(com, "mState", 0)
return
end
if not table.contains(who_has_tele, ctx.rpc_peer_id) then
table.insert(who_has_tele, ctx.rpc_peer_id)
ComponentSetValue2(com, "mState", 1)
if is_holding == ent then
local mycom = EntityGetFirstComponent(ctx.my_player.entity, "TelekinesisComponent")
if mycom ~= nil then
ComponentSetValue2(mycom, "mInteract", true)
end
is_holding = nil
2025-01-25 15:13:03 -05:00
end
end
2025-01-26 14:46:50 -05:00
ComponentSetValue(com, "mBodyID", body_id)
ComponentSetValue2(com, "mStartBodyMaxExtent", extent)
ComponentSetValue2(com, "mStartAimAngle", aimangle)
ComponentSetValue2(com, "mStartBodyAngle", bodyangle)
ComponentSetValue2(com, "mStartBodyDistance", distance)
ComponentSetValue2(com, "mMinBodyDistance", mindistance)
else
ComponentSetValue2(com, "mState", 0)
2025-01-19 18:58:51 -05:00
end
2025-01-26 14:35:57 -05:00
else
ComponentSetValue2(com, "mState", 0)
2025-01-19 18:58:51 -05:00
end
2025-01-26 14:35:57 -05:00
else
ComponentSetValue2(com, "mState", 0)
2025-01-19 18:58:51 -05:00
end
end
local has_tele = false
local ent_to_body = {}
local function body_to_ent(id)
for ent, lst in pairs(ent_to_body) do
for i, bid in ipairs(lst) do
if bid == id then
return ent, i
end
end
end
end
local sent_track_req = {}
2025-02-09 09:44:30 -05:00
local wait_for_ent = {}
local last_wait = {}
function tele.on_new_entity(ent)
table.insert(wait_for_ent, ent)
end
2025-01-19 18:58:51 -05:00
function tele.on_world_update()
2025-02-09 09:44:30 -05:00
for _, ent in ipairs(last_wait) do
2025-02-09 09:50:04 -05:00
if EntityGetIsAlive(ent) then
local lst = PhysicsBodyIDGetFromEntity(ent)
if lst ~= nil and #lst ~= 0 then
ent_to_body[ent] = lst
end
2025-01-19 18:58:51 -05:00
end
end
2025-02-09 09:44:30 -05:00
last_wait = wait_for_ent
wait_for_ent = {}
2025-01-19 18:58:51 -05:00
if GameGetFrameNum() % 60 == 23 then
for ent, _ in pairs(ent_to_body) do
if not EntityGetIsAlive(ent) then
ent_to_body[ent] = nil
2025-02-09 09:50:04 -05:00
else
ent_to_body[ent] = PhysicsBodyIDGetFromEntity(ent)
2025-02-09 10:25:45 -05:00
if ent_to_body[ent] ~= nil and #ent_to_body[ent] == 0 then
ent_to_body[ent] = nil
end
2025-01-19 18:58:51 -05:00
end
end
end
local com = EntityGetFirstComponent(ctx.my_player.entity, "TelekinesisComponent")
if com ~= nil then
if ComponentGetValue2(com, "mState") ~= 0 then
local body = ComponentGetValue(com, "mBodyID")
local ent, num = body_to_ent(tonumber(body))
2025-01-26 15:20:34 -05:00
if ent ~= nil and EntityGetIsAlive(ent) then
local gid
for _, v in ipairs(EntityGetComponent(ent, "VariableStorageComponent") or {}) do
if ComponentGetValue2(v, "name") == "ew_gid_lid" then
gid = v
break
end
end
if gid ~= nil then
2025-01-25 15:13:03 -05:00
is_holding = ent
has_tele = true
rpc.send_tele(
ComponentGetValue2(gid, "value_string"),
num,
ComponentGetValue2(com, "mStartBodyMaxExtent"),
ComponentGetValue2(com, "mStartAimAngle"),
ComponentGetValue2(com, "mStartBodyAngle"),
ComponentGetValue2(com, "mStartBodyDistance"),
ComponentGetValue2(com, "mMinBodyDistance")
)
elseif not table.contains(sent_track_req, ent) then
table.insert(sent_track_req, ent)
ewext.track(ent)
2025-01-19 18:58:51 -05:00
end
end
elseif has_tele then
has_tele = false
rpc.end_tele()
end
end
end
return tele