Advertisement
xerpi

ParticleLib alpha2.1 [xerpi]

Aug 1st, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.85 KB | None | 0 0
  1. --------------xerpi - (c)   2011   -----------------
  2. math.randomseed(os.time()/math.pi*os.clock()/os.time())
  3.  
  4. part_img = image.load("exploluz.png")
  5.  
  6. function draw.circle(x,y,r,color)
  7.   local x0, y0 = r, 0
  8.   for i=0,90,9 do
  9.      local x1,y1 = r*math.cos( math.rad( i )), r*math.sin(math.rad( i ));
  10.      draw.line(x+x1,y+y1,x+x0,y+y0,color);
  11.      draw.line(x+x0,y-y0,x+x1,y-y1,color);
  12.      draw.line(x-x0,y+y0,x-x1,y+y1,color);
  13.      draw.line(x-x0,y-y0,x-x1,y-y1,color);
  14.      x0, y0 = x1, y1;
  15.   end
  16. end
  17.  
  18. particles = {}
  19. os.cpu(333)
  20.  
  21. function random_color()
  22.     return color.new(math.random(100,255),math.random(100,255),math.random(100,255))
  23. end
  24.  
  25.  
  26. function particles.create(n,img,min_vel,max_vel)
  27.     local tab ={number_particles=n,initx=ix,inity=iy,status="stop",particles={}}
  28.     for i = 1, n do
  29.         table.insert(tab.particles,{status="alive",x=0,y=0,vel = math.random((min_vel or 2)*10000, (max_vel or 5)*10000)/10000,ang = math.rad(math.random(0,360))})
  30.         if type(img) == "userdata" then
  31.             tab.particles[i].img = image.create(img:width(),img:height())
  32.             tab.particles[i].img:blit(0,0,part_img)
  33.         elseif type(img) == "table" then
  34.             local pic = img[math.random(1,#img)]
  35.             tab.particles[i].img = image.create(pic:width(),pic:height())
  36.             tab.particles[i].img:blit(0,0,pic)
  37.         else
  38.             tab.particles[i].img = image.create(math.random(500,1000)/100,math.random(200,500)/100,random_color())
  39.         end
  40.         --tab.particles[i].img:clear(color.new(255,0,0))
  41.         tab.particles[i].img:center()
  42.         tab.particles[i].img:rotate(math.deg(tab.particles[i].ang))
  43.     end
  44.     return tab
  45. end
  46.  
  47.  
  48. --Factorscale
  49.     function particles.factorscale(part,n)
  50.         for i =1, part.number_particles do 
  51.             part.particles[i].img:factorscale((n or 1))
  52.         end
  53.     end
  54. --Resize
  55.     function particles.resize(part,w,h)
  56.         for i =1, part.number_particles do
  57.             part.particles[i].img:resize((w or part.particles[i].img:width()),(h or part.particles[i].img:height()))   
  58.         end
  59.     end
  60.  
  61.  
  62. --Blit (normal)
  63.     function particles.blit(cx,cy,part,rad)
  64.         local hypotenuse
  65.         if part.status != "stop" then
  66.             for i =1, part.number_particles do
  67.                 if part.particles[i].status != "alive" then continue end
  68.                 if part.status == "run" then
  69.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  70.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  71.                     local x = (part.particles[i].x+cx)
  72.                     local y = (part.particles[i].y+cy)
  73.                     hypotenuse = math.sqrt(part.particles[i].x*part.particles[i].x+part.particles[i].y*part.particles[i].y)
  74.                     if x >= 480 or x <= 0 or y >= 272 or y <= 0 or hypotenuse >= rad then
  75.                         part.particles[i].status = "dead"      
  76.                     end
  77.                 end
  78.                 part.particles[i].img:blend(cx+part.particles[i].x,cy+part.particles[i].y,255-((hypotenuse/rad*100)*255)/100)
  79.             end
  80.         end
  81.     end
  82.  
  83. --Blend
  84.     function particles.blend(cx,cy,part,rad,alpha)
  85.         if part.status != "stop" then
  86.             for i =1, part.number_particles do
  87.                 if part.particles[i].status != "alive" then continue end
  88.                 if part.status == "run" then
  89.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  90.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  91.                     local x = (part.particles[i].x+cx)
  92.                     local y = (part.particles[i].y+cy)
  93.                     if x >= 480 or x <= 0 or y >= 272 or y <= 0 or math.sqrt(part.particles[i].x*part.particles[i].x+part.particles[i].y*part.particles[i].y) >= rad then
  94.                         part.particles[i].status = "dead"      
  95.                     end
  96.                 end
  97.                 part.particles[i].img:blend(cx+part.particles[i].x,cy+part.particles[i].y,alpha)
  98.             end
  99.         end
  100.     end
  101.  
  102. --FXtint
  103.     function particles.fxtint(cx,cy,part,rad,colour)
  104.         if part.status != "stop" then
  105.             for i =1, part.number_particles do
  106.                 if part.particles[i].status != "alive" then continue end
  107.                 if part.status == "run" then
  108.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  109.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  110.                     local x = (part.particles[i].x+cx)
  111.                     local y = (part.particles[i].y+cy)
  112.                     if x >= 480 or x <= 0 or y >= 272 or y <= 0 or math.sqrt(part.particles[i].x*part.particles[i].x+part.particles[i].y*part.particles[i].y) >= rad then
  113.                         part.particles[i].status = "dead"      
  114.                     end
  115.                 end
  116.                 part.particles[i].img:fxtint(cx+part.particles[i].x,cy+part.particles[i].y,colour or 0x0)
  117.             end
  118.         end
  119.     end
  120.  
  121. --FXadd
  122.     function particles.fxadd(cx,cy,part,rad,alpha)
  123.         if part.status != "stop" then
  124.             for i =1, part.number_particles do
  125.                 if part.particles[i].status != "alive" then continue end
  126.                 if part.status == "run" then
  127.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  128.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  129.                     local x = (part.particles[i].x+cx)
  130.                     local y = (part.particles[i].y+cy)
  131.                     if x >= 480 or x <= 0 or y >= 272 or y <= 0 or math.sqrt(part.particles[i].x*part.particles[i].x+part.particles[i].y*part.particles[i].y) >= rad then
  132.                         part.particles[i].status = "dead"      
  133.                     end
  134.                 end
  135.                 part.particles[i].img:fxadd(cx+part.particles[i].x,cy+part.particles[i].y,alpha)
  136.             end
  137.         end
  138.     end
  139.    
  140. --FXsub
  141.     function particles.fxsub(cx,cy,part,rad,alpha)
  142.         if part.status != "stop" then
  143.             for i =1, part.number_particles do
  144.                 if part.particles[i].status != "alive" then continue end
  145.                 if part.status == "run" then
  146.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  147.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  148.                     local x = (part.particles[i].x+cx)
  149.                     local y = (part.particles[i].y+cy)
  150.                     if x >= 480 or x <= 0 or y >= 272 or y <= 0 or math.sqrt(part.particles[i].x*part.particles[i].x+part.particles[i].y*part.particles[i].y) >= rad then
  151.                         part.particles[i].status = "dead"      
  152.                     end
  153.                 end
  154.                 part.particles[i].img:fxsub(cx+part.particles[i].x,cy+part.particles[i].y,alpha)
  155.             end
  156.         end
  157.     end
  158.    
  159. function particles.init(part)
  160.     part.status = "run"
  161. end
  162. function particles.free(part)
  163.     part = nil
  164. end
  165. function particles.pause(part)
  166.     part.status = "pause"
  167. end
  168. function particles.stop(part)
  169.     part.status = "stop"
  170. end
  171. function particles.reset(part)
  172.     for i =1, part.number_particles do
  173.         part.particles[i].status = "alive"
  174.         part.particles[i].x = 0
  175.         part.particles[i].y = 0
  176.     end
  177. end
  178.  
  179. my_part = particles.create(50,part_img)
  180. --particles.resize(my_part,100,10)
  181. lol=  {x=240,y=136,rad=100}
  182. show=true
  183. while true do
  184. screen.bilinear(1)
  185. controls.read()
  186. if math.abs(controls.analogx())>=50 then lol.x=lol.x+controls.analogx()/50 end
  187. if math.abs(controls.analogy())>=50 then lol.y=lol.y+controls.analogy()/50 end
  188. if lol.x>=480 then lol.x = 479 end
  189. if lol.y>=272 then lol.y = 271 end
  190. if lol.x<=0 then lol.x = 0 end
  191. if lol.y<=0 then lol.y = 0 end
  192.  
  193. if show then
  194.     draw.circle(lol.x,lol.y,lol.rad,color.new(0,255,0))
  195.     draw.line(lol.x-5,lol.y,lol.x+5,lol.y,color.new(0,255,0))
  196.     draw.line(lol.x,lol.y-5,lol.x,lol.y+5,color.new(0,255,0))
  197. end
  198. if controls.press("triangle") then show = not show end
  199.  
  200. if controls.press("cross") then
  201.     particles.reset(my_part)
  202.     particles.init(my_part)
  203. end
  204. if controls.press("square") then
  205.     if my_part.status == "pause" then
  206.         particles.init(my_part)
  207.     else
  208.         particles.pause(my_part)
  209.     end
  210. end
  211. if controls.press("start") then
  212.     init_ram = os.getfreememory()
  213.     particles.free(my_part)
  214.     final_ram=os.getfreememory()
  215.     os.message(init_ram.."\n"..final_ram)
  216. end
  217. if controls.r() then lol.rad=lol.rad+1 end
  218. if controls.l() then lol.rad=lol.rad-1 end
  219.  
  220.  
  221. particles.blit(lol.x,lol.y,my_part,lol.rad)
  222.  
  223. screen.print(430,3,"@"..screen.fps())
  224. if controls.select() then a() end
  225. screen.flip()
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement