Added spectate/freecam module

This commit is contained in:
Logan G 2021-12-16 00:08:16 -07:00
parent e437bbdc57
commit eacaa63725
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 205 additions and 0 deletions

View file

@ -0,0 +1,123 @@
--@name
--@author
--@client
if player() == owner() then do
net.start("LSFT-Get-Config")
net.writeString("LSFT-Get-Freecam-Binds")
net.writeTable({"spectate","speed"})
net.writeTable({"spectate","multiplier"})
net.writeTable({"core","controls","forward"})
net.writeTable({"core","controls","backward"})
net.writeTable({"core","controls","left"})
net.writeTable({"core","controls","right"})
net.writeTable({"core","controls","up"})
net.writeTable({"core","controls","down"})
net.writeTable({"core","controls","run"})
net.writeTable({})
net.send()
local SPEED, MULTIPLIER
local KEY_FORWARD, KEY_BACKWARD, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_RUN
net.receive("LSFT-Get-Freecam-Binds", function()
SPEED = unpack(net.readTable())
MULTIPLER = unpack(net.readTable())
KEY_FORWARD = unpack(net.readTable())
KEY_BACKWARD = unpack(net.readTable())
KEY_LEFT = unpack(net.readTable())
KEY_RIGHT = unpack(net.readTable())
KEY_UP = unpack(net.readTable())
KEY_DOWN = unpack(net.readTable())
KEY_RUN = unpack(net.readTable())
end)
function bool_to_number(input)
return input and 1 or 0
end
net.receive("LSFT-Start-Spectate", function()
local target = nil
xpcall(function() target = net.readEntity() end, function() target = nil end)
if input.canLockControls() then
input.lockControls(true)
else
--printHud(Color(250,150,50), "Cannot lock controls, exiting!")
net.start("LSFT-Stop-Spectate")
net.send()
return
end
-- Spectate
if target:isValid() and target:isPlayer() then
hook.add("calcview", "freecam", function(posin, angin, fovin, znearin, zfarin)
if not input.isControlLocked() or not target:isValid() or not target:isPlayer() then
--printHud(Color(250,150,50), "Controls are no longer locked, exiting!")
hook.remove("calcview", "freecam")
net.start("LSFT-Stop-Spectate")
net.send()
return
end
return {
origin = target:getEyePos(),
angles = target:getEyeAngles(),
fov = fovin,
znear = znearin,
zfar = zfarin
}
end)
else
--Freecam
local position = eyePos()
timer.create("updateholo", 1, 0, function()
net.start("LSFT-LoadHolo-Update")
net.writeVector(position)
net.send()
end)
hook.add("calcview", "freecam", function(posin, angin, fovin, znearin, zfarin)
if not input.isControlLocked() then
--printHud(Color(250,150,50), "Controls are no longer locked, exiting!")
hook.remove("calcview", "freecam")
hook.remove("think", "updatepos")
timer.remove("updateholo")
net.start("LSFT-Stop-Spectate")
net.send()
return
end
return {
origin = position,
angles = eyeAngles(),
fov = fovin,
znear = znearin,
zfar = zfarin
}
end)
hook.add("think", "updatepos", function()
local elevation = eyeAngles().p
local bearing = eyeAngles().y
local forward = bool_to_number(input.isKeyDown(KEY_FORWARD)) - bool_to_number(input.isKeyDown(KEY_BACKWARD))
local left = bool_to_number(input.isKeyDown(KEY_LEFT)) - bool_to_number(input.isKeyDown(KEY_RIGHT))
local up = bool_to_number(input.isKeyDown(KEY_UP)) - bool_to_number(input.isKeyDown(KEY_DOWN))
local thing = Vector(forward, left, 0)
thing:rotate(Angle(elevation, bearing, 0))
position = position + thing * SPEED * (input.isKeyDown(KEY_RUN) and MULTIPLER or 1) * (timer.frametime()/(1/(200/3)))
position = position + up * Vector(0,0,SPEED) * (input.isKeyDown(KEY_RUN) and MULTIPLER or 1) * (timer.frametime()/(1/(200/3)))
end)
end
end)
end end

View file

@ -0,0 +1,82 @@
--@name
--@author
--@server
--[[
TODO: Draw viewmodel (not possible :c)
TODO: Custom HUD (health, armor, etc)
--]]
do
function spectate(args)
local loadHolo = nil
net.start("LSFT-Start-Spectate")
net.receive("LSFT-Stop-Spectate", function()
core:log(log.INFO, "Stopped spectating.")
owner():setViewEntity()
if loadHolo ~= nil then
loadHolo:remove()
end
end)
if args[1] == nil then
if not holograms.canSpawn() then
core:log(log.ERROR, "Cannot spawn hologram for freecam!")
end
loadHolo = holograms.create(chip():getPos(), Angle(0,0,0), "models/hunter/blocks/cube025x025x025.mdl")
loadHolo:setColor(Color(255,255,255,0))
owner():setViewEntity(loadHolo)
net.receive("LSFT-LoadHolo-Update", function()
loadHolo:setPos(net.readVector())
end)
core:log(log.INFO, "Started spectating.")
net.send()
return
end
local targets = find.playersByName(args[1])
if #targets == 0 or targets == nil then
core:log(log.ERROR, "No targets found!")
return
elseif #targets > 1 then
local list = ""
for i, v in pairs(target) do
list = list..v..", "
end
list:sub(1, -2)
core:log(log.ERROR, "Muliple targets found! ("..list..")")
return
end
if targets[1]:isPlayer() then
core:log(log.INFO, "Started spectating \""..targets[1]:getName().."\".")
net.writeEntity(targets[1])
net.send()
owner():setViewEntity(targets[1])
end
end
core.modules.spectate = {
version = 1,
desc = "Spectate players or freecam.",
commands = {
spectate = {
func = spectate,
usage = "spectate [ply]",
desc = "Spectate a player in first person by specifying a player, or leave arguments empty for freecam.",
},
},
}
core.defaultconfig.spectate = {
speed = 8,
multiplier = 4
}
end