Guest User

smoke grenade

a guest
May 23rd, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. ttt_smokegrenade_proj.lua
  2.  
  3.  
  4. AddCSLuaFile()
  5. ENT.Type = "anim"
  6. ENT.Base = "ttt_basegrenade_proj"
  7. ENT.Model = Model("models/weapons/w_eq_smokegrenade_thrown.mdl")
  8.  
  9.  
  10. AccessorFunc( ENT, "radius", "Radius", FORCE_NUMBER )
  11.  
  12. function ENT:SetupDataTables()
  13. self:NetworkVar("Float", 0, "ExplodeTime")
  14. self:NetworkVar("Bool", 0, "Impact")
  15. end
  16.  
  17. function ENT:Initialize()
  18. if not self:GetRadius() then self:SetRadius(100) end
  19.  
  20. self:SetModel(self.Model)
  21.  
  22. self:PhysicsInit(SOLID_VPHYSICS)
  23. self:SetMoveType(MOVETYPE_VPHYSICS)
  24. self:SetSolid(SOLID_BBOX)
  25. self:SetCollisionGroup(COLLISION_GROUP_PROJECTILE)
  26. if SERVER then
  27. self:SetExplodeTime(0)
  28. self:SetImpact(true)
  29. end
  30. end
  31.  
  32. function ENT:Setsmokeimp(t)
  33. print(t)
  34. self:SetImpact(t)
  35. end
  36.  
  37.  
  38.  
  39. if CLIENT then
  40.  
  41. local smokeparticles = {
  42. Model("particle/particle_smokegrenade"),
  43. Model("particle/particle_noisesphere")
  44. };
  45.  
  46. function ENT:CreateSmoke(center)
  47. local em = ParticleEmitter(center)
  48.  
  49. local r = self:GetRadius()
  50. for i=1, 20 do
  51. local prpos = VectorRand() * r
  52. prpos.z = prpos.z + 30
  53. local p = em:Add(table.Random(smokeparticles), center + prpos)
  54. if p then
  55. local gray = math.random(75, 200)
  56. p:SetColor(gray, gray, gray)
  57. p:SetStartAlpha(255)
  58. p:SetEndAlpha(200)
  59. p:SetVelocity(VectorRand() * math.Rand(900, 1300))
  60. p:SetLifeTime(0)
  61.  
  62. p:SetDieTime(math.Rand(100, 140))
  63.  
  64. p:SetStartSize(math.random(280, 300))
  65. p:SetEndSize(math.random(1, 40))
  66. p:SetRoll(math.random(-180, 180))
  67. p:SetRollDelta(math.Rand(-0.1, 0.1))
  68. p:SetAirResistance(600)
  69.  
  70. p:SetCollide(true)
  71. p:SetBounce(0.4)
  72.  
  73. p:SetLighting(false)
  74. end
  75. end
  76.  
  77. em:Finish()
  78. end
  79. end
  80.  
  81. function ENT:Explode(tr)
  82. if SERVER then
  83. print("server?")
  84. self:SetNoDraw(true)
  85. self:SetSolid(SOLID_NONE)
  86.  
  87. -- pull out of the surface
  88. if tr.Fraction != 1.0 then
  89. self:SetPos(tr.HitPos + tr.HitNormal * 0.6)
  90. end
  91.  
  92. local pos = self:GetPos()
  93.  
  94. self:Remove()
  95. else
  96. print("else?")
  97. local spos = self:GetPos()
  98. local trs = util.TraceLine({start=spos + Vector(0,0,64), endpos=spos + Vector(0,0,-128), filter=self})
  99. util.Decal("SmallScorch", trs.HitPos + trs.HitNormal, trs.HitPos - trs.HitNormal)
  100.  
  101. --self:SetDetonateExact(0)
  102.  
  103. if tr.Fraction != 1.0 then
  104. spos = tr.HitPos + tr.HitNormal * 0.6
  105. end
  106.  
  107. -- Smoke particles can't get cleaned up when a round restarts, so prevent
  108. -- them from existing post-round.
  109. if GetRoundState() == ROUND_POST then return end
  110.  
  111. self:CreateSmoke(spos)
  112. end
  113. end
  114.  
  115. function ENT:Think()
  116. etime = self:GetExplodeTime() or 0
  117. if etime != 0 and etime < CurTime() then
  118. -- if thrower disconnects before grenade explodes, just don't explode
  119. if SERVER and (not IsValid(self:GetThrower())) then
  120. self:Remove()
  121. etime = 0
  122. return
  123. end
  124.  
  125. -- find the ground if it's near and pass it to the explosion
  126. local spos = self:GetPos()
  127. local tr = util.TraceLine({start=spos, endpos=spos + Vector(0,0,-32), mask=MASK_SHOT_HULL, filter=self.thrower})
  128.  
  129. local success, err = pcall(self.Explode, self, tr)
  130. if not success then
  131. -- prevent effect spam on Lua error
  132. self:Remove()
  133. ErrorNoHalt("ERROR CAUGHT: ttt_basegrenade_proj: " .. err .. "\n")
  134. end
  135. end
  136. end
  137.  
  138. function ENT:PhysicsCollide( data, physobj )
  139. if self:GetImpact() == true then
  140. self:SetExplodeTime(CurTime())
  141. print(self:GetExplodeTime())
  142. end
  143. end
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. weapon_ttt_smokegrenade.lua
  153.  
  154. AddCSLuaFile()
  155.  
  156. SWEP.HoldType = "grenade"
  157.  
  158. if CLIENT then
  159. SWEP.PrintName = "grenade_smoke"
  160. SWEP.Slot = 3
  161.  
  162. SWEP.ViewModelFlip = false
  163. SWEP.ViewModelFOV = 54
  164.  
  165. SWEP.Icon = "vgui/ttt/icon_nades"
  166. SWEP.IconLetter = "Q"
  167.  
  168. SWEP.EquipMenuData = {
  169. type = "Grenade",
  170. desc = "Smoke grenade, you should know what this is.\n!Right click Instantly detonates!."
  171. }
  172. end
  173.  
  174. SWEP.Base = "weapon_tttbasegrenade"
  175.  
  176. SWEP.WeaponID = AMMO_SMOKE
  177. SWEP.Kind = WEAPON_NADE
  178.  
  179. SWEP.CanBuy = { ROLE_SURVIVALIST }
  180.  
  181. SWEP.UseHands = true
  182. SWEP.ViewModel = "models/weapons/cstrike/c_eq_smokegrenade.mdl"
  183. SWEP.WorldModel = "models/weapons/w_eq_smokegrenade.mdl"
  184.  
  185. SWEP.Weight = 5
  186. SWEP.AutoSpawnable = true
  187. SWEP.Spawnable = true
  188. SWEP.RCImpact = false
  189. -- really the only difference between grenade weapons: the model and the thrown
  190. -- ent.
  191.  
  192. function SWEP:GetGrenadeName()
  193. return "ttt_smokegrenade_proj"
  194. end
  195.  
  196. function SWEP:SecondaryAttack()
  197. self:SetNextSecondaryFire(CurTime() + self.Primary.Delay)
  198.  
  199. if GetRoundState() == ROUND_PREP and GetConVar("ttt_no_nade_throw_during_prep"):GetBool() then
  200. return
  201. end
  202. self.RCImpact = true
  203. self:PullPin(0)
  204. end
  205.  
  206. function SWEP:Throw()
  207. if CLIENT then
  208. self:SetThrowTime(0)
  209. elseif SERVER then
  210. local ply = self:GetOwner()
  211. if not IsValid(ply) then return end
  212.  
  213. if self.was_thrown then return end
  214.  
  215. self.was_thrown = true
  216.  
  217. local ang = ply:EyeAngles()
  218. local src = ply:GetPos() + (ply:Crouching() and ply:GetViewOffsetDucked() or ply:GetViewOffset())+ (ang:Forward() * 8) + (ang:Right() * 10)
  219. local target = ply:GetEyeTraceNoCursor().HitPos
  220. local tang = (target-src):Angle() -- A target angle to actually throw the grenade to the crosshair instead of fowards
  221. -- Makes the grenade go upgwards
  222. if tang.p < 90 then
  223. tang.p = -10 + tang.p * ((90 + 10) / 90)
  224. else
  225. tang.p = 360 - tang.p
  226. tang.p = -10 + tang.p * -((90 + 10) / 90)
  227. end
  228. tang.p=math.Clamp(tang.p,-90,90) -- Makes the grenade not go backwards :/
  229. local vel = math.min(800, (90 - tang.p) * 6)
  230. local thr = tang:Forward() * vel + ply:GetVelocity()
  231. if self.RCImpact then
  232. self:CreateGrenade(src, Angle(0,0,0), thr, Vector(600, math.random(-1200, 1200), 0), ply, true)
  233. elseif not self.RCImpact then
  234. self:CreateGrenade(src, Angle(0,0,0), thr, Vector(600, math.random(-1200, 1200), 0), ply, false)
  235. else
  236. print("Bruh")
  237. end
  238.  
  239. self:SetThrowTime(0)
  240. self:Remove()
  241. end
  242. end
  243.  
  244.  
  245. function SWEP:CreateGrenade(src, ang, vel, angimp, ply, impact)
  246. local gren = ents.Create(self:GetGrenadeName())
  247. if not IsValid(gren) then return end
  248.  
  249. gren:SetPos(src)
  250. gren:SetAngles(ang)
  251.  
  252. -- gren:SetVelocity(vel)
  253. gren:SetOwner(ply)
  254. gren:SetThrower(ply)
  255.  
  256. gren:SetGravity(0.4)
  257. gren:SetFriction(0.2)
  258. gren:SetElasticity(0.45)
  259.  
  260. gren:Spawn()
  261.  
  262. gren:PhysWake()
  263.  
  264. local phys = gren:GetPhysicsObject()
  265. if IsValid(phys) then
  266. phys:SetVelocity(vel)
  267. phys:AddAngleVelocity(angimp)
  268. end
  269.  
  270. gren:SetDetonateExact(self:GetDetTime())
  271.  
  272. -- This has to happen AFTER Spawn() calls gren's Initialize()
  273. if impact then
  274. gren:Setsmokeimp(true)
  275. elseif not impact then
  276. gren:Setsmokeimp(false)
  277. else
  278. print("Bruh")
  279. end
  280.  
  281. return gren
  282. end
Add Comment
Please, Sign In to add comment