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

183 lines
4.7 KiB
Lua
Raw Normal View History

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')
ENT.WireDebugName = "Lamp"
local MODEL = Model( "models/props_wasteland/prison_lamp001c.mdl" )
AccessorFunc( ENT, "m_strFlashlightTexture", "FlashlightTexture" )
ENT:SetFlashlightTexture( "effects/flashlight001" )
/*---------------------------------------------------------
Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
self:SetModel( MODEL )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
self.r = 255
self.g = 255
self.b = 255
self.Inputs = Wire_CreateInputs(self.Entity, { "Red", "Green", "Blue", "On" })
self:TurnOn()
end
/*---------------------------------------------------------
Name: Sets the color of the light
---------------------------------------------------------*/
function ENT:SetLightColor( r, g, b )
self.r = r
self.g = g
self.b = b
self:SetVar( "lightr", r )
self:SetVar( "lightg", g )
self:SetVar( "lightb", b )
self:SetColor( r, g, b, 255 )
self.Entity:SetColor( r, g, b, 255 )
self.m_strLightColor = Format( "%i %i %i", r, g, b )
if ( self.flashlight ) then
self.flashlight:SetKeyValue( "lightcolor", self.m_strLightColor )
end
self:SetOverlayText( "Red:" .. r .. " Green:" .. g .. " Blue" .. b )
end
function ENT:Setup( r, g, b )
self:SetLightColor( r, g, b )
end
/*---------------------------------------------------------
Name: Sets the texture
---------------------------------------------------------*/
function ENT:SetFlashlightTexture( tex )
self.m_strFlashlightTexture = tex
if ( self.flashlight ) then
self.flashlight:Input( "SpotlightTexture", NULL, NULL, self:GetFlashlightTexture() )
end
end
/*---------------------------------------------------------
Name: OnTakeDamage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )
self.Entity:TakePhysicsDamage( dmginfo )
end
/*---------------------------------------------------------
Name: TriggerInput
Desc: the inputs
---------------------------------------------------------*/
function ENT:TriggerInput(iname, value)
if (iname == "Red") then
self:SetLightColor( value, self.g, self.b )
elseif (iname == "Green") then
self:SetLightColor( self.r, value, self.b )
elseif (iname == "Blue") then
self:SetLightColor( self.r, self.g, value )
elseif (iname == "On") then
if value > 0 then
self:TurnOn()
else
self:TurnOff()
end
end
end
function ENT:TurnOn()
self:SetOn(true)
local angForward = self.Entity:GetAngles() + Angle( 90, 0, 0 )
self.flashlight = ents.Create( "env_projectedtexture" )
self.flashlight:SetParent( self.Entity )
// The local positions are the offsets from parent..
self.flashlight:SetLocalPos( Vector( 0, 0, 0 ) )
self.flashlight:SetLocalAngles( Angle(90,90,90) )
// Looks like only one flashlight can have shadows enabled!
self.flashlight:SetKeyValue( "enableshadows", 1 )
self.flashlight:SetKeyValue( "farz", 2048 )
self.flashlight:SetKeyValue( "nearz", 8 )
//Todo: Make this tweakable?
self.flashlight:SetKeyValue( "lightfov", 50 )
// Color.. Bright pink if none defined to alert us to error
self.flashlight:SetKeyValue( "lightcolor", self.m_strLightColor or "255 0 255" )
self.flashlight:Spawn()
self.flashlight:Input( "SpotlightTexture", NULL, NULL, self:GetFlashlightTexture() )
end
function ENT:TurnOff()
self:SetOn(false)
SafeRemoveEntity( self.flashlight )
self.flashlight = nil
end
function ENT:OnRemove()
Wire_Remove(self.Entity)
end
function ENT:OnRestore()
Wire_Restored(self.Entity)
end
include('shared.lua')
2008-01-18 01:36:18 +00:00
function MakeWireLamp( pl, r, g, b, Texture, Data )
if ( !pl:CheckLimit( "wire_lamps" ) ) then return false end
local wire_lamp = ents.Create( "gmod_wire_lamp" )
if (!wire_lamp:IsValid()) then return end
2008-01-18 01:36:18 +00:00
wire_lamp:SetFlashlightTexture( Texture or "effects/flashlight001" )
duplicator.DoGeneric( wire_lamp, Data )
wire_lamp:SetLightColor( r, g, b )
wire_lamp:Spawn()
2008-01-18 01:36:18 +00:00
duplicator.DoGenericPhysics( wire_lamp, pl, Data )
wire_lamp:SetPlayer( pl )
2008-01-18 01:36:18 +00:00
wire_lamp.lightr = r
wire_lamp.lightg = g
wire_lamp.lightb = b
wire_lamp.Texture = Texture or "effects/flashlight001"
pl:AddCount( "wire_lamps", wire_lamp )
pl:AddCleanup( "wire_lamp", wire_lamp )
return wire_lamp
end
2008-01-18 01:36:18 +00:00
duplicator.RegisterEntityClass( "gmod_wire_lamp", MakeWireLamp, "lightr", "lightg", "lightb", "Texture", "Data" )