Advertisement
xerpi

Untitled

Apr 20th, 2011
242
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= 17, y =136 }
  4. mouse2pos = { x= 470, y =136 }
  5. --os.luadevsplash();
  6. os.autofps(120);
  7. chipmunk.init();
  8.  
  9.  
  10. sp = chipmunk.space.new();
  11. chipmunk.space.resizestatichash(sp, 22.0, 500.0);
  12. chipmunk.space.resizeactivehash(sp, 22.0, 500.0);
  13. chipmunk.space.gravity(sp,0.0, 0.0);  --gravedad 0
  14.  
  15. mouse = chipmunk.cursor.new(sp);   
  16.  
  17. -- crear margenes:
  18. body = chipmunk.space.staticbody(sp);
  19. forma = chipmunk.shape.newsegment( body , 0.0 ,1.0, 479.0, 1.0, 0.0);
  20. chipmunk.shape.elasticity(forma,1.0);
  21. chipmunk.shape.friction(forma,0.0);
  22. chipmunk.space.addstaticshape(sp,forma);
  23.  
  24. forma = chipmunk.shape.newsegment( body , 0.0 ,0.0, 0.0, 272.0, 0.0);
  25. chipmunk.shape.elasticity(forma,1.0);
  26. chipmunk.shape.friction(forma,0.0);
  27. chipmunk.space.addstaticshape(sp,forma);
  28.  
  29. forma = chipmunk.shape.newsegment( body , 479.0 ,0.0, 479.0, 272.0, 0.0);
  30. chipmunk.shape.elasticity(forma,1.0);
  31. chipmunk.shape.friction(forma,0.0);
  32. chipmunk.space.addstaticshape(sp,forma);
  33.  
  34. forma = chipmunk.shape.newsegment( body , 0.0 ,272.0, 479.0, 272.0, 0.0);
  35. chipmunk.shape.elasticity(forma,1.0);
  36. chipmunk.shape.friction(forma,0.0);
  37. chipmunk.space.addstaticshape(sp,forma);
  38.  
  39.  
  40. vertices = { { -10, -10 }, { -10 , 35 }, { 2, 35 }, { 2 , -10 } }
  41.  
  42.  
  43.  
  44. p1 = chipmunk.body.new( 1.0 ,chipmunk.moment.polygon( 1.0 , vertices ) );
  45. chipmunk.body.position(p1,mousepos.x,mousepos.y);
  46. chipmunk.space.addbody(sp,p1);
  47. forma1 = chipmunk.shape.newpoly(p1, vertices);
  48. chipmunk.shape.elasticity(forma1,1.0);
  49. chipmunk.shape.friction(forma1,0.5);
  50. chipmunk.space.addshape(sp,forma1);
  51.  
  52.  
  53. p2 = chipmunk.body.new( 1.0 , chipmunk.moment.polygon( 1.0 , vertices ) );
  54. chipmunk.body.position(p2,mouse2pos.x,mouse2pos.y);
  55. chipmunk.space.addbody(sp,p2);
  56. forma2 = chipmunk.shape.newpoly(p2, vertices);
  57. chipmunk.shape.elasticity(forma2,0.5);
  58. chipmunk.shape.friction(forma2,0.5);
  59. chipmunk.space.addshape(sp,forma2);
  60.  
  61.  
  62. ball = chipmunk.body.new( 10.0 , chipmunk.moment.circle( 10.0 , 0.0, 15.0 ) );
  63. chipmunk.body.position(ball,240 ,136);
  64. chipmunk.space.addbody(sp,ball);
  65. forma = chipmunk.shape.newcircle(ball, 6.0);
  66. chipmunk.shape.elasticity(forma,0.5);
  67. chipmunk.shape.friction(forma,0.5);
  68. chipmunk.space.addshape(sp,forma);
  69.  
  70. --os.chipmunksplash();
  71.  
  72. while true do
  73.     chipmunk.space.step(sp, 2/60);
  74.     screen.print(10,10,"Chipmunk PONG at "..screen.fps().."fps - "..os.cpu().." mhz");
  75.     --screen.print(10,25,"R para coger objetos. Cruz/Triangulo/Redonda/Cuadrado\ncambiar gravedad. Cursor: "..mousepos.x.." "..mousepos.y);
  76.     chipmunk.space.wiredraw(sp);
  77.     screen.flip();
  78.     controls.read();
  79.     if controls.triangle() then mouse2pos.y = mouse2pos.y -3.5; end
  80.     if controls.cross() then mouse2pos.y = mouse2pos.y + 3.5; end
  81.     if controls.up() then mousepos.y = mousepos.y -3.5; end
  82.     if controls.down() then mousepos.y = mousepos.y + 3.5; end
  83.    
  84.     if mousepos.y<0 then mousepos.y=0 end
  85.     if mouse2pos.y<0 then mouse2pos.y=0 end
  86.     if mousepos.y>272 then mousepos.y=272 end
  87.     if mouse2pos.y>272 then mouse2pos.y=272 end
  88.    
  89.     chipmunk.cursor.move(mouse,mousepos.x,272-mousepos.y);
  90.     if controls.r() then
  91.         cuerpo = chipmunk.space.pointqueryfirst_body(sp, mousepos.x, 272 - mousepos.y);
  92.         if cuerpo then
  93.             local px, py = chipmunk.body.position(cuerpo);
  94.             mousepos.x = px; mousepos.y = 272.0-py;
  95.         end
  96.         chipmunk.cursor.grab(mouse,mousepos.x,272.0-mousepos.y);
  97.     end
  98.     chipmunk.body.position(p1,mousepos.x ,272.0-mousepos.y);
  99.     chipmunk.body.position(p2,mouse2pos.x ,272.0-mouse2pos.y);
  100.     --[[if controls.release("r") then chipmunk.cursor.release(mouse); end
  101.     if controls.press("triangle") then chipmunk.space.gravity(sp,0.0,100.0); end
  102.     if controls.press("l") then chipmunk.space.gravity(sp,0.0,0); end
  103.     if controls.press("square") then chipmunk.space.gravity(sp,-100.0,0.0); end
  104.     if controls.press("cross") then chipmunk.space.gravity(sp,0.0,-100.0); end
  105.     if controls.press("circle") then chipmunk.space.gravity(sp,100.0,0.0); end]]
  106.     if controls.press("select") then a() end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement