wiremod-svn-archive/wire/lua/entities/gmod_wire_sensor/init.lua

147 lines
3.7 KiB
Lua
Raw Normal View History

2007-02-04 01:16:06 +00:00
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')
ENT.WireDebugName = "Distance"
local MODEL = Model( "models/props_lab/huladoll.mdl" )
function ENT:Initialize()
self.Entity:SetModel( MODEL )
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )
self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" })
end
function ENT:Setup(xyz_mode, outdist, outbrng)
self.XYZMode = xyz_mode
self.PrevOutput = nil
self.Value = 0
self.OutDist = outdist
self.OutBrng = outbrng
local onames = {}
if (outdist) then
2007-02-07 03:45:44 +00:00
table.insert(onames, "Distance")
end
if (xyz_mode) then
table.insert(onames, "X")
table.insert(onames, "Y")
table.insert(onames, "Z")
2007-02-04 01:16:06 +00:00
end
if (outbrng) then
table.insert(onames, "Bearing")
table.insert(onames, "Elevation")
end
Wire_AdjustOutputs(self.Entity, onames)
2007-02-07 03:45:44 +00:00
self:TriggerOutputs(0, Angle(0, 0, 0),Vector(0, 0, 0))
2007-02-04 01:16:06 +00:00
self:ShowOutput()
end
function ENT:Think()
self.BaseClass.Think(self)
if( !self.ToSense or !self.ToSense:IsValid() ) then return end
if (self.Active) then
local dist = 0
2007-02-07 03:45:44 +00:00
local distc = Vector(0, 0, 0)
2007-02-04 01:16:06 +00:00
local brng = Angle(0, 0, 0)
local MyPos = self.Entity:GetPos()
local BeaconPos = self.ToSense:GetBeaconPos(self.Entity)
if (self.OutDist) then
2007-02-07 03:45:44 +00:00
dist = (BeaconPos-MyPos):Length()
end
if (self.XYZMode) then
local DeltaPos = self.Entity:WorldToLocal(BeaconPos)
distc = Vector(-DeltaPos.y, DeltaPos.x, DeltaPos.z)
2007-02-04 01:16:06 +00:00
end
if (self.OutBrng) then
local DeltaPos = self.Entity:WorldToLocal(BeaconPos)
brng = DeltaPos:Angle()
end
2007-02-07 03:45:44 +00:00
self:TriggerOutputs(dist, brng, distc)
2007-02-04 01:16:06 +00:00
self:ShowOutput()
self.Entity:NextThink(CurTime()+0.04)
return true
end
end
function ENT:ShowOutput()
local txt = "Beacon Sensor"
if (self.OutDist) then
2007-02-07 03:45:44 +00:00
txt = txt .. "\nDistance = " .. math.Round(self.Outputs.Distance.Value*1000)/1000
end
if (self.XYZMode) then
txt = txt .. "\nOffset = " .. math.Round(self.Outputs.X.Value*1000)/1000 .. "," .. math.Round(self.Outputs.Y.Value*1000)/1000 .. "," .. math.Round(self.Outputs.Z.Value*1000)/1000
2007-02-04 01:16:06 +00:00
end
if (self.OutBrng) then
txt = txt .. "\nBearing = " .. math.Round(self.Outputs.Bearing.Value*1000)/1000 .. "," .. math.Round(self.Outputs.Elevation.Value*1000)/1000
end
self:SetOverlayText(txt)
end
2007-02-07 04:07:36 +00:00
function ENT:OnRestore()
//this is to prevent old save breakage
if (!self.Outputs.Distance.Value) then
self:Setup(self.XYZMode, self.OutDist, self.OutBrng)
end
self.BaseClass.OnRestore(self)
end
2007-02-04 01:16:06 +00:00
2007-02-07 03:45:44 +00:00
function ENT:TriggerOutputs(dist, brng, distc)
Wire_TriggerOutput(self.Entity, "Distance", dist)
Wire_TriggerOutput(self.Entity, "X", distc.x)
Wire_TriggerOutput(self.Entity, "Y", distc.y)
Wire_TriggerOutput(self.Entity, "Z", distc.z)
2007-02-04 01:16:06 +00:00
local pitch = brng.p
local yaw = brng.y
if (pitch > 180) then pitch = pitch - 360 end
if (yaw > 180) then yaw = yaw - 360 end
Wire_TriggerOutput(self.Entity, "Bearing", -yaw)
Wire_TriggerOutput(self.Entity, "Elevation", -pitch)
end
function ENT:SetBeacon(beacon)
if (beacon) and (beacon:IsValid()) then
self.ToSense = beacon
self.Active = true
else
self.ToSense = nil
self.Active = false
end
end
function ENT:BuildDupeInfo()
local info = self.BaseClass.BuildDupeInfo(self) or {}
if (self.ToSense) and (self.ToSense:IsValid()) then
info.to_sense = self.ToSense:EntIndex()
end
return info
end
function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID, GetConstByID)
self.BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID, GetConstByID)
if (info.to_sense) then
self.ToSense = GetEntByID(info.to_sense)
end
end