Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = 127, 90
- -- starting x and y
- posx = 15
- posy = 11
- xvel = 0
- yvel = 0
- damp = 2
- fric = 4
- -- direction vector (looking direction)
- dirx = 0 -- angle size of rays
- diry = 0 -- left/right offset of rays
- -- up/down
- dirz = 0
- -- 2d angles in which rays enter the world at
- planex = 0 -- (angle rotation)
- planey = 0.66 -- (angle size)
- -- field of vision
- -- this takes the viewport "width" (planey) and the
- -- max ray angles (dirx), and divides them to find the fov
- -- 1:1 = 90
- -- some simple algebra is used to change this to be fov
- -- variable dependant, hence why dirx isn't defined.
- -- dirx / planey
- fov = 100
- bobz = 0
- bobv = 1
- viewbob = 0.36
- -- misc
- rotspd = 0.045
- movspd = 0.10
- lookspd = 0.8
- running = true
- linebug = 0
- debug = false
- world = {
- {1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
- {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1},
- {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
- {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,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}
- }
- function rad(n)
- return n * (3.14159/180) + (0.001)
- end
- function msin(n) return sin(n*0.159) end
- function mcos(n) return cos(n*0.159) end
- function mcut(n,degree)
- if(abs(n) > 1/degree)return n
- return flr(n * degree)/degree
- end
- function updatefov()
- dirx = -(planey / (fov/90))
- end
- function wmap(x,y)
- x = flr(x)
- y = flr(y)
- return world[y][x]
- end
- function _init()
- cls()
- end
- function drawhud()
- rectfill(0,h,128,128,5)
- if(debug)then
- print("rayx: " .. raydx,0,h+8,7)
- print("rayy: " .. raydy,0,h+16,7)
- print("checkerr: " .. linebug,0,h+24,7)
- end
- end
- function _draw()
- updatefov()
- rectfill(0,0,w,(h+dirz+bobz)/2,15)
- rectfill(0,(h+dirz+bobz)/2,w,h,4)
- for x = 0, w do
- -- camera offset relative to middle
- -- -n = left, 0 = middle, +n = right
- local camx = (2 * x) / w -1
- -- min/max ranges of ray directions
- local rdirx = dirx + planex * camx
- local rdiry = diry + planey * camx
- local mapx = flr(posx)
- local mapy = flr(posy)
- local sidex, sidey, stepx, stepy, wall, side
- local hit = 0
- -- length of ray from current x/yside to the next one
- local rdirl=sqrt(rdirx * rdirx + rdiry * rdiry)
- raydx = rdirl/abs(rdirx) -- distance from one x-side to the other
- raydy = rdirl/abs(rdiry) -- distance from one y-side to the other
- -- was a north-south or east-west wall hit?
- -- determine direction to move ray towards
- if(rdirx < 0)then
- stepx = -1
- -- length to first x side
- sidex = (posx - mapx) * raydx
- else
- stepx = 1
- sidex = (mapx + 1.0 - posx) * raydx
- end
- if(rdiry < 0)then
- stepy = -1
- -- length to first y side
- sidey = (posy - mapy) * raydy
- else
- stepy = 1
- sidey = (mapy + 1.0 - posy) * raydy
- end
- -- draw lines
- while (hit == 0) do
- -- move ray along the map until it hits a wall
- if(sidex < sidey)then
- -- this is an north-south wall side.
- sidex += raydx
- mapx += stepx
- side = 0
- else
- -- this is an east-west wall side.
- sidey += raydy
- mapy += stepy
- side = 1
- end
- -- check if ray has hit a wall
- if(world[mapy][mapx] > 0) hit = 1
- end
- -- calculate distance ray has traveled from the player
- -- then use that distance to determine wall height
- if(side == 0)then
- wall = (mapx - posx + (1 - stepx) /2 ) / rdirx
- else
- wall = (mapy - posy + (1 - stepy) /2 ) / rdiry
- end
- -- calculate height of line
- local lineh = flr( (h / wall) )
- -- calculate lowest/highest pixel to fill stripe
- drawstart = flr(-lineh /2 + (h+dirz+bobz)/2)
- drawend = flr( lineh /2 + (h+dirz+bobz)/2)
- if(drawstart < 0) drawstart = 0
- if(drawend >= h ) drawend = h - 1
- -- get color of wall
- box = wmap(mapx,mapy)
- if(side == 0)then -- x sides
- if(box == 1)then boxcol = 8 -- red
- elseif(box == 2)then boxcol = 11 -- green
- elseif(box == 3)then boxcol = 12 -- blue
- elseif(box == 4)then boxcol = 7 -- white
- elseif(box == 5)then boxcol = 10 -- yellow
- else boxcol = 7 end -- white
- elseif(side == 1)then -- y sides
- if(box == 1)then boxcol = 2 -- violet
- elseif(box == 2)then boxcol = 3 -- dark-green
- elseif(box == 3)then boxcol = 1 -- dark-blue
- elseif(box == 4)then boxcol = 6 -- gray
- elseif(box == 5)then boxcol = 9 -- white
- else boxcol = 7 end -- orange
- end
- -- draw striped line
- line(x, drawstart, x, drawend, boxcol)
- end
- if(debug)then
- print("xv: " .. xvel/dirx, 0,8,0)
- print("yv: " .. yvel/diry, 0,16,0)
- print("z: " .. dirz, 0,24,0)
- print("rang: " .. dirx, 0,32,0)
- print("roff: " .. diry, 0,40,0)
- print("prot: " .. planex, 0,48,0)
- print("pwid: " .. planey, 0,56,0)
- end
- drawhud()
- end
- function rotate(rdir)
- local oldx = dirx
- local oldpx = planex
- dirx = dirx * mcos(rdir) - diry * msin(rdir)
- diry = oldx * msin(rdir) + diry * mcos(rdir)
- planex = planex * mcos(rdir) - planey * msin(rdir)
- planey = oldpx * msin(rdir) + planey * mcos(rdir)
- end
- function _update60()
- local moved = false
- -- player input!
- if(btn(2))then -- up
- if(xvel/dirx < movspd)xvel += dirx * movspd/damp
- if(yvel/diry < movspd)yvel += diry * movspd/damp
- moved = true
- end
- if(btn(3))then -- down
- if(xvel/dirx > -movspd)xvel -= dirx * movspd/damp
- if(yvel/diry > -movspd)yvel -= diry * movspd/damp
- moved = true
- end
- if(btn(0))then -- strafe left
- rotate(rad(90))
- if(xvel/dirx > -movspd)xvel -= dirx * movspd/damp
- if(yvel/diry > -movspd)yvel -= diry * movspd/damp
- rotate(-rad(90))
- moved = true
- end
- if(btn(1))then -- strafe right
- rotate(-rad(90))
- if(xvel/dirx > -movspd)xvel -= dirx * movspd/damp
- if(yvel/diry > -movspd)yvel -= diry * movspd/damp
- rotate(rad(90))
- moved = true
- end
- if(btn(2,1))then -- look up
- dirz += lookspd
- end
- if(btn(3,1))then -- look down
- dirz -= lookspd
- end
- -- collision
- if(wmap(posx+xvel, posy) != 0)then
- xvel = -(xvel/2)
- end
- if(wmap(posx,posy+yvel) != 0)then
- yvel = -(yvel/2)
- end
- -- bob whilst moving
- if(xvel != 0 or yvel != 0)then
- bobz += viewbob * bobv
- if(bobz > 5) bobv *= -1
- if(bobz < -5) bobv *= -1
- else
- bobz /= 2
- if(abs(bobz) < 0.001) bobz = 0
- end
- -- slow when not moving
- if(not moved)then
- -- friction
- xvel /= fric
- yvel /= fric
- end
- -- move
- posx += xvel
- posy += yvel
- -- rotation!
- if(btn(1,1))then -- right
- rotate(rotspd)
- end
- if(btn(0,1))then -- left
- rotate(-rotspd)
- end
- --pmx = mx
- --pmy = my
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement