Permalink
Browse files

Made the default Laser Tracer better

If applied to a SWEP, it shoots from proper places. Should be a good
example of custom tracer effects.
  • Loading branch information...
1 parent 41f314f commit c4348c320e920f6447ca351f05c4225bc48b813e @robotboy655 robotboy655 committed Jun 20, 2015
Showing with 41 additions and 33 deletions.
  1. +41 −33 garrysmod/gamemodes/sandbox/entities/effects/lasertracer.lua
@@ -1,67 +1,75 @@
-
EFFECT.Mat = Material( "effects/spark" )
--[[---------------------------------------------------------
- Init( data table )
+ Init( data table )
-----------------------------------------------------------]]
function EFFECT:Init( data )
- self.StartPos = data:GetStart()
- self.EndPos = data:GetOrigin()
- self.Dir = self.EndPos - self.StartPos
-
-
+ self.StartPos = data:GetStart()
+ self.EndPos = data:GetOrigin()
+
+ local ent = data:GetEntity()
+ local att = data:GetAttachment()
+
+ if ( IsValid( ent ) && att > 0 ) then
+ if ( ent.Owner == LocalPlayer() && !LocalPlayer():GetViewModel() != LocalPlayer() ) then ent = ent.Owner:GetViewModel() end
+
+ local att = ent:GetAttachment( att )
+ if ( att ) then
+ self.StartPos = att.Pos
+ end
+ end
+
+ self.Dir = self.EndPos - self.StartPos
+
self:SetRenderBoundsWS( self.StartPos, self.EndPos )
-
- self.TracerTime = math.Rand( 0.2, 0.3 )
+
+ self.TracerTime = math.min( 1, self.StartPos:Distance( self.EndPos ) / 10000 )//math.Rand( 0.2, 0.3 )
self.Length = math.Rand( 0.1, 0.15 )
-
+
-- Die when it reaches its target
self.DieTime = CurTime() + self.TracerTime
-
+
end
--[[---------------------------------------------------------
- THINK
+ Think
-----------------------------------------------------------]]
-function EFFECT:Think( )
+function EFFECT:Think()
if ( CurTime() > self.DieTime ) then
-- Awesome End Sparks
local effectdata = EffectData()
- effectdata:SetOrigin( self.EndPos + self.Dir:GetNormalized() * -2 )
- effectdata:SetNormal( self.Dir:GetNormalized() * -3 )
- effectdata:SetMagnitude( 2 )
- effectdata:SetScale( 3 )
- effectdata:SetRadius( 6 )
+ effectdata:SetOrigin( self.EndPos + self.Dir:GetNormalized() * -2 )
+ effectdata:SetNormal( self.Dir:GetNormalized() * -3 )
+ effectdata:SetMagnitude( 1 )
+ effectdata:SetScale( 1 )
+ effectdata:SetRadius( 6 )
util.Effect( "Sparks", effectdata )
-
+
return false
end
-
+
return true
end
--[[---------------------------------------------------------
- Draw the effect
+ Draw the effect
-----------------------------------------------------------]]
-function EFFECT:Render( )
+function EFFECT:Render()
- local fDelta = (self.DieTime - CurTime()) / self.TracerTime
+ local fDelta = ( self.DieTime - CurTime() ) / self.TracerTime
fDelta = math.Clamp( fDelta, 0, 1 ) ^ 0.5
-
+
render.SetMaterial( self.Mat )
-
+
local sinWave = math.sin( fDelta * math.pi )
-
- render.DrawBeam( self.EndPos - self.Dir * (fDelta - sinWave * self.Length ),
- self.EndPos - self.Dir * (fDelta + sinWave * self.Length ),
- 2 + sinWave * 16,
- 1,
- 0,
- Color( 0, 255, 0, 255 ) )
-
+
+ render.DrawBeam( self.EndPos - self.Dir * ( fDelta - sinWave * self.Length ),
+ self.EndPos - self.Dir * ( fDelta + sinWave * self.Length ),
+ 2 + sinWave * 16, 1, 0, Color( 0, 255, 0, 255 ) )
+
end

0 comments on commit c4348c3

Please sign in to comment.