Advertisement
xerpi

for cam-maker

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