Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------------
- --
- -- THE GCREW TEAM PRESENTS...
- --
- -- pero mecawen... que es esto???
- --
- --
- ------------------------------------------------------------------------
- -- SEGUN LUAPLAYER, las funciones
- drect = image:fillRect
- dfrect = image:fillRect; -- no se k version de luadev tienes , asi k por si las moscas...
- dline = image:fillRect;
- System.setHigh() ; -- esto requiere power... xD
- -- CAMBIAR RESOLUCION? PARECE DE VERDAD XD
- thewid = 8;
- themode = 1;
- 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"} }
- --
- -- PREPARAR NUESTRO BONITO ENTORNO 3D...
- --
- -- MAPA DEL MUND0:
- -- 0 <- implica NO HAY NADA.
- -- X <- cuaquier numero indica textura numero (X-1).
- WORLD_MAP = {
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- };
- -- Definimos nuestro entorno de "raytracing", para ello...
- -- Definimos el rayo en "ray".
- -- Inicializaremos las texturas, y definiremos su tamaño cuadrado en "tex".
- -- También hemos de definir la posicion del jugador en "pos".
- -- Y la rotación de jugador en "rot".
- CAM = {
- ray = { height=32, a=math.rad(60), w=60, h=272, distance=277 },
- tex = {
- --img = image.loadsprite("textures.png",64,64),
- size = 64
- },
- pos = { x=6.5,y=5.5 },
- rot = 0,
- }
- --
- --CAM.ray.distance = ((CAM.ray.w / 2) / math.tan(CAM.ray.a / 2));
- function raytrace()
- -- cast all rays here
- local sina = math.sin(CAM.rot);
- local cosa = math.cos(CAM.rot);
- local u = cosa - sina;
- local v = sina + cosa;
- local du = 2 * sina / CAM.ray.w;
- local dv = -2 * cosa / CAM.ray.w;
- -- nuestra ventana es de CAM.ray.w pixels de ancho, por lo que, por cada "columna.."
- for col=0,CAM.ray.w,1 do
- -- palante como los de alicante
- local uu = (u < 0) and -u or u;
- local vv = (v < 0) and -v or v;
- local duu = 1 / uu;
- local dvv = 1 / vv;
- local stepx = math.floor((u < 0) and -1 or 1);
- local stepy = math.floor((v < 0) and -1 or 1);
- -- donde toy?
- local px = CAM.pos.x;
- local py = CAM.pos.y;
- local mapx = math.floor(px);
- local mapy = math.floor(py);
- -- varis varis
- local texture = 0;
- local hitdist = 0.1;
- local texofs = 0;
- local dark = false
- -- lineas de direccion...
- local distx = (u > 0) and (mapx + 1 - px) * duu or (px - mapx) * duu;
- local disty = (v > 0) and (mapy + 1 - py) * dvv or (py - mapy) * dvv;
- -- hasta que me ahostie
- while (texture <= 0) do
- if (distx > disty) then
- -- mas cerca en y
- hitdist = disty;
- disty = disty + dvv;
- mapy = mapy + stepy;
- if ( mapy >= #WORLD_MAP ) then mapy = #WORLD_MAP; end
- if ( mapy < 1 ) then mapy = 1; end
- if (mapy <= 1) or (mapy >= (#WORLD_MAP -1)) then texture = 1; else texture = WORLD_MAP[mapy][mapx]; end
- if (texture > 0) then
- dark = true;
- if (stepy > 0) then
- local ofs = px + u * (mapy - py) / v;
- texofs = ofs - math.floor(ofs);
- else
- local ofs = px + u * (mapy + 1 - py) / v;
- texofs = ofs - math.floor(ofs);
- end
- end
- else
- -- mas cerca en x
- hitdist = distx;
- distx = distx + duu;
- mapx = mapx + stepx;
- if ( mapx > #WORLD_MAP[1] ) then mapx = #WORLD_MAP[1]; end
- if ( mapx < 1 ) then mapx = 1; end
- if (mapx <= 1 or mapx >= #WORLD_MAP[1]) then texture = 1; else texture = WORLD_MAP[mapy][mapx]; end
- if (texture > 0) then
- if (stepx > 0) then
- local ofs = py + v * (mapx - px) / u;
- texofs = ofs - math.floor(ofs);
- else
- local ofs = py + v * (mapx + 1 - px) / u;
- texofs = math.ceil(ofs) - ofs;
- end
- end
- end
- end
- -- Textura y distancia resuelta...
- local ph = (CAM.tex.size / hitdist) * CAM.ray.distance;
- ph = math.abs( (3-hitdist) * CAM.ray.h / 3 );
- --CAM.tex.img:setframe(texture-1);
- --CAM.tex.img:resize(ph,ph);
- -- El rectangulo en la pantalla es...
- local pr = { x=col, y=(136-(ph/2)), h=ph, w=1 }
- local hd = math.max(math.min(hitdist,2.55),0);
- local hdd = 2.55 - hd;
- local c = Color.new((hdd*100),(hdd*100),(hdd*100),255);
- dfrect(col*thewid,(136-(ph/2)),thewid,ph,c);
- u = u + du;
- v = v + dv;
- --CAM.tex.img:blit(pr.x,pr.y,1,0,1,ph);
- end
- end
- function mapdraw()
- for i=1,#WORLD_MAP do
- for x=1,#WORLD_MAP[i] do
- if WORLD_MAP[i][x] == 1 then drect(240+(i*10),x*10,10,10,0xFFFFFFFF); end
- if WORLD_MAP[i][x] == 0 then drect(240+(i*10),x*10,10,10,0x00FF00FF); end
- if ((CAM.pos.x > x and CAM.pos.x < x+1) and (CAM.pos.y > i and CAM.pos.y < i+1)) then
- 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));
- 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));
- 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));
- else dfrect(240+((i-1)*10)+3,((x)*10)+3,3,3,Color.new(255,0,0)); end
- dfrect(240+(i*10),x*10,10,10,Color.new(255,0,0));
- end
- end
- end
- end
- mapnabled = true;
- oldpad=Controls.read(9
- while true do
- pad = Controls.read();
- raytrace();
- if mapnabled then mapdraw(); end
- screen:print(10,10,"Lua RayCaster 0.01 @ "..screen.fps().."fps");
- screen:print(10,30,"Resolution: "..theconfig[themode][3]);
- screen.waitVblankStart()
- screen.flip();
- if pad:select() and oldpad:select() ~= pad:select() then mapnabled = not mapnabled; end
- 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
- 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
- if pad:start() and oldpad:start() ~= pad:start() then
- themode = themode + 1; if themode > #theconfig then themode = 1; end
- CAM.ray.w = theconfig[themode][1];
- thewid = theconfig[themode][2];
- end
- if pad:up() then CAM.pos.x = CAM.pos.x - 0.1; end
- if pad:down() then CAM.pos.x = CAM.pos.x + 0.1; end
- if pad:right() then CAM.pos.y = CAM.pos.y + 0.1; end
- oldpad = pad
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement