Advertisement
xerpi

RollingBlockMaze v1

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