Advertisement
xerpi

RBM v1

Jul 17th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.33 KB | None | 0 0
  1. --- xerpi (c) 2011---
  2.  
  3. math.randomseed(os.time()) 
  4. os.cpu(222) --A little powah for loading all
  5. if os.language() == "spanish" then
  6.     _moves = "Movidas: %i"
  7.     _level_completed = "Nivel completado."
  8. else
  9.     _moves = "Moves: %i"
  10.     _level_completed = "Level completed."
  11. end
  12.  
  13.  
  14. lcb = 0
  15. function loadcallback()
  16. screen.print(370,255,"Cargandeh"..string.sub("...",1,math.floor(lcb)))
  17. screen.flip()
  18. lcb = lcb + 0.05 if lcb > 4 then lcb = 0 end
  19. end
  20.  
  21.  
  22. prism = {obj=model.load("models/prism.obj")}
  23. exit = {obj=model.load("models/exit.obj")}
  24. cube = {obj=model.load("models/cube.obj")}
  25. floor = {obj=model.load("models/floor.obj")}
  26.  
  27. cnv = 1 --Currentlevel
  28.  
  29. map = {
  30. {
  31. --Level 1
  32. {"","","","",""},
  33. {"p","c","","",""},
  34. {"","","","",""},
  35. {"","","c","",""},
  36. {"","","e","",""},
  37. },
  38. {
  39. --Level 1
  40. {"","","","","","",""},
  41. {"","","","","","",""},
  42. {"","","","","","",""},
  43. {"","","","","","",""},
  44. {"","","","","","",""},
  45. {"","","","","","",""},
  46. {"","","","","","",""},
  47. }
  48. }
  49.  
  50. --Y->z
  51. --X->x
  52. --Z->y 
  53. exit_coord = {}
  54. map.height = #map[cnv]
  55. map.width = #map[cnv][1]
  56. map.w=10
  57. map.h=5
  58. map.d=10
  59.  
  60. pos={x=-map.width*5+5,y=0,z=-75}
  61. rot={x=1,y=0,z=0}
  62.  
  63. prism.x = 1
  64. prism.y = 2
  65. prism.x2 = 1
  66. prism.y2 = 1
  67. prism.status = "vertical"
  68. list={"vertical","up","down","right","left"}
  69. c=1
  70.  
  71.  
  72.  
  73. function change_level(n)
  74. exit_coord = nil
  75. exit_coord = {}
  76. for y = 1, map.height do
  77.     for x = 1, map.width do
  78.         if map[n][y][x] == "e" then
  79.             table.insert(exit_coord,{x=x,y=y})
  80.         elseif map[n][y][x] == "p" then
  81.             prism.x = x
  82.             prism.y= y
  83.         end
  84.     end
  85. end
  86. prism.status="vertical"
  87. prism.moves=0
  88. end
  89.  
  90.  
  91. function check_exit()
  92. if prism.status == "right" then prism.x2 = prism.x+1; prism.y2 = prism.y  end
  93. if prism.status == "left" then prism.x2 = prism.x-1; prism.y2 = prism.y  end
  94. if prism.status == "up" then prism.x2 = prism.x; prism.y2 = prism.y-1  end
  95. if prism.status == "down" then prism.x2 = prism.x; prism.y2 = prism.y+1  end
  96.     if #exit_coord == 1 then       
  97.         if prism.status == "vertical" and exit_coord[1].x == prism.x and exit_coord[1].y == prism.y then
  98.             blit_map()
  99.             blit_info()
  100.             screen.flip()
  101.             os.message(_level_completed)
  102.             cnv = cnv+1
  103.             change_level(cnv)
  104.  
  105.         end
  106.     elseif #exit_coord == 2 then
  107.         if exit_coord[1].x == prism.x and exit_coord[1].y == prism.y and exit_coord[2].x == prism.x2 and exit_coord[2].y == prism.y2 or
  108.         exit_coord[2].x == prism.x and exit_coord[2].y == prism.y and exit_coord[1].x == prism.x2 and exit_coord[1].y == prism.y2 then
  109.             blit_map()
  110.             blit_info()
  111.             screen.flip()
  112.             os.message(_level_completed)
  113.             cnv = cnv+1
  114.             change_level(cnv)
  115.         end
  116.     end
  117.  
  118. end
  119.  
  120. function blit_map()
  121.     local depth = 0
  122.     local p={x=0,y=0,z=0}
  123.     local r={x=0,y=0,z=0}
  124.     local sp = {x=0,y=0,z=0}
  125.     local sr = {x=0,y=0,z=0}
  126.     local ny = map.height/2
  127.         for y = 1, map.height do
  128.             for x = 1, map.width do
  129.                 --Calculate the position of empty models
  130.                     p.x = pos.x +((x-1)*map.w)
  131.                     p.y = pos.y + math.sin(-rot.x)*(y-ny)*map.w
  132.                     p.z = pos.z + math.cos(-rot.x)*(y-ny)*map.w
  133.                 --Blit the empty models
  134.                     floor.obj:rotation(rot.x ,rot.y, rot.z)
  135.                     floor.obj:position(p.x, p.y, p.z)                              
  136.                     floor.obj:blit()   
  137.                     if x== prism.x and y == prism.y then                   
  138.                         if prism.status == "up" then
  139.                             sp.x = p.x
  140.                             sp.y = p.y+math.sin(1.57-rot.x)*15
  141.                             sp.z = p.z+math.cos(1.57-rot.x)*15
  142.                             sr.x = rot.x-1.57
  143.                             sr.y = rot.y
  144.                             sr.z = rot.z
  145.                         elseif prism.status == "down" then
  146.                             sp.x = p.x
  147.                             sp.y = pos.y + math.sin(-rot.x)*((y-1)-ny)*map.w+math.sin(1.57-rot.x)*5
  148.                             sp.z = pos.z + math.cos(-rot.x)*((y-1)-ny)*map.w+math.cos(1.57-rot.x)*5
  149.                             sr.x = rot.x+1.57
  150.                             sr.y = rot.y
  151.                             sr.z = rot.z
  152.                         elseif prism.status == "right" then
  153.                             sp.x = p.x
  154.                             sp.y = pos.y+ math.sin(-rot.x)*((y-1)-ny)*map.w+math.sin(1.57-rot.x)*15
  155.                             sp.z = pos.z + math.cos(-rot.x)*((y-1)-ny)*map.w+math.cos(1.57-rot.x)*15
  156.                             sr.x = rot.x-1.57
  157.                             sr.y = rot.y
  158.                             sr.z = rot.z-1.57
  159.                         elseif prism.status == "left" then
  160.                             sp.x = pos.x +(x*map.w)
  161.                             sp.y = p.y+math.sin(1.57-rot.x)*15
  162.                             sp.z = p.z+math.cos(1.57-rot.x)*15
  163.                             sr.x = rot.x-1.57
  164.                             sr.y = rot.y
  165.                             sr.z = rot.z+1.57                  
  166.                         elseif prism.status == "vertical" then
  167.                             sp.x = p.x
  168.                             sp.y = p.y+math.sin(1.57-rot.x)*5
  169.                             sp.z = p.z+math.cos(1.57-rot.x)*5
  170.                             sr.x = rot.x
  171.                             sr.y = rot.y
  172.                             sr.z = rot.z
  173.                         end
  174.                         prism.obj:rotation(sr.x,sr.y,sr.z)
  175.                         prism.obj:position(sp.x,sp.y,sp.z)
  176.                         prism.obj:blit()
  177.                        
  178.                     end
  179.                     if map[cnv][y][x] != "" then
  180.                         if map[cnv][y][x] == "c" then
  181.                             cube.obj:position(p.x,p.y+math.sin(1.57-rot.x)*5,p.z+math.cos(1.57-rot.x)*5)
  182.                             cube.obj:rotation(rot.x,rot.y,rot.z)
  183.                             cube.obj:blit()
  184.                         elseif map[cnv][y][x] == "e" then
  185.                             exit.obj:position(p.x,p.y+math.sin(1.57-rot.x)*5,p.z+math.cos(1.57-rot.x)*5)
  186.                             exit.obj:rotation(rot.x,rot.y,rot.z)
  187.                             exit.obj:blit()    
  188.                         end
  189.                     end
  190.             end
  191.     end
  192.  
  193. end
  194.  
  195. function xy(y,x)
  196.     if x >0 and y > 0 and x <= map.width and y<=map.height then
  197.         return map[cnv][y][x]
  198.     else
  199.         return "c"
  200.     end
  201. end
  202.  
  203. function prism.move()
  204. if controls.press("down") then
  205.     if prism.status == "vertical" then
  206.         if xy(prism.y +1,prism.x) != "c" and xy(prism.y +2,prism.x) != "c" then
  207.             prism.y = prism.y+1
  208.             prism.status = "down"
  209.             prism.moves = prism.moves+1
  210.         end
  211.     elseif prism.status == "up" then
  212.         if xy(prism.y +1,prism.x) != "c" then
  213.             prism.y = prism.y+1
  214.             prism.status = "vertical"  
  215.             prism.moves = prism.moves+1
  216.         end
  217.     elseif prism.status == "down" then
  218.         if xy(prism.y +1,prism.x) != "c" and xy(prism.y +2,prism.x) != "c" then
  219.             prism.y = prism.y+2
  220.             prism.status = "vertical"  
  221.             prism.moves = prism.moves+1
  222.         end
  223.     elseif prism.status == "right" then
  224.         if xy(prism.y +1,prism.x) != "c" and xy(prism.y +1,prism.x+1) != "c" then
  225.             prism.y = prism.y+1
  226.             prism.moves = prism.moves+1
  227.         end
  228.     elseif prism.status == "left" then
  229.         if xy(prism.y +1,prism.x) != "c" and xy(prism.y +1,prism.x-1) != "c" then
  230.             prism.y = prism.y+1
  231.             prism.moves = prism.moves+1
  232.         end
  233.     end
  234.     check_exit()
  235. end
  236.  
  237. if controls.press("up") then
  238.     if prism.status == "vertical" then
  239.         if xy(prism.y -1,prism.x) != "c" and xy(prism.y -2,prism.x) != "c" then
  240.             prism.y = prism.y-1
  241.             prism.status = "up"
  242.             prism.moves = prism.moves+1        
  243.         end
  244.     elseif prism.status == "up" then
  245.         if xy(prism.y -1,prism.x) != "c" and xy(prism.y -2,prism.x) != "c" then
  246.             prism.y = prism.y-2
  247.             prism.status = "vertical"  
  248.             prism.moves = prism.moves+1
  249.         end
  250.     elseif prism.status == "down" then
  251.         if xy(prism.y -1,prism.x) != "c" then
  252.             prism.y = prism.y-1
  253.             prism.status = "vertical"
  254.             prism.moves = prism.moves+1        
  255.         end
  256.     elseif prism.status == "right" then
  257.         if xy(prism.y -1,prism.x) != "c" and xy(prism.y -1,prism.x+1) != "c" then
  258.             prism.y = prism.y-1
  259.             prism.moves = prism.moves+1
  260.         end
  261.     elseif prism.status == "left" then
  262.         if xy(prism.y -1,prism.x) != "c" and xy(prism.y -1,prism.x-1) != "c" then
  263.             prism.y = prism.y-1
  264.             prism.moves = prism.moves+1
  265.         end
  266.     end
  267.     check_exit()
  268. end
  269.  
  270. if controls.press("right") then
  271.     if prism.status == "vertical" then
  272.         if xy(prism.y ,prism.x+1) != "c" and xy(prism.y ,prism.x+2) != "c" then
  273.             prism.x = prism.x+1
  274.             prism.status = "right"
  275.             prism.moves = prism.moves+1        
  276.         end
  277.     elseif prism.status == "up" then
  278.         if xy(prism.y ,prism.x+1) != "c" and xy(prism.y-1 ,prism.x+1) != "c" then
  279.             prism.x = prism.x+1
  280.             prism.moves = prism.moves+1
  281.         end
  282.     elseif prism.status == "down" then
  283.         if xy(prism.y ,prism.x+1) != "c" and xy(prism.y+1 ,prism.x+1) != "c" then
  284.             prism.x = prism.x+1
  285.             prism.moves = prism.moves+1
  286.         end
  287.     elseif prism.status == "right" then
  288.         if xy(prism.y ,prism.x+1) != "c" and xy(prism.y ,prism.x+2) != "c" then
  289.             prism.x = prism.x+2
  290.             prism.status = "vertical"
  291.             prism.moves = prism.moves+1
  292.         end
  293.     elseif prism.status == "left" then
  294.         if xy(prism.y ,prism.x+1) != "c" then
  295.             prism.x = prism.x+1
  296.             prism.status = "vertical"
  297.             prism.moves = prism.moves+1
  298.         end
  299.     end
  300.     check_exit()
  301. end
  302.  
  303. if controls.press("left") then
  304.     if prism.status == "vertical" then
  305.         if xy(prism.y ,prism.x-1) != "c" and xy(prism.y ,prism.x-2) != "c" then
  306.             prism.x = prism.x-1
  307.             prism.status = "left"
  308.             prism.moves = prism.moves+1        
  309.         end
  310.     elseif prism.status == "up" then
  311.         if xy(prism.y ,prism.x-1) != "c" and xy(prism.y-1 ,prism.x-1) != "c" then
  312.             prism.x = prism.x-1
  313.             prism.moves = prism.moves+1
  314.         end
  315.     elseif prism.status == "down" then
  316.         if xy(prism.y ,prism.x-1) != "c" and xy(prism.y+1 ,prism.x-1) != "c" then
  317.             prism.x = prism.x-1
  318.             prism.moves = prism.moves+1
  319.         end
  320.     elseif prism.status == "right" then
  321.         if xy(prism.y ,prism.x-1) != "c" then
  322.             prism.x = prism.x-1
  323.             prism.status = "vertical"
  324.             prism.moves = prism.moves+1
  325.         end
  326.     elseif prism.status == "left" then
  327.         if xy(prism.y ,prism.x-1) != "c" then
  328.             prism.x = prism.x-2
  329.             prism.status = "vertical"
  330.             prism.moves = prism.moves+1
  331.         end
  332.     end
  333.     check_exit()
  334. end
  335. end
  336.  
  337. function blit_info()
  338. screen.print(5,5,string.format(_moves,prism.moves))
  339. screen.print(425,5,"@"..screen.fps())
  340. end
  341.  
  342. change_level(cnv)
  343. while true do
  344. --prism.status=list[c]
  345. controls.read()
  346.  
  347. if controls.start() then
  348.     --POS
  349.     if controls.up() then pos.y = pos.y+0.5 end
  350.     if controls.down() then pos.y = pos.y-0.5 end
  351.     if controls.left() then pos.x = pos.x-0.5 end
  352.     if controls.right() then pos.x = pos.x+0.5 end
  353.     if controls.l() then pos.z = pos.z-0.5 end
  354.     if controls.r() then pos.z = pos.z+0.5 end
  355.     --ROT
  356.     if controls.triangle() then rot.y = rot.y+0.05 end
  357.     if controls.cross() then rot.y = rot.y-0.05 end
  358.     if controls.square() then rot.x = rot.x-0.05 end
  359.     if controls.circle() then rot.x = rot.x+0.05 end
  360.     if math.abs(controls.analogy())> 50 then rot.x = rot.x+controls.analogy()/2000 end
  361. else
  362. if controls.press("l") then c= c-1 if c<1 then c = #list end end
  363. if controls.press("r") then c= c+1 if c>#list then c = 1 end end
  364. end
  365.  
  366. if controls.press("r") then --Reset level
  367.     change_level(cnv)
  368. end
  369.  
  370.  
  371. prism.move()
  372. blit_map()
  373.  
  374.  
  375. --screen.print(5,5,"ROT: ".." x:"..rot.x.." y:"..rot.y.." z:"..rot.z)
  376. --screen.print(5,25,"POS: ".." x:"..pos.x.." y:"..pos.y.." z:"..pos.z)
  377. --screen.print(5,45,"Prism: ".." x:"..prism.x.." y:"..prism.y.." status:"..prism.status)
  378. blit_info()
  379.  
  380. if controls.select() then a() end
  381. screen.flip()
  382. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement