Advertisement
xerpi

3DnRaya beta1

Jun 28th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.20 KB | None | 0 0
  1. functions={time=timer.new()}
  2.  
  3. object={}
  4. object.sel = {model = model.load("models/selec.obj"),pos={x=0,y=0,z=0},rot={x=0,y=0,z=0},turning = {x=0,y=0,z=0}}
  5. object.cross = {model = model.load("models/cross.obj"),pos={x=0,y=0,z=0},rot={x=0,y=0,z=0},turning = {x=0,y=0,z=0}}
  6. object.circle = {model = model.load("models/circle.obj"),pos={x=0,y=0,z=0},rot={x=0,y=0,z=0},turning = {x=5,y=0,z=0}}
  7. object.star_4 = {model = model.load("models/star4.obj"),pos={x=0,y=0,z=0},rot={x=0,y=0,z=0},turning = {x=0,y=5,z=0}}
  8. object.star_12 = {model = model.load("models/star12.obj"),pos={x=0,y=0,z=0},rot={x=0,y=0,z=0},turning = {x=0,y=0,z=0}}
  9. object.nazi = {model = model.load("models/nazi.obj"),pos={x=0,y=0,z=0},rot={x=0,y=0,z=0},turning = {x=0,y=0,z=0}}
  10. ----------
  11. mdls = {object.cross,object.circle,object.star_4,object.star_12,object.nazi}
  12.  
  13. n=2
  14. --------
  15.  
  16. scenery={}
  17. scenery[1] = {mode = "2D",n = 3,turn = 1,x = 100,y = 50, w = 20, h = 20, sel = {image = image.create(20, 20, color.new(255,255,255,125) ), x = 1, y = 1},J={}}
  18. scenery[1].table = {
  19. {"","",""},
  20. {"","",""},
  21. {"","",""}
  22. }
  23. scenery[1].J[1] = {string = "x", image = image.create(scenery[1].w, scenery[1].h, color.new(255,0,0) ) }
  24. scenery[1].J[2] = {string = "o", image = image.create(scenery[1].w, scenery[1].h, color.new(0,0,255) ) }
  25. ----------------------------------------------------------------------
  26. scenery[2] = {mode = "3D",difx=0,dify=0,n = 3,turn = 1,pos={x = 1,y = -6,z=-43},rot={x=33,y=0,z=0}, w = 10, h = 2, sel = { x = 1, y = 1},J={}}
  27.     scenery[2].table={
  28.         {"","x","o"},
  29.         {"x","","x"},
  30.         {"o","x",""}
  31.     }
  32.        
  33. scenery[2].pos.x = -scenery[2].w-scenery[2].difx
  34. scenery[2].J[1] = {string = "x", object = mdls[math.random(1,#mdls)], n = 1}
  35. scenery[2].J[2] = {string = "o", object = mdls[math.random(1,#mdls)], n = 2}
  36.  
  37.  
  38.  
  39. function functions.scenery_2D_new()
  40. local t={};
  41. t= {}
  42. table.insert(scenery,t)
  43. end
  44. function functions.scenery_3D_new()
  45. local t={};
  46. t= {}
  47. table.insert(scenery,t)
  48. end
  49.  
  50. function functions.rotate_3D_models(array)
  51.     if array.mode == "3D" then
  52.         for y = 1, #array.table do
  53.             for x = 1, #array.table[y] do
  54.            
  55.            
  56.             end
  57.         end
  58.     end
  59. end
  60.  
  61. function functions.draw_scenery(array)
  62.     local pos={}
  63.     local rot={}
  64.  
  65.     for y = 1, #array.table do
  66.         for x = 1, #array.table[y] do
  67.             for i = 1, #array.J do
  68.                 if array.mode == "2D" then
  69.                     if array.table[y][x] == array.J[i].string then
  70.                         array.J[i].image:blit(array.x + (x-1)*array.w, array.y + (y-1)*array.h)
  71.                     end
  72.                 elseif array.mode == "3D" then
  73.                
  74.                     if array.table[y][x] == array.J[i].string then
  75.                         ------
  76.                         pos.x = array.pos.x +((x-1)*array.w +(x-1)*array.difx)+array.J[i].object.pos.x
  77.                         pos.y = array.pos.y +math.sin(math.rad(-array.rot.x))*((y-1)*array.w + (y-1)*array.dify)+array.J[i].object.pos.y
  78.                         pos.z = array.pos.z + math.cos(math.rad(-array.rot.x))*((y-1)*array.w+ (y-1)*array.dify)+array.J[i].object.pos.z
  79.                         rot.x = math.rad(array.rot.x+array.J[i].object.rot.x+ (array.J[i].object.turning.x*functions.time:time()/10))
  80.                         rot.y = math.rad(array.rot.y+array.J[i].object.rot.y+ (array.J[i].object.turning.y*functions.time:time()/10))
  81.                         rot.z = math.rad(array.rot.z+array.J[i].object.rot.z+ (array.J[i].object.turning.z*functions.time:time()/10))
  82.                         -------                                        
  83.                         array.J[i].object.model:position(pos.x, pos.y,pos.z)
  84.                         array.J[i].object.model:rotation(rot.x,rot.y,rot.z)
  85.                         array.J[i].object.model:blit()
  86.                     end            
  87.                 end
  88.                
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. function functions.draw_scenery_contorn(array,colore)
  95.     if array.mode == "2D" then
  96.         draw.rect( array.x, array.y , #array.table * array.w, #array.table[1] * array.h, ( array.colore or color.new(255,255,255) ) )
  97.     elseif array.mode == "3D" then
  98. draw.rect(array.pos.x, array.pos.y, array.pos.x+((#array.table[1]-1)*array.w + (#array.table-1)*array.difx) , array.pos.x+((#array.table-1)*array.h + (#array.table-1)*array.dify), color.new(255,0,0) )       
  99.     end
  100. end
  101.  
  102. function functions.print_selector(array)
  103.     if array.mode == "2D" then
  104.         array.sel.image:blit( array.x + (array.sel.x - 1)*array.w, array.y + (array.sel.y - 1)*array.h)
  105.     elseif array.mode == "3D" then
  106.         object.sel.model:position(array.pos.x +((array.sel.x-1)*array.w +(array.sel.x-1)*array.difx), array.pos.y +math.sin(math.rad(-array.rot.x))*((array.sel.y-1)*array.w + (array.sel.y-1)*array.dify),array.pos.z + math.cos(math.rad(-array.rot.x))*((array.sel.y-1)*array.w+ (array.sel.y-1)*array.dify))
  107.         object.sel.model:rotation(math.rad(array.rot.x), math.rad(array.rot.y) ,math.rad(array.rot.z))
  108.         object.sel.model:blit()
  109.     end
  110. end
  111.  
  112. function functions.next_turn(array)
  113.     array.turn = array.turn + 1 if array.turn > #array.J then array.turn = 1 end
  114. end
  115.  
  116. function functions.move_selector(array)
  117.     if controls.press("up") then
  118.         array.sel.y = array.sel.y - 1; if array.sel.y < 1 then array.sel.y = #array.table end
  119.     end
  120.     if controls.press("down") then
  121.         array.sel.y = array.sel.y + 1; if array.sel.y > #array.table then array.sel.y = 1 end
  122.     end
  123.     if controls.press("left") then
  124.         array.sel.x = array.sel.x - 1; if array.sel.x < 1 then array.sel.x = #array.table[1] end
  125.     end
  126.     if controls.press("right") then
  127.         array.sel.x = array.sel.x + 1; if array.sel.x > #array.table[1] then array.sel.x = 1 end
  128.     end
  129. end
  130.  
  131. function fb(array,x,y)
  132.     if  x <= #array.table[1] and y <= #array.table
  133.     and x >= 1 and y >= 1 then return true else return false end
  134. end
  135.  
  136. function functions.check(array)
  137.     local cont = {hor_right = 1,hor_left = 1,ver_up = 1,ver_down = 1,dia_up_inc = 1,dia_up_dec = 1,dia_down_inc = 1,dia_down_dec = 1};
  138.     local completed = true;
  139.     local s = "";
  140.     for y = 1 , #array.table do
  141.         for x = 1, #array.table[y] do
  142.             if array.table[y][x] == "" then completed = false; end --board completed
  143.             if array.table[y][x] != "" then            
  144.                 s = array.table[y][x]
  145.                 for i = 1, array.n do
  146.                     if fb(array,x+i,y) and array.table[y][x+i] == s then cont.hor_right = cont.hor_right+1 end
  147.                     if fb(array,x,y+i) and array.table[y+i][x] == s then cont.ver_down = cont.ver_down+1 end
  148.                     if fb(array,x-i,y) and array.table[y][x-i] == s then cont.hor_left = cont.hor_left+1 end
  149.                     if fb(array,x,y-i) and array.table[y-i][x] == s then cont.ver_up = cont.ver_up+1 end
  150.                     if fb(array,x+i,y-i) and array.table[y-i][x+i] == s then cont.dia_up_dec = cont.dia_up_dec + 1 end
  151.                     if fb(array,x-i,y-i) and array.table[y-i][x-i] == s then cont.dia_up_inc = cont.dia_up_inc + 1 end
  152.                     if fb(array,x+i,y+i) and array.table[y+i][x+i] == s then cont.dia_down_inc = cont.dia_down_inc + 1 end
  153.                     if fb(array,x-i,y+i) and array.table[y+i][x-i] == s then cont.dia_down_dec = cont.dia_down_dec+1 end                   
  154.                 end
  155.                 if cont.hor_right >= array.n then return s end cont.hor_right = 1;
  156.                 if cont.hor_left >= array.n then return s end cont.hor_left = 1;                   
  157.                 if cont.ver_up >= array.n then return s end cont.ver_up = 1;
  158.                 if cont.ver_down >= array.n then return s end cont.ver_down = 1;                   
  159.                 if cont.dia_up_inc >= array.n then return s end cont.dia_up_inc = 1;               
  160.                 if cont.dia_down_inc >= array.n then return s end cont.dia_down_inc = 1;   
  161.                 if cont.dia_up_dec >= array.n then return s end cont.dia_up_dec = 1;               
  162.                 if cont.dia_down_dec >= array.n then return s end cont.dia_down_dec = 1;
  163.                 cont = {hor_right = 1,hor_left = 1,ver_up = 1,ver_down = 1,dia_up_inc = 1,dia_up_dec = 1,dia_down_inc = 1,dia_down_dec = 1};
  164.                
  165.             end
  166.         end
  167.     end
  168.     return completed;
  169. end
  170.  
  171. function functions.check_winner(s,array)
  172. for i = 1, #array.J do
  173.     if array.J[i].string == s then return i end
  174. end
  175. return false
  176. end
  177.  
  178. function functions.fill_scenery(array)
  179.     local winner = functions.check(array)
  180.     if  winner != nil then
  181.         if type(winner) == "boolean" and winner then
  182.             os.message("Tie.")
  183.             reset()
  184.         elseif type(winner) == "string" then
  185.             os.message("Player "..functions.check_winner(winner,array).." wins.")
  186.             reset()
  187.         end
  188.     end
  189.     if controls.press("cross") then
  190.         if array.table[array.sel.y][array.sel.x] == "" then
  191.             array.table[array.sel.y][array.sel.x] = array.J[array.turn].string
  192.             functions.next_turn(array)
  193.         end            
  194.     end
  195. end
  196. ------
  197. function reset()
  198.     scenery[n].table = {
  199.         {"","",""},
  200.         {"","",""},
  201.         {"","",""}
  202.         }
  203.     scenery[2].table={
  204.         {"o","x",""},
  205.         {"x","o","o"},
  206.         {"o","o","x"}
  207.         }
  208. end
  209.  
  210. functions.time:start()
  211.  
  212. while true do
  213. controls.read()
  214.  
  215. functions.draw_scenery(scenery[n])
  216. functions.draw_scenery_contorn(scenery[n], color.new(0,255,0))
  217. functions.move_selector(scenery[n])
  218. functions.print_selector(scenery[n])
  219.  
  220. ----------Test things
  221. if scenery[n].mode == "3D" then
  222.     --testing models
  223.     if controls.press("square") then
  224.         scenery[2].J[1].object = mdls[math.random(1,#mdls)]
  225.     end
  226.     if controls.press("circle") then
  227.         scenery[2].J[2].object = mdls[math.random(1,#mdls)]
  228.     end
  229.     --changing coordinates, positions & rotations
  230.     if controls.r() then scenery[n].pos.z = scenery[n].pos.z-1 end
  231.     if controls.l() then scenery[n].pos.z = scenery[n].pos.z+1 end
  232.         if controls.start() then
  233.             if controls.down() then scenery[n].pos.y = scenery[n].pos.y-1 end
  234.             if controls.up() then scenery[n].pos.y = scenery[n].pos.y+1 end
  235.             if controls.right() then scenery[n].pos.x = scenery[n].pos.x+1 end
  236.             if controls.left() then scenery[n].pos.x = scenery[n].pos.x-1 end                  
  237.             if math.abs(controls.analogy()) > 40 then scenery[n].rot.x = scenery[n].rot.x + controls.analogy()/40 end
  238.             --if math.abs(controls.analogx()) > 40 then scenery[n].rot.y = scenery[n].rot.y + controls.analogx()/40 end        
  239.         end
  240. else
  241.     if math.abs(controls.analogx()) > 40 then scenery[n].x = scenery[n].x + controls.analogx()/40 end
  242.     if math.abs(controls.analogy()) > 40 then scenery[n].y = scenery[n].y + controls.analogy()/40 end
  243. end
  244.  
  245. if controls.start() and controls.r() and controls.l() then
  246.     reset()
  247.     scenery[2].pos={x = 1,y = -6,z=-43} scenery[2].rot={x=0,y=0,z=0}
  248. end
  249.  
  250. if controls.l() and controls.r() and controls.right() then n = 2 end
  251. if controls.l() and controls.r() and controls.left() then n = 1 end
  252.  
  253. screen.print(400,5,"@"..screen.fps())
  254. if controls.select() then a() end
  255. screen.print(5,5,"Turn: "..scenery[n].turn.." S: "..n.."  Mode: "..scenery[n].mode)
  256. screen.print(5,25,"Pos.x: "..scenery[2].pos.x.."  Pos.y: "..scenery[2].pos.y.."  Pos.z: "..scenery[2].pos.z.."    rot.x"..scenery[2].rot.x)
  257. screen.flip()
  258. functions.fill_scenery(scenery[n])
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement