Advertisement
xerpi

Untitled

Apr 20th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. -- Chipmunks Engine Tester
  2.  
  3. mousepos = { x= 240, y =136 }
  4. --os.luadevsplash();
  5. os.autofps(120);
  6. chipmunk.init();
  7.  
  8.  
  9. sp = chipmunk.space.new();
  10. chipmunk.space.resizestatichash(sp, 22.0, 500.0);
  11. chipmunk.space.resizeactivehash(sp, 22.0, 500.0);
  12. chipmunk.space.gravity(sp,0.0, -100.0);
  13.  
  14. mouse = chipmunk.cursor.new(sp);   
  15.  
  16. -- crear margenes:
  17. body = chipmunk.space.staticbody(sp);
  18. forma = chipmunk.shape.newsegment( body , 0.0 ,0.0, 479.0, 0.0, 0.0);
  19. chipmunk.shape.elasticity(forma,1.0);
  20. chipmunk.shape.friction(forma,1.0);
  21. chipmunk.space.addstaticshape(sp,forma);
  22.  
  23. forma = chipmunk.shape.newsegment( body , 0.0 ,0.0, 0.0, 272.0, 0.0);
  24. chipmunk.shape.elasticity(forma,1.0);
  25. chipmunk.shape.friction(forma,1.0);
  26. chipmunk.space.addstaticshape(sp,forma);
  27.  
  28. forma = chipmunk.shape.newsegment( body , 479.0 ,0.0, 479.0, 272.0, 0.0);
  29. chipmunk.shape.elasticity(forma,1.0);
  30. chipmunk.shape.friction(forma,1.0);
  31. chipmunk.space.addstaticshape(sp,forma);
  32.  
  33. forma = chipmunk.shape.newsegment( body , 0.0 ,272.0, 479.0, 272.0, 0.0);
  34. chipmunk.shape.elasticity(forma,1.0);
  35. chipmunk.shape.friction(forma,1.0);
  36. chipmunk.space.addstaticshape(sp,forma);
  37.  
  38. forma = chipmunk.shape.newsegment( body , 100.0 ,100.0, 200.0, 200.0, 0.0);
  39. chipmunk.shape.elasticity(forma,2.0);
  40. chipmunk.shape.friction(forma,1.0);
  41. chipmunk.space.addstaticshape(sp,forma);
  42.  
  43. vertices = { { -10.0, -10.0 }, { -10.0 , 10.0 }, { 10.0, 10.0 }, { 10.0 , -10.0 } }
  44.  
  45. for i=0,10 do
  46.     for j = 0, i do
  47.         cuerpo = chipmunk.body.new( 1.0 , chipmunk.moment.polygon( 1.0 , vertices ) );
  48.         chipmunk.body.position(cuerpo,240.0 + j*20.0 - i*10.0, 300.0 - i*20.0);
  49.         chipmunk.space.addbody(sp,cuerpo);
  50.         forma = chipmunk.shape.newpoly(cuerpo, vertices);
  51.         chipmunk.shape.elasticity(forma,0.0);
  52.         chipmunk.shape.friction(forma,0.8);
  53.         chipmunk.space.addshape(sp,forma);
  54.     end
  55. end
  56.  
  57.  
  58. cuerpo = chipmunk.body.new( 10.0 , chipmunk.moment.circle( 10.0 , 0.0, 15.0 ) );
  59. chipmunk.body.position(cuerpo,240.0 , 15.0);
  60. chipmunk.space.addbody(sp,cuerpo);
  61. forma = chipmunk.shape.newcircle(cuerpo, 15.0);
  62. chipmunk.shape.elasticity(forma,0.0);
  63. chipmunk.shape.friction(forma,0.9);
  64. chipmunk.space.addshape(sp,forma);
  65.  
  66. --os.chipmunksplash();
  67.  
  68. while true do
  69.     chipmunk.space.step(sp, 2/60);
  70.     screen.print(10,10,"Chipmunk DEMO at "..screen.fps().."fps - "..os.cpu().." mhz");
  71.     --screen.print(10,25,"R para coger objetos. Cruz/Triangulo/Redonda/Cuadrado\ncambiar gravedad. Cursor: "..mousepos.x.." "..mousepos.y);
  72.     chipmunk.space.wiredraw(sp);
  73.     draw.line(mousepos.x,mousepos.y-3,mousepos.x,mousepos.y+4,color.new(255,250,250));
  74.     draw.line(mousepos.x-3,mousepos.y,mousepos.x+4,mousepos.y,color.new(255,250,250));
  75.     screen.flip();
  76.     controls.read();
  77.     --[[if controls.left() then mousepos.x = mousepos.x - 3; end
  78.     if controls.right() then mousepos.x = mousepos.x + 3; end
  79.     if controls.up() then mousepos.y = mousepos.y -3; end
  80.     if controls.down() then mousepos.y = mousepos.y + 3; end]]
  81.     if math.abs(controls.analogx())>20 then mousepos.x = mousepos.x + controls.analogx()/30 end
  82.     if math.abs(controls.analogy())>20 then mousepos.y = mousepos.y + controls.analogy()/20 end
  83.     chipmunk.cursor.move(mouse,mousepos.x,272-mousepos.y);
  84.     if controls.r() then
  85.         cuerpo = chipmunk.space.pointqueryfirst_body(sp, mousepos.x, 272 - mousepos.y);
  86.         if cuerpo then
  87.             local px, py = chipmunk.body.position(cuerpo);
  88.             mousepos.x = px; mousepos.y = 272.0-py;
  89.         end
  90.         chipmunk.cursor.grab(mouse,mousepos.x,272.0-mousepos.y);
  91.     end
  92.    
  93.     if controls.release("r") then chipmunk.cursor.release(mouse); end
  94.     if controls.press("triangle") then chipmunk.space.gravity(sp,0.0,100.0); end
  95.     if controls.press("l") then chipmunk.space.gravity(sp,0.0,0); end
  96.     if controls.press("square") then chipmunk.space.gravity(sp,-100.0,0.0); end
  97.     if controls.press("cross") then chipmunk.space.gravity(sp,0.0,-100.0); end
  98.     if controls.press("circle") then chipmunk.space.gravity(sp,100.0,0.0); end
  99.     if controls.press("select") then a() end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement