Advertisement
xerpi

ParticleLib alpha1 [xerpi]

Aug 1st, 2011
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. --------------xerpi - (c)   2011   -----------------
  2. math.randomseed(os.time()/math.pi*os.clock()/os.time())
  3.  
  4. function draw.circle(x,y,r,color)
  5.   local x0, y0 = r, 0
  6.   for i=0,90,9 do
  7.      local x1,y1 = r*math.cos( math.rad( i )), r*math.sin(math.rad( i ));
  8.      draw.line(x+x1,y+y1,x+x0,y+y0,color);
  9.      draw.line(x+x0,y-y0,x+x1,y-y1,color);
  10.      draw.line(x-x0,y+y0,x-x1,y+y1,color);
  11.      draw.line(x-x0,y-y0,x-x1,y-y1,color);
  12.      x0, y0 = x1, y1;
  13.   end
  14. end
  15.  
  16.  
  17. particles = {}
  18. os.cpu(333)
  19.  
  20. function random_color()
  21.     return color.new(math.random(50,255),math.random(50,255),math.random(50,255))
  22. end
  23.  
  24.  
  25. function particles.create(n,max_rad)
  26.     local tab ={initx=ix,inity=iy,status="stop",particles={},rad=max_rad}
  27.     for i = 1, n do
  28.         table.insert(tab.particles,{status="alive",x=0,y=0,vel = math.random(15000,50000)/10000,ang = math.rad(math.random(0,360))})
  29.         tab.particles[i].img = image.create(math.random(500,1000)/100,math.random(200,500)/100,random_color())
  30.         --tab.particles[i].img:clear(color.new(255,0,0))
  31.         tab.particles[i].img:center()
  32.         tab.particles[i].img:rotate(math.deg(tab.particles[i].ang))
  33.     end
  34.     return tab
  35. end
  36.  
  37. function particles.blit(cx,cy,part,rad)
  38.     if part.status != "stop" then
  39.         for i =1, #part.particles do
  40.             if part.particles[i].status != "alive" then continue end
  41.             if part.status == "run" then
  42.                 part.particles[i].x = part.particles[i].x + math.cos(part.particles[i].ang)*part.particles[i].vel
  43.                 part.particles[i].y = part.particles[i].y + math.sin(part.particles[i].ang)*part.particles[i].vel
  44.                 local x = (part.particles[i].x+cx)
  45.                 local y = (part.particles[i].y+cy)
  46.                 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
  47.                     part.particles[i].status = "dead"      
  48.                 end
  49.             end
  50.             part.particles[i].img:blit(cx+part.particles[i].x,cy+part.particles[i].y)
  51.         end
  52.     end
  53. end
  54.  
  55.  
  56. function particles.init(part)
  57.     part.status = "run"
  58. end
  59. function particles.pause(part)
  60.     part.status = "pause"
  61. end
  62. function particles.stop(part)
  63.     part.status = "stop"
  64. end
  65. function particles.reset(part)
  66.     for i =1, #part.particles do
  67.         part.particles[i].status = "alive"
  68.         part.particles[i].x = 0
  69.         part.particles[i].y = 0
  70.     end
  71. end
  72.  
  73. my_part = particles.create(90,100)
  74.  
  75. lol=  {x=240,y=136,rad=50}
  76.  
  77.  
  78. while true do
  79. controls.read()
  80.  
  81. if math.abs(controls.analogx())>=50 then lol.x=lol.x+controls.analogx()/50 end
  82. if math.abs(controls.analogy())>=50 then lol.y=lol.y+controls.analogy()/50 end
  83. if lol.x>=480 then lol.x = 479 end
  84. if lol.y>=272 then lol.y = 271 end
  85. if lol.x<=0 then lol.x = 0 end
  86. if lol.y<=0 then lol.y = 0 end
  87.  
  88. draw.circle(lol.x,lol.y,lol.rad,color.new(0,255,0))
  89. draw.line(lol.x-5,lol.y,lol.x+5,lol.y,color.new(0,255,0))
  90. draw.line(lol.x,lol.y-5,lol.x,lol.y+5,color.new(0,255,0))
  91.  
  92. if controls.press("cross") then
  93.     particles.reset(my_part)
  94.     particles.init(my_part)
  95. end
  96. if controls.press("square") then
  97.     particles.pause(my_part)
  98. end
  99. if controls.r() then lol.rad=lol.rad+1 end
  100. if controls.l() then lol.rad=lol.rad-1 end
  101.  
  102. particles.blit(lol.x,lol.y,my_part,lol.rad)
  103.  
  104. screen.print(430,3,"@"..screen.fps())
  105. if controls.select() then a() end
  106. screen.flip()
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement