Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(fs.exists("blittle"))then os.loadAPI("blittle") end
- defWindow = term.current()
- if(fs.exists("blittle"))then
- litWindow = blittle.createWindow()
- else
- litWindow = term.current()
- end
- local w,h = term.getSize()
- mode = 1
- -- starting x and y
- posx = 15
- posy = 11
- xvel = 0
- yvel = 0
- damp = 2
- -- 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 = 1.33 -- (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 = 60
- bobz = 0
- bobv = 1
- viewbob = 0.15
- dobob = true
- -- misc
- rotspd = 0.045
- movspd = 0.10
- lookspd = 0.8
- mw, ms, ma, md, ml, mr, mup, mdown = false, false, false, false, false, false, false, false
- 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 renderMode()
- if(mode == 1 and fs.exists("blittle"))then
- term.clear()
- term.redirect(litWindow)
- w,h = term.getSize()
- end
- if(mode == 0)then
- term.clear()
- term.redirect(defWindow)
- w,h = term.getSize()
- end
- _draw()
- end
- function renderSwap()
- if(mode == 1)then mode = 0 end
- renderMode()
- end
- function rad(n)
- return n * (3.14159/180) + (0.001)
- end
- function mcut(n,degree)
- if(math.abs(n) > 1/degree)then return n end
- return math.floor(n * degree)/degree
- end
- function updatefov()
- -- dirx*-1 / planey = n*90 = fov
- -- planey = -dirx * (fov/90)
- dirx = -(planey / (fov/90))
- end
- function wmap(x,y)
- x = math.floor(x)
- y = math.floor(y)
- return world[y][x]
- end
- function _init()
- cls()
- end
- function _draw()
- mw, ms, ma, md, ml, mr, mup, mdown = false, false, false, false, false, false, false, false
- updatefov()
- term.setBackgroundColor(colors.gray)
- term.clear()
- 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 = math.floor(posx)
- local mapy = math.floor(posy)
- local sidex, sidey, stepx, stepy, wall, side
- local hit = 0
- -- length of ray from one side to another
- local rdirl=math.sqrt(rdirx * rdirx + rdiry * rdiry)
- -- Normally i'd have a second sqrt for each ray delta.
- -- This is a little faster than that, and prevents the small chance of
- -- strange sqrt underflows.
- raydx = rdirl/math.abs(rdirx)
- raydy = rdirl/math.abs(rdiry)
- -- find which direction to step in
- -- this step direction moves the ray around the map.
- -- these side variables are for determining north/south/east/west sides of the walls.
- if(rdirx < 0)then
- stepx = -1
- sidex = (posx - mapx) * raydx
- else
- stepx = 1
- sidex = (mapx + 1.0 - posx) * raydx
- end
- if(rdiry < 0)then
- stepy = -1
- sidey = (posy - mapy) * raydy
- else
- stepy = 1
- sidey = (mapy + 1.0 - posy) * raydy
- end
- -- draw lines
- while (hit == 0) do
- -- keep moving ray until it hits
- if(sidex < sidey)then
- sidex = sidex + raydx
- mapx = mapx + stepx
- side = 0
- else
- sidey = sidey + raydy
- mapy = mapy + stepy
- side = 1
- end
- -- check if ray has hit a wall
- if(world[mapy][mapx] > 0)then hit = 1 end
- end
- -- Calculate distance ray has traveled, this is later used to determine the height
- -- to draw the wall.
- 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 = math.floor( (h / wall) )
- -- calculate lowest/highest pixel to fill stripe
- drawstart = math.floor(-lineh /2 + (h+dirz+bobz)/2)
- drawend = math.floor( lineh /2 + (h+dirz+bobz)/2)
- if(drawstart < 0)then drawstart = 0 end
- if(drawend >= h )then drawend = h - 1 end
- -- get color of wall
- box = wmap(mapx,mapy)
- if(side == 0)then -- x sides
- if(box == 1)then boxcol = colors.red
- elseif(box == 2)then boxcol = colors.lime -- green
- elseif(box == 3)then boxcol = colors.lightBlue -- blue
- elseif(box == 4)then boxcol = colors.white -- white
- elseif(box == 5)then boxcol = colors.yellow -- yellow
- else boxcol = colors.white end -- white
- elseif(side == 1)then -- y sides
- if(box == 1)then boxcol = colors.orange -- violet
- elseif(box == 2)then boxcol = colors.green -- dark-green
- elseif(box == 3)then boxcol = colors.blue -- dark-blue
- elseif(box == 4)then boxcol = colors.lightGray -- gray
- elseif(box == 5)then boxcol = colors.white -- white
- else boxcol = colors.white end
- end
- -- draw striped line
- paintutils.drawLine(x, drawstart, x, drawend, boxcol)
- end
- end
- function rotate(rdir)
- local oldx = dirx
- local oldpx = planex
- dirx = dirx * math.cos(rdir) - diry * math.sin(rdir)
- diry = oldx * math.sin(rdir) + diry * math.cos(rdir)
- planex = planex * math.cos(rdir) - planey * math.sin(rdir)
- planey = oldpx * math.sin(rdir) + planey * math.cos(rdir)
- end
- local pmx = 0
- local pmy = 0
- function _update60()
- local e = {os.pullEvent()}
- if(e[1] == "key")then
- local k = e[2]
- if(k == keys.w )then mw = true else mw = false end
- if(k == keys.s )then ms = true else ms = false end
- if(k == keys.a )then ma = true else ma = false end
- if(k == keys.d )then md = true else md = false end
- if(k == keys.up )then mup = true else mup = false end
- if(k == keys.down )then mdown = true else mdown = false end
- if(k == keys.left )then ml = true else ml = false end
- if(k == keys.right)then mr = true else mr = false end
- if(k == keys.b)then dobob = not dobob end
- if(k == keys.g)then renderSwap() end
- end
- local moved = false
- -- player input!
- if(mw)then -- up
- if(xvel/dirx < movspd)then xvel = xvel + dirx * movspd/damp end
- if(yvel/diry < movspd)then yvel = yvel + diry * movspd/damp end
- moved = true
- end
- if(ms)then -- down
- if(xvel/dirx > -movspd)then xvel = xvel - dirx * movspd/damp end
- if(yvel/diry > -movspd)then yvel = yvel - diry * movspd/damp end
- moved = true
- end
- if(md)then -- strafe right
- rotate(rad(90))
- if(xvel/dirx > -movspd)then xvel = xvel - dirx * movspd/damp end
- if(yvel/diry > -movspd)then yvel = yvel - diry * movspd/damp end
- rotate(-rad(90))
- moved = true
- end
- if(ma)then -- strafe left
- rotate(-rad(90))
- if(xvel/dirx > -movspd)then xvel = xvel - dirx * movspd/damp end
- if(yvel/diry > -movspd)then yvel = yvel - diry * movspd/damp end
- rotate(rad(90))
- moved = true
- end
- if(mup)then -- look up
- dirz = dirz + lookspd
- end
- if(mdown)then -- look down
- dirz = 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) and dobob)then
- bobz = bobz + viewbob * bobv
- if(bobz > 1.2)then bobv = bobv * -1 end
- if(bobz < -1.2)then bobv = bobv * -1 end
- else
- bobz = bobz / 2
- if(math.abs(bobz) < 0.001)then bobz = 0 end
- end
- -- slow when not moving
- if(not moved)then
- xvel = 0
- yvel = 0
- end
- -- move
- posx = posx + xvel
- posy = posy + yvel
- -- rotation!
- if(ml)then -- right
- rotate(rotspd)
- end
- if(mr)then -- left
- rotate(-rotspd)
- end
- end
- renderMode()
- while running do
- term.current().setVisible(false)
- _draw()
- _update60()
- term.current().setVisible(true)
- end
Add Comment
Please, Sign In to add comment