CapsAdmin

Untitled

Oct 27th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1. local PARTICLE = {}
  2. PARTICLE.__index = PARTICLE
  3.  
  4. class.GetSet(PARTICLE, "Pos", Vec3(0,0,0))
  5. class.GetSet(PARTICLE, "Velocity", Vec3(0,0,0))
  6. class.GetSet(PARTICLE, "Drag", 0.98)
  7. class.GetSet(PARTICLE, "Size", Vec2(1,1))
  8.  
  9. class.GetSet(PARTICLE, "StartSize", 10)
  10. class.GetSet(PARTICLE, "EndSize", 0)
  11.  
  12. class.GetSet(PARTICLE, "StartAlpha", 1)
  13. class.GetSet(PARTICLE, "EndAlpha", 0)
  14.  
  15. class.GetSet(PARTICLE, "LifeTime", 1)
  16. class.GetSet(PARTICLE, "Texture", NULL)
  17. class.GetSet(PARTICLE, "Color", Color(1,1,1,1))
  18.  
  19. function PARTICLE:SetLifeTime(n)
  20.     self.LifeTime = n
  21.     self.life_end = glfw.GetTime() + n
  22. end
  23.  
  24.  
  25.  
  26.  
  27. local EMITTER = {}
  28. EMITTER.__index = EMITTER
  29.  
  30. class.GetSet(EMITTER, "Rate", 0.1)
  31. class.GetSet(EMITTER, "EmitCount", 1)
  32. class.GetSet(EMITTER, "Mode2D", true)
  33. class.GetSet(EMITTER, "Pos", Vec3(0, 0, 0))
  34. class.GetSet(EMITTER, "Additive", true)
  35.  
  36. function EMITTER:IsValid()
  37.     return true
  38. end
  39.  
  40. local emitters = {}
  41.  
  42. function Emitter()
  43.     local self = setmetatable({}, EMITTER)
  44.    
  45.     self.particles = {}
  46.     self.last_emit = 0
  47.    
  48.     table.insert(emitters, self)
  49.    
  50.     return self
  51. end
  52.  
  53. function EMITTER:Remove()
  54.     for k,v in pairs(emitters) do
  55.         if v == self then
  56.             table.remove(emitters, k)
  57.             break
  58.         end
  59.     end
  60.    
  61.     utilities.MakeNULL(self)
  62. end
  63.  
  64. function EMITTER:Think(dt)
  65.     local time = glfw.GetTime()
  66.    
  67.     if self.Rate == 0 then
  68.         self:Emit()
  69.     else
  70.         if self.last_emit < time then
  71.             self:Emit()
  72.             self.last_emit = time + self.Rate
  73.         end
  74.     end
  75.    
  76.     for key, p in pairs(self.particles) do 
  77.         if p.life_end < time then
  78.             self.particles[key] = nil
  79.         else
  80.             -- velocity
  81.             p.Pos.x = p.Pos.x + (p.Velocity.x * dt)
  82.             p.Pos.y = p.Pos.y + (p.Velocity.y * dt)
  83.            
  84.             -- friction
  85.             p.Velocity.x = p.Velocity.x * p.Drag
  86.             p.Velocity.y = p.Velocity.y * p.Drag
  87.                
  88.             p.life_mult = math.clamp(p.life_end - time, 0, 1)
  89.         end
  90.     end
  91. end
  92.  
  93. function EMITTER:Draw()
  94.     local old = render.GetAdditive()
  95.     render.SetAdditive(self.Additive)
  96.    
  97.     if self.Mode2D then
  98.    
  99.         for _, p in pairs(self.particles) do
  100.             if p.Texture:IsValid() then
  101.                 surface.SetTexture(p.Texture)
  102.             else
  103.                 surface.SetWhiteTexture()
  104.             end
  105.        
  106.             local size = math.lerp(p.life_mult, p.EndSize, p.StartSize)
  107.             local alpha = math.lerp(p.life_mult, p.EndAlpha, p.StartAlpha)
  108.             local w = size * p.Size.x
  109.             local h = size * p.Size.y
  110.            
  111.             surface.Color(p.Color.r, p.Color.g, p.Color.b, p.Color.a * alpha)
  112.            
  113.             surface.DrawRect(
  114.                 p.Pos.x - w*0.5,
  115.                 p.Pos.y - h*0.5,
  116.                 w,
  117.                 h
  118.             )
  119.         end
  120.     else   
  121.         -- 3d here 
  122.     end
  123.    
  124.     render.SetAdditive(old)
  125. end  
  126.  
  127. function EMITTER:GetParticles()
  128.     return self.particles
  129. end
  130.  
  131. function EMITTER:Emit()
  132.     for i = 1, self.EmitCount do
  133.         local p = setmetatable({}, PARTICLE)
  134.         p:SetPos(self:GetPos():Copy())
  135.         p.life_mult = 1
  136.        
  137.         p:SetLifeTime(1)
  138.        
  139.         self:OnEmit(p)
  140.  
  141.         table.insert(self.particles, p)
  142.     end
  143. end
  144.  
  145. event.AddListener("OnDraw2D", "particles", function(dt)
  146.     surface.SetWhiteTexture()
  147.     surface.Color(0,0,0,1)
  148.     surface.DrawRect(0,0,surface.GetScreenSize())
  149.    
  150.     for _, emitter in pairs(emitters) do
  151.         emitter:Draw()
  152.     end
  153. end)
  154.  
  155. event.AddListener("OnUpdate", "particles", function(dt)
  156.     surface.SetWhiteTexture()
  157.     surface.Color(0,0,0,1)
  158.     surface.DrawRect(0,0,surface.GetScreenSize())
  159.    
  160.     for _, emitter in pairs(emitters) do
  161.         emitter:Think(dt)
  162.         if wait(1) then
  163.             print(table.count(emitter:GetParticles()))
  164.         end
  165.     end
  166. end)
  167.  
  168. window.Open(1024, 1024)
  169.  
  170. -- test
  171. local emitter = Emitter()
  172. emitter:SetPos(Vec3(50,50))
  173. emitter:SetRate(0)
  174. emitter:SetEmitCount(100)
  175.  
  176. local sphere = Texture(64, 64):Fill(function(x, y)
  177.     x = x / 64
  178.     y = y / 64
  179.    
  180.     x = x - 1
  181.     y = y - 1.5
  182.    
  183.     x = x * math.pi
  184.     y = y * math.pi
  185.        
  186.     local a = math.sin(x) * math.cos(y)
  187.    
  188.     a = a ^ 32
  189.        
  190.     return 255, 255, 255, a * 128
  191. end)
  192.    
  193. function emitter:OnEmit(p)
  194.     p:SetTexture(sphere)
  195.     p:SetPos(Vec3((window.GetSize()/2):Unpack()) + Vec3() + Vec3():GetRandom(-10,10))
  196.     p:SetVelocity(Vec3():GetRandom():GetAng3():GetForward() * 200)
  197.    
  198.     p:SetLifeTime(math.randomf(1,5))
  199.    
  200.     p:SetStartSize(math.randomf(10,20)*2)
  201.     p:SetStartAlpha(0)
  202.     p:SetEndAlpha(1)
  203.     p:SetColor(HSVToColor(glfw.GetTime()/5, 0.5))  
  204. end
Advertisement
Add Comment
Please, Sign In to add comment