Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local curCols = {}
- buffer_details = {1, 1, colors.black, colors.white, nil}
- local sw,sh = term.getSize()
- function getColorTab(char, background, text)
- if curCols[char] ~= nil then
- if curCols[char][background] ~= nil then
- if curCols[char][background][text] ~= nil then
- return curCols[char][background][text]
- end
- end
- end
- return nil
- end
- function addColorTab(col)
- local char = col.char
- local background = col.background
- local text = col.text
- if getColorTab(char, background, text) == nil then
- if curCols[char] == nil then curCols[char] = {} end
- if curCols[char][background] == nil then curCols[char][background] = {} end
- curCols[char][background][text] = col
- return true
- end
- return false
- end
- function createColor(char, background, text)
- --sleep(0)
- local col = getColorTab(char, background, text)
- if col == nil then
- col = {}
- col.char = char
- col.background = background
- col.text = text
- addColorTab(col)
- end
- return col
- end
- function createPixel(x, y, col)
- local pixel = {}
- pixel.x = x
- pixel.y = y
- pixel.col = col
- pixel.compare = function(self, col)
- return self.col == col
- end
- return pixel
- end
- function createBuffer()
- local buffer = {}
- buffer.width = sw
- buffer.height = height
- buffer.changes = {}
- buffer.changed = {}
- buffer.term = term
- buffer_details[5] = buffer
- for x=1,sw do
- buffer[x] = {}
- buffer.changes[x] = {}
- for y=1,sh do
- --print("x="..x..", y="..y)
- buffer[x][y] = createPixel(x, y, createColor(" ", colors.black, colors.white))
- buffer.changes[x][y] = false
- end
- end
- buffer.get = function(self, x, y)
- if self[x] ~= nil then
- if self[x][y] ~= nil then
- return self[x][y]
- end
- end
- return nil
- end
- buffer.check = function(self, x, y)
- if self.changes[x] ~= nil then
- if self.changes[x][y] ~= nil then
- return self.changes[x][y]
- end
- end
- return false
- end
- buffer.set = function(self, x, y, col)
- local pixel = self:get(x,y)
- if pixel ~= nil then
- if not pixel:compare(col) then
- pixel.col = col
- if not self:check(x,y) then
- self.changed[#self.changed + 1] = {x,y}
- if self.changes[x] == nil then self.changes[x] = {} end
- self.changes[x][y] = true
- end
- end
- end
- end
- buffer.render = function(self)
- for i=1,#self.changed do
- local pos = self.changed[i]
- self.changes[pos[1]][pos[2]] = false
- local pixel = self[pos[1]][pos[2]]
- self.term.setCursorPos(pixel.x, pixel.y)
- self.term.setBackgroundColor(pixel.col.background)
- self.term.setTextColor(pixel.col.text)
- self.term.write(pixel.col.char)
- end
- term.setCursorPos(1, 13)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- print(#self.changed.." pixels changed")
- self.changed = {}
- end
- buffer.setBuffer = function(self)
- buffer_detials[5] = self
- end
- buffer.setCursorPos = function(x, y)
- buffer_details[1] = x
- buffer_details[2] = y
- end
- buffer.setBackgroundColor = function(col)
- buffer_details[3] = col
- end
- buffer.setTextColor = function(col)
- buffer_details[4] = col
- end
- buffer.getSize = function()
- local buffer = buffer_details[5]
- if buffer == nil then print("NIL_BUFFER") end
- return buffer_details[5].width, buffer_details[5].height
- end
- buffer.write = function(char)
- char = char:sub(1,1)
- local col = createColor(char, buffer_details[3], buffer_details[4])
- local buffer = buffer_details[5]
- if buffer ~= nil then
- buffer:set(buffer_details[1], buffer_details[2], col)
- end
- end
- buffer.clear = function()
- buffer_details[5]:reset()
- end
- buffer.reset = function(self)
- local buffer = self
- for x=1,self.width do
- buffer[x] = {}
- buffer.changes[x] = {}
- for y=1,sh do
- --print("x="..x..", y="..y)
- local pixel = createPixel(x, y, createColor(" ", buffer_details[3], colors.white))
- term.setCursorPos(x, y)
- term.setBackgroundColor(pixel.col.background)
- term.setTextColor(pixel.col.text)
- term.write(pixel.col.char)
- buffer[x][y] = pixel
- buffer.changes[x][y] = false
- end
- end
- buffer.changes = {}
- buffer.changed = {}
- end
- return buffer
- end
- print("making buffer...")
- local buffer = createBuffer()
- print("overriding buffer...")
- --buffer = buffer
- print("setting buffer details...")
- buffer_details[5] = buffer
- if buffer_details[5] == nil then print("NO BUFFER :<") end
- --[[
- GAME CODE;
- ]]
- print("testing size...")
- --local sw,sh = buffer.getSize()
- local data = {
- ["+"] = {
- name = "wall",
- solid = true,
- id = 2,
- grip = false,
- col = {colors.brown, colors.black, " "},
- finish = false,
- boundry = true,
- checkpoint = false,
- },
- ["|"] = {
- name = "rail",
- solid = true,
- id = 3,
- grip = true,
- col = {colors.brown, colors.white, "|"},
- finish = false,
- boundry = true,
- checkpoint = false,
- },
- [" "] = {
- name = "air",
- solid = false,
- id = 1,
- grip = false,
- col = {colors.white, colors.black, " "},
- finish = false,
- boundry = false,
- checkpoint = false,
- },
- ["~"] = {
- name = "finish",
- solid = false,
- id = 4,
- grip = false,
- col = {colors.white, colors.orange, "@"},
- finish = true,
- boundry = false,
- checkpoint = false,
- },
- ["="] = {
- name = "boundry",
- solid = false,
- id = 5,
- grip = false,
- col = {colors.white, colors.black, "."},
- finish = false,
- boundry = true,
- checkpoint = false,
- },
- ["!"] = {
- name = "checkpoint",
- solid = false,
- id = 6,
- grip = false,
- col = {colors.white, colors.orange, "!"},
- finish = false,
- boundry = true,
- checkpoint = true,
- },
- [":"] = {
- name = "rope",
- solid = false,
- id = 7,
- grip = true,
- col = {colors.white, colors.brown, ":"},
- finish = false,
- boundry = false,
- checkpoint = false,
- },
- ["#"] = {
- name = "gate",
- solid = true,
- id = 8,
- grip = false,
- col = {colors.white, colors.black, "#"},
- finish = false,
- boundry = false,
- checkpoint = false,
- },
- }
- local function createTile(type, x, y)
- if data[type] ~= nil then
- local dat = data[type]
- local tile = {}
- tile.name = dat.name
- tile.solid = dat.solid
- tile.id = dat.id
- tile.grip = dat.grip
- tile.finish = dat.finish
- tile.x = x
- tile.y = y
- tile.col = dat.col
- tile.boundry = dat.boundry
- tile.checkpoint = dat.checkpoint
- tile.block = false
- tile.draw = function(self, ox, oy)
- buffer.setCursorPos(self.x + ox, self.y + oy)
- buffer.setBackgroundColor(self.col[1])
- buffer.setTextColor(self.col[2])
- buffer.write(self.col[3])
- end
- return tile
- elseif type == "@" then
- return 1
- elseif type == ">" then
- return 2
- elseif type == "v" then
- return 3
- elseif type == "^" then
- return 4
- elseif type == "O" then
- return 5
- elseif type == "U" then
- return 6
- elseif type == "-" then
- return 7
- elseif type == "x" then
- return 8
- end
- end
- local function createActor(x, y, states, map)
- local actor = {}
- actor.pos = {x, y}
- actor.state = 1
- actor.states = states
- actor.grip = 0
- actor.map = map
- actor.jump = 0
- actor.dir = 0
- actor.active = true
- actor.drop = false
- actor.lastPos = {x, y}
- actor.god = false
- actor.draw = function(self, ox, oy)
- buffer.setCursorPos(self.pos[1] + ox, self.pos[2] + oy)
- local state = self.states[self.state]
- buffer.setBackgroundColor(state[1])
- buffer.setTextColor(state[2])
- buffer.write(state[3])
- end
- actor.check = function(self, dx, dy, solid, bound)
- local opos = {self.pos[1], self.pos[2]}
- local result = self:move(dx, dy, solid, bound)
- self.pos = {opos[1], opos[2]}
- return result
- end
- actor.move = function(self, dx, dy, solid, bound)
- local x = self.pos[1] + dx
- local y = self.pos[2] + dy
- if y <= self.map.height and y > 0 and x <= self.map.width and x > 0 then
- local tile = self.map[x][y]
- if ((not tile.solid and solid) or (not solid)) and ((not tile.boundry and bound) or (not bound)) then
- self.lastPos = {self.pos[1], self.pos[2]}
- self.pos = {x, y}
- return true
- else
- if not self.god then
- self.jump = 0
- end
- end
- else
- if not self.god then
- self.jump = 0
- end
- end
- return false
- end
- actor.doJump = function(self, amt)
- if not self:isFlying() then self.jump = amt end
- end
- actor.isFlying = function(self)
- if not self.god then
- local pos = self.pos
- local flying = false
- if pos[2] + 1 <= self.map.height then
- local down = self.map[pos[1]][pos[2]+1]
- if (not down.solid and not down.block) and self.grip == 0 then flying = true end
- end
- return flying
- else
- return false
- end
- end
- actor.update = function(self)
- end
- actor.gravity = function(self, canGrip, solid, bound)
- local opos = {self.pos[1], self.pos[2]}
- if not canGrip then self.drop = true end
- local pos = self.pos
- if actor.jump == 0 and not actor.god then
- if pos[2] + 1 <= self.map.height then
- local down = self.map[pos[1]][pos[2]+1]
- if ((not down.solid and not down.block and solid) or (not solid)) and ((not down.boundry and bound) or (not bound)) then
- local grip = 0
- if pos[1] - 1 > 0 then
- local left = self.map[pos[1]-1][pos[2]]
- if left.grip then grip = 1 end
- end
- if grip == 0 then
- if pos[1] + 1 <= self.map.width then
- local right = self.map[pos[1]+1][pos[2]]
- if right.grip then grip = 2 end
- end
- end
- self.grip = grip
- if grip == 0 or self.drop then
- self.pos[2] = self.pos[2] + 1
- end
- end
- end
- elseif self.jump > 0 and not actor.god then
- if pos[2] - 1 > 0 then
- local up = self.map[pos[1]][pos[2]-1]
- if ((not up.solid and not block and solid) or (not solid)) and ((not up.boundry and bound) or (not bound)) then
- self.pos[2] = self.pos[2] - 1
- self.jump = self.jump - 1
- else
- self.jump = 0
- end
- end
- self.grip = 0
- elseif actor.god then
- if self.drop then
- self:move(0, 1, solid, bound)
- elseif self.jump > 0 then
- self:move(0, -1, solid, bound)
- self.jump = 0
- end
- end
- if self.dir ~= 0 then
- if not self:move(self.dir, 0, solid, bound) then
- end
- self.dir = 0
- self.grip = 0
- end
- self.drop = false
- if opos[1] ~= pos[1] or opos[2] ~= pos[2] then
- self.lastPos = opos
- end
- end
- return actor
- end
- local function createActorType(x, y, type, map)
- local tile = nil
- local actor = nil
- if type == 1 then
- tile = createTile(" ", x, y)
- map.player = createActor(x, y, {{colors.white, colors.black, "|"}, {colors.white, colors.black, "\\"}, {colors.white, colors.black, "/"}}, map)
- map.player.update = function(self)
- self:gravity(true, true, false)
- self.state = self.grip + 1
- term.setCursorPos(1, 11)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- print("last pos: "..self.lastPos[1]..","..self.lastPos[2])
- print("cur pos : "..self.pos[1]..","..self.pos[2])
- end
- map.start = {x, y}
- actor = map.player
- elseif type == 2 or type == 3 then
- local id = type
- tile = createTile(" ", x, y)
- actor = createActor(x, y, {{colors.white, colors.black, "x"}, {colors.white, colors.black, "+"}}, map)
- actor.update = function(self)
- if self.id == 2 then
- if not self:move(self.dir, 0, false, true) then self.dir = self.dir * -1 end
- elseif self.id == 3 then
- if not self:move(0, self.dir, false, true) then self.dir = self.dir * -1 end
- end
- if self.state == 1 then self.state = 2 else self.state = 1 end
- if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
- --self.active = false
- self.map.player.active = false
- end
- end
- actor.dir = 1
- actor.id = id
- elseif type == 4 then
- tile = createTile(" ", x, y)
- tile.boundry = true
- actor = createActor(x, y, {{colors.white, colors.red, "^"}}, map)
- actor.update = function(self)
- if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
- self.map.player.active = false
- end
- end
- elseif type == 5 then
- tile = createTile(" ", x, y)
- actor = createActor(x, y, {{colors.white, colors.brown, "O"}}, map)
- actor.update = function(self)
- local opos = {self.pos[1], self.pos[2]}
- self:gravity(false, true, true)
- if opos[1] == self.pos[1] and opos[2] == self.pos[2] then else
- self.map[opos[1]][opos[2]].boundry = false
- self.map[opos[1]][opos[2]].block = false
- end
- self.map[self.pos[1]][self.pos[2]].boundry = true
- self.map[self.pos[1]][self.pos[2]].block = true
- local pos = self.pos
- local ppos = self.map.player.pos
- if pos[1] == ppos[1] and pos[2] == ppos[2] then
- local lpos = self.map.player.lastPos
- if #lpos > 0 then
- if pos[1] - 1 == lpos[1] and pos[2] == lpos[2] then
- local opos = {self.pos[1], self.pos[2]}
- if not self:move(1, 0, true, true) then
- self.map.player.pos[1] = lpos[1]
- self.map.player.pos[2] = lpos[2]
- else
- self.map[self.pos[1]][self.pos[2]].boundry = true
- self.map[self.pos[1]][self.pos[2]].block = true
- self.map[opos[1]][opos[2]].boundry = false
- self.map[opos[1]][opos[2]].block = false
- end
- elseif pos[1] + 1 == lpos[1] and pos[2] == lpos[2] then
- local opos = {self.pos[1], self.pos[2]}
- if not self:move(-1, 0, true, true) then
- self.map.player.pos[1] = lpos[1]
- self.map.player.pos[2] = lpos[2]
- else
- self.map[self.pos[1]][self.pos[2]].boundry = true
- self.map[self.pos[1]][self.pos[2]].block = true
- self.map[opos[1]][opos[2]].boundry = false
- self.map[opos[1]][opos[2]].block = false
- end
- else
- self.map.player.pos[1] = lpos[1]
- self.map.player.pos[2] = lpos[2]
- end
- end
- end
- end
- elseif type == 6 then
- tile = createTile(" ", x, y)
- actor = createActor(x, y, {{colors.white, colors.gray, "U"}, {colors.white, colors.lightGray, "$"}}, map)
- actor.update = function(self)
- local pos = {self.pos[1], self.pos[2] + 1}
- if pos[2] <= self.map.height then
- local tile = self.map[pos[1]][pos[2]]
- while tile ~= nil do
- if tile.block == false and tile.solid == false then
- if self.map.player.pos[1] == tile.x and self.map.player.pos[2] == tile.y then
- self.map.player.active = false
- end
- pos[2] = pos[2] + 1
- if pos[2] <= self.map.height then
- tile = self.map[pos[1]][pos[2]]
- else
- tile = nil
- end
- else
- tile = nil
- end
- end
- end
- end
- actor.draw = function(self, ox, oy)
- buffer.setCursorPos(self.pos[1] + ox, self.pos[2] + oy)
- local state = self.states[self.state]
- buffer.setBackgroundColor(state[1])
- buffer.setTextColor(state[2])
- buffer.write(state[3])
- local pos = {self.pos[1], self.pos[2] + 1}
- if pos[2] <= self.map.height then
- local tile = self.map[pos[1]][pos[2]]
- while tile ~= nil do
- if tile.block == false and tile.solid == false then
- buffer.setCursorPos(pos[1] + ox, pos[2] + oy)
- local state = self.states[2]
- buffer.setBackgroundColor(state[1])
- buffer.setTextColor(state[2])
- buffer.write(state[3])
- pos[2] = pos[2] + 1
- if pos[2] <= self.map.height then
- tile = self.map[pos[1]][pos[2]]
- else
- tile = nil
- end
- else
- tile = nil
- end
- end
- end
- end
- elseif type == 7 then
- tile = createTile(" ", x, y)
- actor = createActor(x, y, {{colors.white, colors.black, "-"}}, map)
- actor.update = function(self)
- local pos = {self.pos[1], self.pos[2]-1}
- if pos[1] == self.map.player.pos[1] and pos[2] == self.map.player.pos[2] then
- self.map.player:move(self.dir, 0, false, true)
- end
- local opos = {self.pos[1], self.pos[2]}
- if not self:move(self.dir, 0, false, true) then self.dir = self.dir * -1
- else
- if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
- self.pos = {opos[1], opos[2]}
- self.dir = self.dir * -1
- else
- self.map[opos[1]][opos[2]].solid = false
- self.map[opos[1]][opos[2]].boundry = false
- end
- end
- self.map[self.pos[1]][self.pos[2]].solid = true
- self.map[self.pos[1]][self.pos[2]].boundry = true
- end
- actor.dir = 1
- elseif type == 8 then
- tile = createTile(" ", x, y)
- actor = createActor(x, y, {{colors.white, colors.black, "x"}, {colors.white, colors.black, "+"}}, map)
- actor.update = function(self)
- local dir = self.dirs[self.curdir]
- local dirx = dir[1]
- local diry = dir[2]
- if not self:move(dirx, diry, false, true) then
- local ndir = self.curdir + 1
- if ndir > #self.dirs then ndir = 1 end
- if not self:check(self.dirs[ndir][1], self.dirs[ndir][2], false, true) then
- ndir = self.curdir - 1
- if ndir < 1 then ndir = #self.dirs end
- end
- self.curdir = ndir
- end
- if self.state == 1 then self.state = 2 else self.state = 1 end
- if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
- --self.active = false
- self.map.player.active = false
- end
- end
- actor.dirs = {{1,0},{0,1},{-1,0},{0,-1}}
- actor.curdir = 1
- end
- return actor, tile
- end
- local function loadMap(maptab)
- local map = {}
- map.width = 0
- map.height = #maptab
- map.player = nil
- map.actors = {}
- map.start = {1,1}
- map.chars = " +|~>v^U=#O-x"
- map.curChar = 1
- for y=1,#maptab do
- local line = maptab[y]
- if map.width == 0 then map.width = string.len(line) end
- for x=1,string.len(line) do
- if map[x] == nil then map[x] = {} end
- local tile = createTile(line:sub(x, x), x, y)
- if type(tile) == "number" then
- local actor, tle = createActorType(x, y, tile, map)
- if actor ~= nil then
- map.actors[#map.actors + 1] = actor
- end
- if tle ~= nil then
- tile = tle
- else
- tile = createTile(" ", x, y)
- end
- end
- map[x][y] = tile
- end
- end
- map.offset = {0,0}
- map.updateOffset = function(self)
- if self.width > sw or self.height > sh then
- local offset = {self.player.pos[1] - math.floor(sw/2), self.player.pos[2] - math.floor(sh/2)}
- if offset[1] < 0 then offset[1] = 0 end
- if offset[2] < 0 then offset[2] = 0 end
- if self.width > sw and offset[1] > self.width-sw then
- offset[1] = self.width-sw
- end
- if self.height > sh and offset[1] > self.height-sh then
- offset[2] = self.height-sh
- end
- self.offset = offset
- else
- end
- end
- map.update = function(self)
- self.player:update()
- local nactors = {}
- for i=1,#self.actors do
- local actor = self.actors[i]
- if actor.active then
- if actor ~= self.player then actor:update() end
- nactors[#nactors + 1] = actor
- end
- end
- self.actors = nactors
- local tile = self[self.player.pos[1]][self.player.pos[2]]
- if tile.checkpoint then self.start = {self.player.pos[1],self.player.pos[2]} end
- self:updateOffset()
- return self.player.active
- end
- map.drawTiles = function(self)
- --print("DRAWING TILES... "..self.width..","..self.height)
- local width = self.width
- local height = self.height
- if width > sw then width = sw end
- if height > sh then height = sh end
- for x=1+map.offset[1],width+map.offset[1] do
- --print("LOOPED X")
- for y=1+map.offset[2],height+map.offset[2] do
- local tile = self[x][y]
- tile:draw(-map.offset[1], -map.offset[2])
- --print("DREW TILE @ "..x..","..y)
- end
- end
- end
- map.drawActors = function(self)
- for i=1,#self.actors do
- local actor = self.actors[i]
- if actor.pos[1] > map.offset[1] and actor.pos[2] > map.offset[2] and actor.pos[1] < map.offset[1] + sw and actor.pos[2] < map.offset[2] + sh then
- actor:draw(-map.offset[1], -map.offset[2])
- end
- end
- self.player:draw(-map.offset[1], -map.offset[2])
- end
- map.draw = function(self)
- self:drawTiles()
- self:drawActors()
- if self.player.god then
- buffer.setCursorPos(1,1)
- buffer.setBackgroundColor(colors.black)
- buffer.setTextColor(colors.white)
- buffer.write(self.chars:sub(self.curChar, self.curChar))
- end
- end
- map.checkFinish = function(self)
- local tile = self[self.player.pos[1]][self.player.pos[2]]
- return tile.finish
- end
- map.place = function(self, x, y)
- local char = self.chars:sub(self.curChar, self.curChar)
- if map[x] ~= nil then
- if map[x][y] ~= nil then
- local tile = createTile(char, x, y)
- if type(tile) == "number" then
- local actor, tle = createActorType(x, y, tile, map)
- if actor ~= nil then
- map.actors[#map.actors + 1] = actor
- end
- if tle ~= nil then
- tile = tle
- else
- tile = createTile(" ", x, y)
- end
- end
- map[x][y] = tile
- end
- end
- end
- return map
- end
- local maps =
- {
- {"+++++++++++++++",
- "+ ~ +",
- "+^ |+| ^+",
- "+++| |+++",
- "+ x +",
- "+ ++ : ++ +",
- "+ O : ! +",
- "| =+++ : +++ |",
- "| @ = |",
- "+++++++++++++++"},
- {"+++++++++++++++",
- "+ = +",
- "+> = +",
- "+| | + | + + ++",
- "+ | > +",
- "+| | + ++ + + +",
- "+ + +",
- "++ |+v+++++ +",
- "+@ +~ +",
- "+++++++++++++++"},
- }
- local function getLines(dir)
- local lines = {}
- if fs.exists(dir) then
- local file = fs.open(dir, "r")
- local line = file.readLine()
- while line ~= nil do
- lines[#lines + 1] = line
- line = file.readLine()
- end
- file.close()
- end
- return lines
- end
- local function loadMaps(dir)
- local lines = getLines(dir)
- if #lines > 0 then
- local maps = {}
- local curmap = {}
- for i=1,#lines do
- local line = lines[i]
- if line == "###" then
- if #curmap > 0 then
- maps[#maps + 1] = curmap
- curmap = {}
- end
- elseif line ~= "" then
- curmap[#curmap + 1] = line
- end
- end
- return maps
- else
- return nil
- end
- end
- local stuff = {...}
- if #stuff > 0 then
- local dir = table.concat(stuff, " ")
- local mps = loadMaps(dir)
- if mps ~= nil then maps = mps end
- end
- local title =
- { " ) ( ) ( ____ ",
- " ( /( )\\ ) ( /( ( )\\ ) | / ",
- " )\\())(()/( )\\()) ( )\\ (()/( | / ",
- "((_)\\ /(_))((_)\\ )\\((((_)( /(_)) | / ",
- " _((_)(_)) _((_) ((_))\\ _ )\\ (_)) |/ ",
- "| \\| ||_ _| | \\| | _ | |(_)_\\(_)/ __| ( ",
- "| .` | | | | .` || || | / _ \\ \\__ \\ )\\ ",
- "|_|\\_||___| |_|\\_| \\__/ /_/ \\_\\ |___/((_) "}
- local start =
- {" _ _ ",
- " ___| |_ __ _ _ __| |_ ",
- "/ __| __/ _` | '__| __| ",
- "\\__ \\ || (_| | | | |_ ",
- "|___/\\__\\__,_|_| \\__| "}
- local load =
- {"_ _ ",
- "| | ___ __ _ __| | ",
- "| |/ _ \\ / _` |/ _` | ",
- "| | (_) | (_| | (_| | ",
- "|_|\\___/ \\__,_|\\__,_| "}
- local function centrePrint(str, y)
- local midx = math.floor(sw/2)
- local x = midx - math.floor(string.len(str)/2)
- term.setCursorPos(x, y)
- term.write(str)
- end
- local function getMaps()
- local mps = {}
- local list = fs.list("")
- for i=1,#list do
- local dir = list[i]
- if dir:sub(-4, -1) == ".nmp" then mps[#mps + 1] = dir end
- end
- return mps
- end
- local function titleScreen()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.clear()
- term.setCursorPos(1,1)
- for i=1,#title do
- centrePrint(title[i], i)
- end
- term.setTextColor(colors.orange)
- for i=1,#start do
- centrePrint(start[i], #title + i )
- end
- for i=1,#load do
- centrePrint(load[i], #title + #start + i )
- end
- local maps = {}
- local pages = 1
- local cur = 1
- local state = 1
- local tsOn = true
- while tsOn do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "key" then
- local key = p1
- if key == 29 then
- tsOn = false
- return nil
- elseif state == 2 then
- if key == 203 then --left
- if cur > 1 then cur = cur - 1 end
- elseif key == 205 then --right
- if cur < pages then cur = cur + 1 end
- elseif key >= 2 and key <= 6 then
- local dir = maps[(5*(cur-1))+(key-1)]
- if dir ~= nil then
- tsOn = false
- return dir
- end
- end
- for i=((cur-1)*5) + 1, ((cur-1)*5) + 6 do
- local j = i - ((cur-1)*5)
- if maps[i] ~= nil then
- centrePrint(" ", #title + #load + j + 1)
- centrePrint(j..": "..maps[i], #title + #load + j + 1)
- end
- end
- end
- elseif event == "mouse_click" then
- if state == 1 then
- local y = p3
- if y > #title and y <= #title + #start then
- tsOn = false
- return true
- elseif y > #title + #start and y <= #title + #start + #load then
- term.clear()
- term.setTextColor(colors.red)
- term.setCursorPos(1,1)
- for i=1,#title do
- centrePrint(title[i], i)
- end
- term.setTextColor(colors.orange)
- for i=1,#load do
- centrePrint(load[i], #title + i)
- end
- maps = getMaps()
- if #maps > 5 then pages = math.floor(#maps/5) end
- for i=((cur-1)*5) + 1, ((cur-1)*5) + 6 do
- local j = i - ((cur-1)*5)
- if maps[i] ~= nil then
- centrePrint(" ", #title + #load + j + 1)
- centrePrint(j..": "..maps[i], #title + #load + j + 1)
- end
- end
- state = 2
- end
- end
- end
- end
- return nil
- end
- local win =
- {" ) ) ) ) ",
- " ( /( ( /( ( ( ( /( ( /( ",
- " )\\()) )\\()) ( )\\))( ' )\\()) )\\()) ",
- " ((_)\\ ((_)\\ )\\ ((_)()\\ ) ((_)\\ ((_)\\ ",
- "__ ((_) ((_) _ ((_) _(())\\_)() ((_) _((_) ",
- "\\ \\ / / / _ \\ | | | | \\ \\((_)/ / / _ \\ | \\| | ",
- " \\ V / | (_) | | |_| | \\ \\/\\/ / | (_) | | .' | ",
- " |_| \\___/ \\___/ \\_/\\_/ \\___/ |_|\\_| "}
- local result = titleScreen()
- if type(result) == "string" then
- maps = loadMaps(result)
- result = true
- end
- if result then
- local curmap = 1
- local map = loadMap(maps[curmap])
- buffer.setBackgroundColor(colors.black)
- buffer.clear()
- buffer.setCursorPos(1,1)
- map:draw()
- local on = true
- local pause = 0.1
- local timer = os.startTimer(pause)
- while on do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "timer" and p1 == timer then
- if not map:update() then
- if not map.player.god then map.player.pos = {map.start[1], map.start[2]} end
- map.player.active = true
- end
- --print("DRAWING..")
- map:draw()
- if map:checkFinish() then
- curmap = curmap + 1
- if curmap > #maps then
- buffer.setBackgroundColor(colors.black)
- buffer.setTextColor(colors.red)
- buffer.clear()
- buffer.setCursorPos(1,1)
- for i=1,#win do
- local line = win[i]
- print(line)
- sleep(0.2)
- end
- for i=1,3 do
- buffer.clear()
- buffer.setCursorPos(1,1)
- sleep(0.25)
- for i=1,#win do
- local line = win[i]
- print(line)
- end
- sleep(0.35)
- end
- on = false
- else
- map = loadMap(maps[curmap])
- end
- end
- buffer:render()
- timer = os.startTimer(pause)
- elseif event == "mouse_scroll" then
- local dir = p1
- if map.player.god then
- map.curChar = map.curChar + dir
- if map.curChar > string.len(map.chars) then map.curChar = 1 end
- if map.curChar < 1 then map.curChar = string.len(map.chars) end
- end
- elseif event == "key" then
- local key = p1
- if key == 17 then --w
- map.player:doJump(2)
- elseif key == 30 then --a
- map.player.dir = -1
- elseif key == 31 then --s
- map.player.jump = 0
- map.player.drop = true
- elseif key == 32 then --d
- map.player.dir = 1
- elseif key == 57 then --space
- map.player:doJump(2)
- elseif key == 19 then --r
- map = loadMap(maps[curmap])
- elseif key == 29 then --ctrl
- on = false
- elseif key == 41 then
- map.player.god = (map.player.god == false)
- elseif key == 200 then --up
- if not map.player.god then map.player:doJump(2) else
- map:place(map.player.pos[1], map.player.pos[2] - 1)
- end
- elseif key == 203 then --left
- if not map.player.god then map.player.dir = -1 else
- map:place(map.player.pos[1] - 1, map.player.pos[2])
- end
- elseif key == 208 then --down
- if not map.player.god then
- map.player.jump = 0
- map.player.drop = true
- else
- map:place(map.player.pos[1], map.player.pos[2] + 1)
- end
- elseif key == 205 then --right
- if not map.player.god then map.player.dir = 1 else
- map:place(map.player.pos[1] + 1, map.player.pos[2])
- end
- elseif key == 79 then
- local dir = 1
- if map.player.god then
- map.curChar = map.curChar + dir
- if map.curChar > string.len(map.chars) then map.curChar = 1 end
- if map.curChar < 1 then map.curChar = string.len(map.chars) end
- end
- elseif key == 82 then
- local dir = -1
- if map.player.god then
- map.curChar = map.curChar + dir
- if map.curChar > string.len(map.chars) then map.curChar = 1 end
- if map.curChar < 1 then map.curChar = string.len(map.chars) end
- end
- end
- end
- end
- end
- --buffer = buffer.buffer
Advertisement
Add Comment
Please, Sign In to add comment