Advertisement
xerpi

ParticleLib alpha2 [xerpi]

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