Guest User

Untitled

a guest
Aug 9th, 2015
12
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. AddCSLuaFile()
  3.  
  4. ENT.Type = "anim"
  5. ENT.Base = "base_anim"
  6.  
  7. ENT.PrintName = "Lightsaber"
  8. ENT.Category = "Robotboy655's Entities"
  9.  
  10. ENT.Editable = true
  11. ENT.Spawnable = true
  12.  
  13. ENT.RenderGroup = RENDERGROUP_TRANSLUCENT
  14.  
  15. function ENT:SetupDataTables()
  16. self:NetworkVar( "Float", 0, "BladeLength" )
  17. self:NetworkVar( "Float", 1, "BladeWidth", { KeyName = "BladeWidth", Edit = { type = "Float", category = "Blade", min = 2, max = 4, order = 1 } } )
  18. self:NetworkVar( "Float", 2, "MaxLength", { KeyName = "MaxLength", Edit = { type = "Float", category = "Blade", min = 32, max = 64, order = 2 } } )
  19.  
  20. self:NetworkVar( "Bool", 0, "Enabled" )
  21. self:NetworkVar( "Bool", 1, "DarkInner", { KeyName = "DarkInner", Edit = { type = "Boolean", category = "Blade", order = 3 } } )
  22.  
  23. self:NetworkVar( "Vector", 0, "CrystalColor", { KeyName = "CrystalColor", Edit = { type = "VectorColor", category = "Hilt", order = 4 } } )
  24.  
  25. if ( SERVER ) then
  26. self:SetBladeLength( 0 )
  27. self:SetBladeWidth( 2 )
  28. self:SetMaxLength( 42 )
  29.  
  30. self:SetDarkInner( false )
  31. self:SetEnabled( true )
  32. end
  33. end
  34.  
  35. function ENT:Initialize()
  36.  
  37. //self.BladeWidth = self.BladeWidth or self:GetBladeWidth()
  38. //self.MaxLength = self.MaxLength or self:GetMaxLength()
  39.  
  40. if ( SERVER ) then
  41. self:PhysicsInit( SOLID_VPHYSICS )
  42. self:SetMoveType( MOVETYPE_VPHYSICS )
  43. self:SetSolid( SOLID_VPHYSICS )
  44.  
  45. self.LoopSound = self.LoopSound or "lightsaber/SaberLoop" .. math.random( 1, 8 ) .. ".wav"
  46. self.SwingSound = self.SwingSound or "lightsaber/SaberSwing" .. math.random( 1, 2 ) .. ".wav"
  47. self.OnSound = self.OnSound or "lightsaber/SaberOn" .. math.random( 1, 2 ) .. ".wav"
  48. self.OffSound = self.OffSound or "lightsaber/SaberOff" .. math.random( 1, 2 ) .. ".wav"
  49. //self.Enabled = self:GetEnabled()
  50.  
  51. if ( self:GetEnabled() ) then self:EmitSound( self.OnSound ) end
  52.  
  53. self.SoundSwing = CreateSound( self, Sound( self.SwingSound ) )
  54. if ( self.SoundSwing ) then self.SoundSwing:Play() self.SoundSwing:ChangeVolume( 0, 0 ) end
  55.  
  56. self.SoundHit = CreateSound( self, Sound( "lightsaber/SaberHit.wav" ) )
  57. if ( self.SoundHit ) then self.SoundHit:Play() self.SoundHit:ChangeVolume( 0, 0 ) end
  58.  
  59. self.SoundLoop = CreateSound( self, Sound( self.LoopSound ) )
  60. if ( self.SoundLoop ) then self.SoundLoop:Play() end
  61. else
  62. self:SetRenderBounds( Vector( -self:GetBladeLength(), -128, -128 ), Vector( self:GetBladeLength(), 128, 128 ) )
  63.  
  64. language.Add( self.ClassName, self.PrintName )
  65. killicon.AddAlias( "ent_lightsaber", "weapon_lightsaber" )
  66. end
  67. end
  68.  
  69. function ENT:OnRemove()
  70. if ( CLIENT ) then rb655_SaberClean( self:EntIndex() ) return end
  71.  
  72. if ( self.SoundLoop ) then self.SoundLoop:Stop() self.SoundLoop = nil end
  73. if ( self.SoundSwing ) then self.SoundSwing:Stop() self.SoundSwing = nil end
  74. if ( self.SoundHit ) then self.SoundHit:Stop() self.SoundHit = nil end
  75.  
  76. if ( self:GetEnabled() ) then self:EmitSound( self.OffSound ) end
  77. end
  78.  
  79. function ENT:GetSaberPosAng( num )
  80. num = num or 1
  81.  
  82. local attachment = self:LookupAttachment( "blade" .. num )
  83. if ( attachment > 0 ) then
  84. local PosAng = self:GetAttachment( attachment )
  85.  
  86. return PosAng.Pos, PosAng.Ang:Forward()
  87. end
  88.  
  89. return self:LocalToWorld( Vector( 1, -0.58, -0.25 ) ), -self:GetAngles():Forward()
  90.  
  91. end
  92.  
  93. function ENT:Draw()
  94.  
  95. //render.SetColorModulation( 1, 1, 1 )
  96. self:DrawModel()
  97.  
  98. local clr = self:GetCrystalColor() * 255
  99. clr = Color( clr.x, clr.y, clr.z )
  100.  
  101. local pos, ang = self:GetSaberPosAng()
  102. rb655_RenderBlade( pos, ang, self:GetBladeLength(), self:GetMaxLength(), self:GetBladeWidth(), clr, self:GetDarkInner(), self:EntIndex(), self:WaterLevel() > 2 )
  103. if ( self:LookupAttachment( "blade2" ) > 0 ) then
  104. local pos, ang = self:GetSaberPosAng( 2 )
  105. rb655_RenderBlade( pos, ang, self:GetBladeLength(), self:GetMaxLength(), self:GetBladeWidth(), clr, self:GetDarkInner(), self:EntIndex() + 655, self:WaterLevel() > 2 )
  106. end
  107.  
  108. end
  109.  
  110. if ( CLIENT ) then return end
  111.  
  112. function ENT:OnTakeDamage( dmginfo )
  113.  
  114. -- React physically when shot/getting blown
  115. self:TakePhysicsDamage( dmginfo )
  116.  
  117. end
  118.  
  119. function ENT:Think()
  120.  
  121. if ( !self:GetEnabled() && self:GetBladeLength() != 0 ) then
  122. self:SetBladeLength( math.Approach( self:GetBladeLength(), 0, 2 ) )
  123. elseif ( self:GetEnabled() && self:GetBladeLength() != self:GetMaxLength() ) then
  124. self:SetBladeLength( math.Approach( self:GetBladeLength(), self:GetMaxLength(), 8 ) )
  125. end
  126.  
  127. if ( self:GetBladeLength() <= 0 ) then
  128. if ( self.SoundSwing ) then self.SoundSwing:ChangeVolume( 0, 0 ) end
  129. if ( self.SoundLoop ) then self.SoundLoop:ChangeVolume( 0, 0 ) end
  130. if ( self.SoundHit ) then self.SoundHit:ChangeVolume( 0, 0 ) end
  131. return
  132. end
  133.  
  134. local pos, ang = self:GetSaberPosAng()
  135. local hit = self:BladeThink( pos, ang )
  136. if ( self:LookupAttachment( "blade2" ) > 0 ) then
  137. local pos2, ang2 = self:GetSaberPosAng( 2 )
  138. local hit_2 = self:BladeThink( pos2, ang2 )
  139. hit = hit or hit_2
  140. end
  141.  
  142. if ( self.SoundHit ) then
  143. if ( hit ) then self.SoundHit:ChangeVolume( math.Rand( 0.1, 0.5 ), 0 ) else self.SoundHit:ChangeVolume( 0, 0 ) end
  144. end
  145.  
  146. if ( self.SoundSwing ) then
  147. //local ang = self:GetAngles()
  148. if ( self.LastAng != ang ) then
  149. self.LastAng = self.LastAng or ang
  150. self.SoundSwing:ChangeVolume( math.Clamp( ang:Distance( self.LastAng ) / 2, 0, 1 ), 0 )
  151. //self.SoundSwing:ChangeVolume( math.Rand( 0, 1 ), 0 ) -- For some reason if I spam always 1, the sound doesn't loop
  152. //self.SoundSwing:ChangeVolume( math.min( pos:Distance( self.LastPos ) / 16, 1 ), 0 )
  153. end
  154. self.LastAng = ang
  155. end
  156.  
  157. if ( self.SoundLoop ) then
  158. local pos = pos + ang * self:GetBladeLength()
  159. if ( self.LastPos != pos ) then
  160. self.LastPos = self.LastPos or pos
  161. self.SoundLoop:ChangeVolume( 0.1 + math.Clamp( pos:Distance( self.LastPos ) / 32, 0, 0.2 ), 0 )
  162. //self.SoundLoop:ChangeVolume( 0.1 + math.Clamp( pos:Distance( self.LastPos ) / 32, 0, 0.2 ), 0 )
  163. //self.SoundLoop:ChangeVolume( 1 - math.min( pos:Distance( self.LastPos ) / 16, 1 ), 0 )
  164. //self.SoundLoop:ChangeVolume( self:GetBladeLength() / self:GetMaxLength(), 0 )
  165. end
  166. self.LastPos = pos
  167. end
  168.  
  169. self:NextThink( CurTime() )
  170. return true
  171. end
  172.  
  173. function ENT:BladeThink( startpos, dir )
  174. local trace = util.TraceHull( {
  175. start = startpos,
  176. endpos = startpos + dir * self:GetBladeLength(),
  177. filter = self,
  178. /*mins = Vector( -1, -1, -1 ) * self:GetBladeWidth() / 2,
  179. maxs = Vector( 1, 1, 1 ) * self:GetBladeWidth() / 2*/
  180. } )
  181.  
  182. if ( trace.Hit ) then
  183. rb655_DrawHit( trace.HitPos, trace.HitNormal )
  184. rb655_LS_DoDamage( trace, self )
  185. end
  186.  
  187. return trace.Hit
  188. end
  189.  
  190. function ENT:Use( activator, caller, useType, value )
  191. if ( !IsValid( activator ) || !activator:KeyPressed( IN_USE ) ) then return end
  192.  
  193. if ( self:GetEnabled() ) then
  194. self:EmitSound( self.OffSound )
  195. else
  196. self:EmitSound( self.OnSound )
  197. end
  198.  
  199. self:SetEnabled( !self:GetEnabled() )
  200. end
  201.  
  202. function ENT:SpawnFunction( ply, tr )
  203. if ( !tr.Hit || !ply:CheckLimit( "ent_lightsabers" ) ) then return end
  204.  
  205. local ent = ents.Create( ClassName )
  206. ent:SetPos( tr.HitPos + tr.HitNormal * 2 )
  207.  
  208. local ang = ply:EyeAngles()
  209. ang.p = 0
  210. ang:RotateAroundAxis( ang:Right(), 180 )
  211. ent:SetAngles( ang )
  212.  
  213. // Sync values fro the tool
  214. ent:SetMaxLength( math.Clamp( ply:GetInfoNum( "rb655_lightsaber_bladel", 42 ), 32, 64 ) )
  215. ent:SetCrystalColor( Vector( ply:GetInfo( "rb655_lightsaber_red" ), ply:GetInfo( "rb655_lightsaber_green" ), ply:GetInfo( "rb655_lightsaber_blue" ) ) / 255 )
  216. ent:SetDarkInner( ply:GetInfo( "rb655_lightsaber_dark" ) == "1" )
  217. ent:SetModel( ply:GetInfo( "rb655_lightsaber_model" ) )
  218. ent:SetBladeWidth( math.Clamp( ply:GetInfoNum( "rb655_lightsaber_bladew", 2 ), 2, 4 ) )
  219.  
  220. ent.LoopSound = ply:GetInfo( "rb655_lightsaber_humsound" )
  221. ent.SwingSound = ply:GetInfo( "rb655_lightsaber_swingsound" )
  222. ent.OnSound = ply:GetInfo( "rb655_lightsaber_onsound" )
  223. ent.OffSound = ply:GetInfo( "rb655_lightsaber_offsound" )
  224.  
  225. ent:Spawn()
  226. ent:Activate()
  227.  
  228. ent.Owner = ply
  229. ent.Color = ent:GetColor()
  230.  
  231. local phys = ent:GetPhysicsObject()
  232. if ( IsValid( phys ) ) then phys:Wake() end
  233.  
  234. if ( IsValid( ply ) ) then
  235. ply:AddCount( "ent_lightsabers", ent )
  236. ply:AddCleanup( "ent_lightsabers", ent )
  237. end
  238.  
  239. return ent
  240. end
RAW Paste Data