Advertisement
xerpi

Untitled

Mar 11th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.45 KB | None | 0 0
  1. ------------------------------------------------------------------------
  2. --
  3. -- THE GCREW TEAM PRESENTS...
  4. --
  5. --      pero mecawen... que es esto???
  6. --
  7. --
  8. ------------------------------------------------------------------------
  9.  
  10. -- SEGUN LUAPLAYER, las funciones
  11. drect = image:fillRect
  12. dfrect = image:fillRect; -- no se k version de luadev tienes , asi k por si las moscas...
  13. dline = image:fillRect;
  14. System.setHigh() ; -- esto requiere power... xD
  15.  
  16. -- CAMBIAR RESOLUCION? PARECE DE VERDAD XD
  17. thewid = 8;
  18. themode = 1;
  19. theconfig = { {60, 8, "High-speed FULL"}, {120, 4, "Medium FULL"}, {240, 2, "LOW FULL"}, {480,1, "XTREME GRAPHICS"}, {240,1, "Half extreme"}, {120,2, "half doubled"} }
  20.  
  21.  
  22. --
  23. -- PREPARAR NUESTRO BONITO ENTORNO 3D...
  24. --
  25.  
  26. -- MAPA DEL MUND0:
  27. -- 0 <- implica NO HAY NADA.
  28. -- X <- cuaquier numero indica textura numero (X-1).
  29. WORLD_MAP = {
  30.     { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  31.     { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  32.     { 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
  33.     { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
  34.     { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
  35.     { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
  36.     { 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
  37.     { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  38.     { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  39. };
  40.  
  41. -- Definimos nuestro entorno de "raytracing", para ello...
  42. -- Definimos el rayo en "ray".
  43. -- Inicializaremos las texturas, y definiremos su tamaño cuadrado en "tex".
  44. -- También hemos de definir la posicion del jugador en "pos".
  45. -- Y la rotación de jugador en "rot".
  46. CAM = {
  47.     ray = { height=32, a=math.rad(60), w=60, h=272, distance=277 },
  48.     tex = {
  49.         --img = image.loadsprite("textures.png",64,64),
  50.         size = 64
  51.         },
  52.     pos = { x=6.5,y=5.5 },
  53.     rot = 0,
  54. }
  55.  
  56. --
  57. --CAM.ray.distance = ((CAM.ray.w / 2) / math.tan(CAM.ray.a / 2));
  58.  
  59. function raytrace()
  60.     -- cast all rays here
  61.     local sina = math.sin(CAM.rot);
  62.     local cosa = math.cos(CAM.rot);
  63.     local u = cosa - sina;
  64.     local v = sina + cosa;
  65.     local du = 2 * sina / CAM.ray.w;
  66.     local dv = -2 * cosa / CAM.ray.w;
  67.    
  68.     -- nuestra ventana es de CAM.ray.w pixels de ancho, por lo que, por cada "columna.."
  69.     for col=0,CAM.ray.w,1 do
  70.         -- palante como los de alicante
  71.         local uu = (u < 0) and -u or u;
  72.         local vv = (v < 0) and -v or v;
  73.         local duu = 1 / uu;
  74.         local dvv = 1 / vv;
  75.         local stepx = math.floor((u < 0) and -1 or 1);
  76.         local stepy = math.floor((v < 0) and -1 or 1);
  77.    
  78.         -- donde toy?
  79.         local px = CAM.pos.x;
  80.         local py = CAM.pos.y;
  81.         local mapx = math.floor(px);
  82.         local mapy = math.floor(py);
  83.    
  84.         -- varis varis
  85.         local texture = 0;
  86.         local hitdist = 0.1;
  87.         local texofs = 0;
  88.         local dark = false
  89.        
  90.         -- lineas de direccion...
  91.         local distx = (u > 0) and (mapx + 1 - px) * duu or (px - mapx) * duu;
  92.         local disty = (v > 0) and (mapy + 1 - py) * dvv or (py - mapy) * dvv;
  93.        
  94.         -- hasta que me ahostie
  95.         while (texture <= 0) do
  96.             if (distx > disty) then
  97.                 -- mas cerca en y
  98.                 hitdist = disty;
  99.                 disty = disty + dvv;
  100.                 mapy = mapy + stepy;
  101.                 if ( mapy >= #WORLD_MAP ) then mapy = #WORLD_MAP; end
  102.                 if ( mapy < 1 ) then mapy = 1; end
  103.                 if (mapy <= 1) or (mapy >= (#WORLD_MAP -1)) then texture = 1; else texture = WORLD_MAP[mapy][mapx]; end
  104.                 if (texture > 0) then
  105.                     dark = true;
  106.                     if (stepy > 0) then
  107.                         local ofs = px + u * (mapy - py) / v;
  108.                         texofs = ofs - math.floor(ofs);
  109.                     else
  110.                         local ofs = px + u * (mapy + 1 - py) / v;
  111.                         texofs = ofs - math.floor(ofs);
  112.                     end
  113.                 end
  114.             else
  115.                 -- mas cerca en x
  116.                 hitdist = distx;
  117.                 distx = distx + duu;
  118.                 mapx = mapx + stepx;
  119.                 if ( mapx > #WORLD_MAP[1] ) then mapx = #WORLD_MAP[1]; end
  120.                 if ( mapx < 1 ) then mapx = 1; end
  121.                 if (mapx <= 1 or mapx >= #WORLD_MAP[1]) then texture = 1; else texture = WORLD_MAP[mapy][mapx]; end
  122.                 if (texture > 0) then
  123.                     if (stepx > 0) then
  124.                         local ofs = py + v * (mapx - px) / u;
  125.                         texofs = ofs - math.floor(ofs);
  126.                     else
  127.                         local ofs = py + v * (mapx + 1 - px) / u;
  128.                         texofs = math.ceil(ofs) - ofs;
  129.                     end
  130.                 end
  131.             end
  132.         end
  133.        
  134.         -- Textura y distancia resuelta...
  135.  
  136.         local ph = (CAM.tex.size / hitdist) * CAM.ray.distance;
  137.         ph = math.abs( (3-hitdist) * CAM.ray.h / 3 );
  138.         --CAM.tex.img:setframe(texture-1);
  139.         --CAM.tex.img:resize(ph,ph);
  140.        
  141.         -- El rectangulo en la pantalla es...
  142.         local pr = { x=col, y=(136-(ph/2)), h=ph, w=1 }
  143.         local hd = math.max(math.min(hitdist,2.55),0);
  144.         local hdd = 2.55 - hd;
  145.         local c = Color.new((hdd*100),(hdd*100),(hdd*100),255);
  146.         dfrect(col*thewid,(136-(ph/2)),thewid,ph,c);
  147.        
  148.         u = u + du;
  149.         v = v + dv;
  150.         --CAM.tex.img:blit(pr.x,pr.y,1,0,1,ph);
  151.     end
  152. end
  153.  
  154. function mapdraw()
  155.     for i=1,#WORLD_MAP do
  156.         for x=1,#WORLD_MAP[i] do
  157.             if WORLD_MAP[i][x] == 1 then drect(240+(i*10),x*10,10,10,0xFFFFFFFF); end
  158.             if WORLD_MAP[i][x] == 0 then drect(240+(i*10),x*10,10,10,0x00FF00FF); end
  159.             if ((CAM.pos.x > x and CAM.pos.x < x+1) and (CAM.pos.y > i and CAM.pos.y < i+1)) then
  160.                 if CAM.rot <= 0.6 or CAM.rot >= 5.9 then dfrect(240+((i+0)*10)+3,((x+1)*10)+3,3,3,Color.new(255,0,0));
  161.                 elseif CAM.rot > 0.6 and CAM.rot <= 2.3 then dfrect(240+((i+1)*10)+3,((x+0)*10)+3,3,3,Color.new(255,0,0));
  162.                 elseif CAM.rot > 2.3 and CAM.rot < 3.8 then dfrect(240+((i-0)*10)+3,((x-1)*10)+3,3,3,Color.new(255,0,0));
  163.                 else dfrect(240+((i-1)*10)+3,((x)*10)+3,3,3,Color.new(255,0,0)); end
  164.                 dfrect(240+(i*10),x*10,10,10,Color.new(255,0,0));
  165.             end
  166.         end
  167.     end
  168. end
  169.  
  170. mapnabled = true;
  171. oldpad=Controls.read(9
  172. while true do
  173.     pad = Controls.read();
  174.     raytrace();
  175.     if mapnabled then mapdraw(); end
  176.     screen:print(10,10,"Lua RayCaster 0.01 @ "..screen.fps().."fps");
  177.     screen:print(10,30,"Resolution: "..theconfig[themode][3]);
  178.     screen.waitVblankStart()
  179.     screen.flip();
  180.     if pad:select() and oldpad:select() ~= pad:select() then mapnabled = not mapnabled; end
  181.     if pad:r() then CAM.rot = math.deg(CAM.rot) + 5; if CAM.rot > 360 then CAM.rot = CAM.rot - 360; end CAM.rot = math.rad(CAM.rot); end
  182.     if pad:l() then CAM.rot = math.deg(CAM.rot) - 5; if CAM.rot < 0 then CAM.rot = CAM.rot + 360; end CAM.rot = math.rad(CAM.rot); end
  183.     if pad:start() and oldpad:start() ~= pad:start() then
  184.         themode = themode + 1; if themode > #theconfig then themode = 1; end
  185.         CAM.ray.w = theconfig[themode][1];
  186.         thewid = theconfig[themode][2];
  187.     end
  188.     if pad:up() then CAM.pos.x = CAM.pos.x - 0.1; end
  189.     if pad:down() then CAM.pos.x = CAM.pos.x + 0.1; end
  190.     if pad:right() then CAM.pos.y = CAM.pos.y + 0.1; end
  191.      oldpad = pad
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement