Zaktak

squad_shield.lua

Nov 26th, 2020
506
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. AddCSLuaFile()
  2.  
  3. ENT.PrintName       = 'Squad Shield'
  4. ENT.Base            = 'base_gmodentity'
  5. ENT.Type            = 'anim'
  6. ENT.Model           = 'models/hunter/misc/shell2x2a.mdl'
  7. ENT.RenderGroup     = RENDERGROUP_OTHER
  8. ENT.Radius          = 47                                    -- Actual prop radius (Do not change this value!)
  9. ENT.SphereScale     = 6                                     -- Sphere scale
  10. ENT.LifeTime        = 60                                    -- Life time of shield activity, in seconds
  11. ENT.CoolDownTime    = 30                                    -- Cooldown time, in seconds
  12. ENT.Spawnable       = false
  13.  
  14.  
  15. function ENT:SetupDataTables()
  16.     self:NetworkVar('Entity',0,'ShieldOwner')
  17.     self:NetworkVar('Bool',0,'Active')
  18.     self:NetworkVar('Int',0,'TimeOffset')
  19. end
  20.  
  21.  
  22. if CLIENT then
  23.  
  24.     function ENT:Initialize()
  25.         local dlight = DynamicLight(self:EntIndex())
  26.         if dlight then
  27.             dlight.pos = self:GetPos()
  28.             dlight.r = 0
  29.             dlight.g = 64
  30.             dlight.b = 255
  31.             dlight.brightness = 2
  32.             dlight.Decay = 0
  33.             dlight.Size = 512
  34.             dlight.DieTime = CurTime()+self.LifeTime
  35.         end
  36.     end
  37. end
  38.  
  39.  
  40. if CLIENT then return end
  41.  
  42. local function updateCLTable()
  43.     net.Start("squad_shield_draw_circle")
  44.     net.Broadcast()
  45. end
  46.  
  47. function ENT:Initialize(owner)
  48.     if !IsValid(owner) then return end
  49.  
  50.     self:PhysicsInit(SOLID_NONE)
  51.     self:SetNoDraw(true)
  52.     self:PhysWake()
  53.     self:SetPos(owner:GetPos())
  54.     self:Spawn()
  55.     self:AddEffects(EF_NOSHADOW)
  56.     self:SetOwner(owner)
  57.     self:Activate()
  58.  
  59.     self.VisualProp = ents.Create('prop_dynamic')
  60.     self.VisualProp:SetModel('models/squadshield/squad_shield.mdl')
  61.     self.VisualProp:SetPos(owner:GetPos()+Vector(0,0,2))
  62.     self.VisualProp:SetParent(self)
  63.     self.VisualProp:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
  64.     self.VisualProp:AddEffects(EF_NOSHADOW)
  65.     self.VisualProp:Spawn()
  66.     self.VisualProp:Activate()
  67.  
  68.     self.BulletHolder = ents.Create('prop_physics')
  69.     self.BulletHolder:SetModel(self.Model)
  70.     self.BulletHolder:SetModelScale(self.SphereScale)
  71.     self.BulletHolder:SetNoDraw(true)
  72.     self.BulletHolder:SetPos(owner:GetPos())
  73.     self.BulletHolder:SetParent(self)
  74.     self.BulletHolder:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
  75.     self.BulletHolder:SetSolid(SOLID_BSP)
  76.     self.BulletHolder:AddEffects(EF_NOSHADOW)
  77.     self.BulletHolder:Spawn()
  78.     self.BulletHolder:Activate()
  79.     self.BulletHolder.PleaseKillMe = true
  80.  
  81.     self:SetShieldOwner(owner)
  82.     owner:SetNWEntity('CSquadShield',self)
  83.     self.SoundLoop = CreateSound(self,'ambient/levels/citadel/field_loop2.wav') --'ambient/machines/combine_shield_loop3.wav'
  84.     self.SoundLoop:SetSoundLevel(80)
  85.     self:ToggleShield(true)
  86.     self:SetTimeOffset(CurTime())
  87. end
  88.  
  89. function ENT:ToggleShield(bool)
  90.     self:SetActive(bool)
  91.     self.BulletHolder:SetSolid(bool and SOLID_BSP or SOLID_NONE)
  92.     local owner = self:GetShieldOwner()
  93.     if bool then
  94.         updateCLTable()
  95.         self.SoundLoop:PlayEx(0.32,64)
  96.         self:EmitSound('ambient/machines/thumper_hit.wav',80,64,.64)
  97.         self:EmitSound('npc/scanner/scanner_siren1.wav',80,88,.64)
  98.         self:EmitSound('ambient/levels/citadel/pod_open1.wav',80,100,.64)
  99.         owner:SetNWInt('CSqShCooldown',-1)
  100.     else
  101.         updateCLTable()
  102.         self.SoundLoop:Stop()
  103.         self:EmitSound('ambient/levels/canals/headcrab_canister_open1.wav',80,120,.64)
  104.         self:EmitSound('ambient/levels/citadel/pod_close1.wav',80,100,.64)
  105.         self.Destructable = CurTime()
  106.         owner:SetNWInt('CSqShCooldown',CurTime()+self.CoolDownTime)
  107.     end
  108. end
  109.  
  110. function ENT:Think()
  111.     if self:GetActive() and CurTime() >= self:GetTimeOffset()+self.LifeTime then
  112.         self:ToggleShield(false)
  113.     end
  114.     if !self:GetActive() and self.Destructable and CurTime() >= self.Destructable+1 then
  115.         self:Remove()
  116.     end
  117. end
  118.  
  119. function ENT:OnRemove()
  120.     self.SoundLoop:Stop()
  121.     self:GetShieldOwner():SetNWInt('CSqShCooldown',CurTime()+self.CoolDownTime)
  122. end
  123.  
  124. function ENT:OnTakeDamage(dmg)
  125.     if !self:GetActive() then return end
  126.     local ed = EffectData()
  127.     ed:SetOrigin(dmg:GetDamagePosition())
  128.     ed:SetNormal((dmg:GetDamagePosition()-self:GetPos()):GetNormalized())
  129.     ed:SetRadius(1)
  130.     util.Effect('cball_bounce',ed)
  131.     util.Effect('AR2Explosion',ed)
  132.     self:EmitSound(('ambient/energy/weld%s.wav'):format(math.random(1,2)),75,math.random(144,192),.64,CHAN_ITEM)
  133. end
RAW Paste Data