wiremod-svn-archive/wire/lua/entities/gmod_wire_pod/init.lua
2008-06-14 00:55:20 +00:00

154 lines
3.6 KiB
Lua

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')
ENT.WireDebugName = "Pod Controller"
local MODEL = Model("models/jaanus/wiretool/wiretool_siren.mdl")
function ENT:Initialize()
self.Entity:SetModel( MODEL )
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )
-- Output keys. Format: self.Keys["name"] = IN_*
self.Keys = { }
self.Keys["Attack"] = IN_ATTACK
self.Keys["Attack2"] = IN_ATTACK2
self.Keys["Forward"] = IN_FORWARD
self.Keys["Left"] = IN_MOVELEFT
self.Keys["Back"] = IN_BACK
self.Keys["Right"] = IN_MOVERIGHT
self.Keys["Reload"] = IN_RELOAD
self.Keys["Jump"] = IN_JUMP
self.Keys["Duck"] = IN_DUCK
self.Keys["Sprint"] = IN_SPEED
self.Keys["Zoom"] = IN_ZOOM
-- Invert the table to use it with Wire_CreateOutputs
local outputs = { }
local n = 1
for k, v in pairs( self.Keys ) do
outputs[n] = k
n = n + 1
end
outputs[n] = "Active"
self.VPos = Vector(0, 0, 0)
-- Create outputs
self.Outputs = Wire_CreateOutputs( self.Entity, outputs )
self.Inputs = Wire_CreateInputs( self.Entity, { "Lock", "Eject", "Crosshair", "Open" } )
self:SetOverlayText( "Pod Controller" )
end
function ENT:SetKeys(keys)
self.Keys = keys
local out = {}
for k,v in pairs(keys) do
out[#out+1] = k
end
out[#out+1] = "Active"
WireLib.AdjustOutputs(self.Entity, out)
end
-- Link to pod
function ENT:Setup(pod)
self.Pod = pod
end
function ENT:ShowOutput(value)
if value ~= self.PrevOutput then
self:SetOverlayText( "Pod Controller" )
self.PrevOutput = value
end
end
function ENT:OnRestore()
self.BaseClass.OnRestore(self)
end
-- Called every 0.01 seconds, check for key down
function ENT:Think()
-- Check that we have a pod
if self.Pod and self.Pod:IsValid() then
self.Ply = self.Pod:GetPassenger()
if self.Ply and self.Ply:IsValid() and self.Keys then
-- Loop through all the self.Keys, and check if they was pressed last frame
for k, v in pairs(self.Keys) do
if self.Ply:KeyDownLast(v[1]) then
Wire_TriggerOutput(self.Entity, k, v[2])
else
Wire_TriggerOutput(self.Entity, k, v[3])
end
end
local trace = util.GetPlayerTrace(self.Ply)
trace.filter = self.Pod
self.VPos = util.TraceLine(trace).HitPos
Wire_TriggerOutput(self.Entity, "Active", 1)
else
Wire_TriggerOutput(self.Entity, "Active", 0)
end
end
self.Entity:NextThink(CurTime() + 0.01)
return true
end
function ENT:TriggerInput(iname, value)
if not (self.Pod and self.Pod:IsValid()) then return end
if iname == "Lock" then
if value ~= 0 then
self.Pod:Fire("Lock", "1", 0)
else
self.Pod:Fire("Unlock", "1", 0)
end
elseif iname == "Eject" then
if value ~= 0 then
self.Pod:Fire("ExitVehicle", "1", 0)
end
elseif iname == "Crosshair" and self.Ply and self.Ply:IsValid() then
if value ~= 0 then
self.Ply:CrosshairEnable()
else
self.Ply:CrosshairDisable()
end
elseif iname == "Open" then
if value ~= 0 then
self.Pod:Fire("Open", "1", 0)
else
self.Pod:Fire("Close", "1", 0)
end
end
end
function ENT:GetBeaconPos(sensor)
return self.VPos
end
-- Duplicator support to save pod link (TAD2020)
function ENT:BuildDupeInfo()
local info = self.BaseClass.BuildDupeInfo(self) or {}
if self.Pod and self.Pod:IsValid() then
info.pod = self.Pod:EntIndex()
end
return info
end
function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
self.BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID)
if info.pod then
self.Pod = GetEntByID(info.pod)
if not (self.Pod and self.Pod:IsValid()) then
self.Pod = ents.GetByIndex(info.pod)
end
end
end