Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local scr_x, scr_y = term.getSize()
- palateFile = fs.combine(shell.dir(),"palate")
- levelFile = "level"
- guy = {
- x = 2, --Your X position on the MAP, not the screen.
- y = 5, --Same, but with Y.
- speedY = 0, --I want to represent gravity fairly accurately.
- char = "O", --The character that represents you.
- mode = "", --Determines whether you are walking, jumping, whatever.
- grounded = false, --Whether or not you are on solid ground, and can jump.
- bgcol = colors.red, --Background color.
- txtcol = colors.yellow, --Text color.
- name = "Mario", --Thine name! I just added this in case I want to use it.
- }
- local tArg = {...}
- if tArg[1] then
- levelFile = fs.combine(shell.dir(),tArg[1])
- end
- if tArg[2] then
- palateFile = fs.combine(shell.dir(),tArg[2])
- end
- scrollX = 0
- scrollY = 0
- function CTB(_color) --Color To Blit
- local blitcolors = {
- [colors.white] = "0",
- [colors.orange] = "1",
- [colors.magenta] = "2",
- [colors.lightBlue] = "3",
- [colors.yellow] = "4",
- [colors.lime] = "5",
- [colors.pink] = "6",
- [colors.gray] = "7",
- [colors.lightGray] = "8",
- [colors.cyan] = "9",
- [colors.purple] = "a",
- [colors.blue] = "b",
- [colors.brown] = "c",
- [colors.green] = "d",
- [colors.red] = "e",
- [colors.black] = "f",
- }
- return blitcolors[_color]
- end
- if fs.exists(palateFile) then
- dofile(palateFile)
- end
- if not palate then
- palate = { --I say palate because it can (as should be expected to) be changed per game.
- [1] = { --Blocks will be stored as IDs, not by name.
- desc = "air", --Brief description of what it is supposed to represent.
- char = " ", --Character used to represent it.
- txtcol = colors.black, --TEXT color. Uses colors API, not blit.
- bgcol = colors.black, --BACKGROUND color. Again, colors API.
- solid = false, --Whether or not you can go through it.
- isLiquid = false, --Whether it is a liquid or not.
- touchDamage = 0, --Damage done by TOUCHING IT. Meaning, being inside it OR a space next to it.
- insideDamage = 0, --Damage done by BEING IN THE SAME SPACE AS IT. Not being next to it.
- gravity = 1, --Gravity (let's just say in G's) when INSIDE the block.
- }, --Oh BTW, [1] will always be air. That's constant. Also, it's not really a block, but an empty space.
- [2] = {
- desc = "sto ne",
- char = "L",
- txtcol = colors.lightGray,
- bgcol = colors.gray,
- solid = true,
- isLiquid = false,
- touchDamage = 0,
- insideDamage = 0,
- gravity = 1,
- },
- [3] = {
- desc = "woodplank",
- char = "=",
- txtcol = colors.orange,
- bgcol = colors.brown,
- solid = true,
- isLiquid = false,
- touchDamage = 0,
- insideDamage = 0,
- gravity = 1,
- },
- [4] = {
- desc = "water",
- char = "/",
- txtcol = colors.lightBlue,
- bgcol = colors.cyan,
- solid = false,
- isLiquid = true,
- touchDamage = 0,
- insideDamage = 0,
- gravity = 0.4,
- },
- [5] = {
- desc = "lava",
- char = "#",
- txtcol = colors.red,
- bgcol = colors.orange,
- solid = false,
- isLiquid = true,
- touchDamage = 0,
- insideDamage = 2,
- gravity = 1.2,
- },
- [6] = {
- desc = "cactus",
- char = "#",
- txtcol = colors.gray,
- bgcol = colors.green,
- solid = true,
- isLiquid = false,
- touchDamage = 1,
- insideDamage = 0,
- gravity = 1,
- },
- }
- local file = fs.open(palateFile,"w")
- file.write("palate = "..textutils.serialise(palate))
- file.close()
- end
- selectedItem = 1
- local level
- if levelFile then
- local file = fs.open(levelFile,"r")
- level = textutils.unserialize(file.readAll())
- file.close()
- else
- level = {{}}
- end
- function inflateLevel(_level)
- local output = _level
- local maxLong = 0
- local maxHigh = #level
- for a = 1, #_level do
- for b = 1, #_level[a] do
- if #_level[a][b] > maxLong then
- maxLong = #_level[a]
- end
- end
- end
- for a = 1, maxHigh do
- if not _level[a] then
- output[a] = {}
- end
- for b = 1, maxLong do
- if not _level[a][b] then
- output[a][b] = 1
- end
- end
- end
- return output
- end
- level = inflateLevel(level)
- local function clearDot(x,y,col)
- local pX,pY = term.getCursorPos()
- term.setCursorPos(x,y)
- if col then
- term.blit(" ",CTB(col),CTB(col))
- else
- term.write(" ")
- end
- term.setCursorPos(pX,pY)
- end
- function palateclear()
- local prevX,prevY = term.getCursorPos()
- term.setBackgroundColor(palate[1].bgcol)
- term.setTextColor(palate[1].txtcol)
- for a = 1, scr_y do
- term.setCursorPos(1,a)
- write(palate[1].char:rep(scr_x))
- end
- term.setCursorPos(prevX,prevY)
- end
- function renderLevel(_level,_scrollX,_scrollY)
- if guy.x > math.floor(scr_x/2) then
- scrollX = guy.x - math.floor(scr_x/2)
- term.clear()
- else
- clearDot(guy.x,guy.y,palate[level[guy.y][guy.x]].bgcol)
- end
- for y = 1,scr_y do
- for x = 1,scr_x do
- if level[x+scrollX] then
- lobster = palate[level[(scr_y-y)+scrollY+1][x+scrollX]] --Well it wasn't a rock
- if lobster then
- term.setCursorPos(x,y)
- term.blit(lobster.char,CTB(lobster.txtcol),CTB(lobster.bgcol))
- end
- end
- end
- end
- term.setCursorPos(guy.x-scrollX,(scr_y-guy.y)+scrollY)
- term.blit(guy.char,CTB(guy.txtcol),CTB(guy.bgcol))
- end
- function handleQuit()
- term.setCursorPos(1,scr_y)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- sleep(0)
- error() --This is probably the wrong way of going about doing this.
- end
- function handleGuyPhysics(mode)
- local gravity
- if level[guy.y] then
- if level[guy.y][guy.x] then
- gravity = palate[level[guy.y][guy.x]].gravity
- else
- gravity = palate[1].gravity
- end
- else
- gravity = palate[1].gravity
- end
- guy.speedY = guy.speedY - gravity
- if level[guy.x] then
- if level[guy.x][guy.y-gravity] then
- if palate[level[guy.y-gravity][guy.x]].solid then
- guy.grounded = true
- guy.speedY = 0
- else
- guy.grounded = false
- guy.y = guy.y + guy.speedY
- end
- else
- guy.grounded = false
- guy.y = guy.y + guy.speedY
- end
- end
- if mode == "jump" then
- if guy.grounded then
- guy.speedY = 5*gravity
- end
- end
- if mode == "walkRight" then
- if level[guy.x+1] then
- if level[guy.y][guy.x+1] then
- if not level[guy.y][guy.x+1].solid then
- guy.x = guy.x + 1
- end
- else
- guy.x = guy.x + 1
- end
- else
- guy.x = guy.x + 1
- end
- end
- if mode == "walkLeft" then
- if level[guy.y] then
- if level[guy.y][guy.x-1] then
- if not level[guy.y][guy.x-1].solid then
- guy.x = guy.x - 1
- end
- else
- guy.x = guy.x - 1
- end
- else
- guy.x = guy.x - 1
- end
- end
- end
- function handleKeyMovement()
- keysPressed = {
- ["right"] = false,
- ["left"] = false,
- ["space"] = false,
- }
- while true do
- local evt = {os.pullEvent()}
- if evt[1] == "key" then
- if evt[2] == keys.right then
- keysPressed["right"] = true
- elseif evt[2] == keys.left then
- keysPressed["left"] = true
- elseif evt[2] == keys.space then
- keysPressed["space"] = true
- end
- elseif evt[1] == "key_up" then
- if evt[2] == keys.right then
- keysPressed["right"] = false
- elseif evt[2] == keys.left then
- keysPressed["left"] = false
- elseif evt[2] == keys.space then
- keysPressed["space"] = false
- end
- end
- end
- end
- function doit()
- palateclear()
- renderLevel(level,scrollX,scrollY)
- while true do
- sleep(0.05)
- if keysPressed["space"] and guy.grounded then
- handleGuyPhysics("jump")
- end
- if keysPressed["right"] then
- handleGuyPhysics("walkRight")
- elseif keysPressed["left"] then
- handleGuyPhysics("walkLeft")
- else
- handleGuyPhysics()
- end
- renderLevel(level,scrollX,scrollY)
- end
- end
- parallel.waitForAny(doit,handleKeyMovement)
Add Comment
Please, Sign In to add comment