Advertisement
xerpi

ParticleLib alpha2.2 [xerpi]

Aug 1st, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.99 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,{alpha=255,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,x,y
  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.                     x = (part.particles[i].x+cx)
  72.                     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.                     part.particles[i].alpha = 255
  78.                     if (hypotenuse/rad*100) > 40 then
  79.                         part.particles[i].alpha = 255-((hypotenuse/rad*80)*255)/100
  80.                     end
  81.                 end
  82.                 part.particles[i].img:blend(cx+part.particles[i].x,cy+part.particles[i].y,part.particles[i].alpha)
  83.             end
  84.         end
  85.     end
  86.  
  87. --Blend
  88.     function particles.blend(cx,cy,part,rad,alpha)
  89.         if part.status != "stop" then
  90.             for i =1, part.number_particles do
  91.                 if part.particles[i].status != "alive" then continue end
  92.                 if part.status == "run" then
  93.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  94.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  95.                     local x = (part.particles[i].x+cx)
  96.                     local y = (part.particles[i].y+cy)
  97.                     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
  98.                         part.particles[i].status = "dead"      
  99.                     end
  100.                 end
  101.                 part.particles[i].img:blend(cx+part.particles[i].x,cy+part.particles[i].y,alpha)
  102.             end
  103.         end
  104.     end
  105.  
  106. --FXtint
  107.     function particles.fxtint(cx,cy,part,rad,colour)
  108.         if part.status != "stop" then
  109.             for i =1, part.number_particles do
  110.                 if part.particles[i].status != "alive" then continue end
  111.                 if part.status == "run" then
  112.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  113.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  114.                     local x = (part.particles[i].x+cx)
  115.                     local y = (part.particles[i].y+cy)
  116.                     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
  117.                         part.particles[i].status = "dead"      
  118.                     end
  119.                 end
  120.                 part.particles[i].img:fxtint(cx+part.particles[i].x,cy+part.particles[i].y,colour or 0x0)
  121.             end
  122.         end
  123.     end
  124.  
  125. --FXadd
  126.     function particles.fxadd(cx,cy,part,rad,alpha)
  127.         if part.status != "stop" then
  128.             for i =1, part.number_particles do
  129.                 if part.particles[i].status != "alive" then continue end
  130.                 if part.status == "run" then
  131.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  132.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  133.                     local x = (part.particles[i].x+cx)
  134.                     local y = (part.particles[i].y+cy)
  135.                     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
  136.                         part.particles[i].status = "dead"      
  137.                     end
  138.                 end
  139.                 part.particles[i].img:fxadd(cx+part.particles[i].x,cy+part.particles[i].y,alpha)
  140.             end
  141.         end
  142.     end
  143.    
  144. --FXsub
  145.     function particles.fxsub(cx,cy,part,rad,alpha)
  146.         if part.status != "stop" then
  147.             for i =1, part.number_particles do
  148.                 if part.particles[i].status != "alive" then continue end
  149.                 if part.status == "run" then
  150.                     part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  151.                     part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  152.                     local x = (part.particles[i].x+cx)
  153.                     local y = (part.particles[i].y+cy)
  154.                     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
  155.                         part.particles[i].status = "dead"      
  156.                     end
  157.                 end
  158.                 part.particles[i].img:fxsub(cx+part.particles[i].x,cy+part.particles[i].y,alpha)
  159.             end
  160.         end
  161.     end
  162.    
  163. function particles.init(part)
  164.     part.status = "run"
  165. end
  166. function particles.free(part)
  167.     part = nil
  168. end
  169. function particles.pause(part)
  170.     part.status = "pause"
  171. end
  172. function particles.stop(part)
  173.     part.status = "stop"
  174. end
  175. function particles.reset(part)
  176.     for i =1, part.number_particles do
  177.         part.particles[i].status = "alive"
  178.         part.particles[i].x = 0
  179.         part.particles[i].y = 0
  180.     end
  181. end
  182.  
  183. my_part = particles.create(50,part_img)
  184. --particles.resize(my_part,100,10)
  185. lol=  {x=240,y=136,rad=100}
  186. show=true
  187. while true do
  188. screen.bilinear(1)
  189. controls.read()
  190. if math.abs(controls.analogx())>=50 then lol.x=lol.x+controls.analogx()/50 end
  191. if math.abs(controls.analogy())>=50 then lol.y=lol.y+controls.analogy()/50 end
  192. if lol.x>=480 then lol.x = 479 end
  193. if lol.y>=272 then lol.y = 271 end
  194. if lol.x<=0 then lol.x = 0 end
  195. if lol.y<=0 then lol.y = 0 end
  196.  
  197. if show then
  198.     draw.circle(lol.x,lol.y,lol.rad,color.new(0,255,0))
  199.     draw.line(lol.x-5,lol.y,lol.x+5,lol.y,color.new(0,255,0))
  200.     draw.line(lol.x,lol.y-5,lol.x,lol.y+5,color.new(0,255,0))
  201. end
  202. if controls.press("triangle") then show = not show end
  203.  
  204. if controls.press("cross") then
  205.     particles.reset(my_part)
  206.     particles.init(my_part)
  207. end
  208. if controls.press("square") then
  209.     if my_part.status == "pause" then
  210.         particles.init(my_part)
  211.     else
  212.         particles.pause(my_part)
  213.     end
  214. end
  215. if controls.press("start") then
  216.     init_ram = os.getfreememory()
  217.     particles.free(my_part)
  218.     final_ram=os.getfreememory()
  219.     os.message(init_ram.."\n"..final_ram)
  220. end
  221. if controls.r() then lol.rad=lol.rad+1 end
  222. if controls.l() then lol.rad=lol.rad-1 end
  223.  
  224.  
  225. particles.blit(lol.x,lol.y,my_part,lol.rad)
  226.  
  227. screen.print(430,3,"@"..screen.fps())
  228. if controls.select() then a() end
  229. screen.flip()
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement