Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function cloneTable(table)
- local newTable = {}
- for k,v in pairs(table) do
- newTable[k] = v
- end
- return newTable
- end
- local function genKey(length)
- local str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- local key = ""
- for i=1,length do
- local j = math.random(string.len(str))
- key = key..str:sub(j, j)
- end
- return key
- end
- local function createWorld()
- local world = {}
- world.createItemKey = function(parent)
- local items = parent.items
- local i = 1
- local key = genKey(7)
- while i < 1000 and items[key] ~= nil do
- key = genKey(7)
- i = i + 1
- end
- if items[key] == nil then
- return key
- else
- return nil
- end
- end
- world.createDefaultWorld = function(parent, width, height)
- local newWorld = createWorld()
- newWorld.screen = parent.createScreen(newWorld, width, height)
- newWorld.items = {}
- return newWorld
- end
- world.createScreen = function(parent, width, height)
- local screen = {}
- screen.parent = parent
- screen.x = 0
- screen.y = 0
- screen.width = 0
- screen.height = 0
- screen.draw = function(self)
- local items = { {}, {}, {}, {}, {} }
- local curitems = cloneTable(self.parent.items)
- for x=self.x, self.x+self.width do
- for y=self.y, self.y+self.height do
- local nitems = {}
- for name,item in pairs(curitems) do
- if item.check(x, y) then
- items[item.priority][#items + 1] = item
- else
- nitems[name] = item
- end
- end
- curitems = nitems
- end
- end
- for i=#items,1,-1 do
- local tab = items[i]
- for j=1,#tab do
- local item = tab[j]
- item:draw()
- end
- end
- end
- return screen
- end
- world.items = {}
- world.createItem = function(parent, x, y, name, typ)
- local item = {}
- item.parent = parent
- item.priority = 1
- item.position = {}
- item.position.x = x
- item.position.y = y
- item.position.width = 1
- item.position.height = 1
- iterm.info = {}
- item.info.name = name
- item.info.type = typ
- item.check = function(self, x, y)
- if x >= item.position.x and y >= item.position.y and x < item.position.x+item.position.width and y < item.position.y+item.position.height then
- return true
- end
- return false
- end
- item.setGraphic = function(self, graphic)
- item.graphic = graphic
- item.position.width = graphic.image.width
- item.position.height = graphic.image.height
- end
- item.draw = function(self)
- self.graphic:draw(self.position.x, self.position.y)
- end
- return item
- end
- world.createPixel = function(parent, backgroundColour, textColour, char, x, y)
- local pixel = {}
- pixel.parent = parent
- pixel.char = char
- pixel.background = background
- pixel.text = text
- pixel.position = {}
- pixel.position.x = x
- pixle.position.y = y
- pixel.draw = function(self, x, y)
- term.setCursorPos(self.position.x+x, self.position.y+y)
- term.setBackgroundColor(self.background)
- term.setTextColor(self.text)
- term.write(self.text)
- end
- return pixel
- end
- world.createImage = function(parent, imageString, converter)
- local image = {}
- image.parent = parent
- for y=1,#imageString do
- local line = imageString[y]
- for x=1,string.len(line) do
- local char = line:sub(x, x)
- if image[x] == nil then image[x] = {} end
- local pixel = parent.createPixel(converter[char][1], converter[char][2], converter[char][3], x, y)
- image[x][y] = pixel
- end
- if image.width == nil then image.width = string.len(line) end
- end
- image.height = #imageString
- image.draw = function(self, x, y)
- for x=1,#image do
- local tab = image[x]
- if tab ~= nil then
- for y=1,#tab do
- local pixel = tab[y]
- pixel:draw(x, y)
- end
- end
- end
- end
- return image
- end
- world.createGraphic = function(parent, images)
- local graphic = {}
- graphic.parent = parent
- graphic.image = images[1]
- graphic.animation = {}
- graphic.animation.images = images
- graphic.animation.frame = 1
- graphic.animation.tick = function(parent)
- parent.animation.frame = parent.animation.frame + 1
- if parent.animation.frame > #parent.animation.images then
- parent.animation.frame = 1
- end
- parent.image = parent.animation.images[parent.animation.frame]
- end
- graphic.draw = function(self, x, y)
- self.image:draw(x, y)
- self.animation.tick(self)
- end
- graphic.drawRaw = function(self, x, y)
- self.image:draw(x, y)
- end
- return graphic
- end
- world.createItemData = function(name, typ, images, convert)
- local imgs = {}
- for i=1,#images do
- local img = {}
- local imgstr = images[i][1]
- img.string = imgstr
- img.convert = convert
- imags[#imgs + 1] = img
- end
- local itemdat = {}
- itemdat.imgs = imgs
- itemdat.name = name
- itemdat.type = typ
- return itemdat
- end
- --... is in the format {key_char, background_colour, text_colour, text}, {*}, {*} etc.
- world.createConverter = function(parent, ...)
- local converts = {...}
- local converter = {}
- converter.parent = parent
- for i=1,#converts do
- local tab = converts[i]
- converter[tab[1]] = {tab[2],tab[3],tab[4]}
- end
- converter.addConvert = function(self, char, backcol, textcol, text)
- self[char] = {backcol, textcol, text}
- end
- end
- --events is a table containing { ["event"] = event_table , ["event_2"] = event_table }
- -- event_table can contain anything but must have a self function in it called "func", so
- -- event_table:func(mode, event, param1, param2, param3) can run, e.g.
- -- event_table.func = function(self, event, param1, param2, param3)
- world.createMode(parent, name, events)
- local mode = {}
- mode.parent = parent
- mode.name = name
- mode.events = events
- mode.callEvent = function(self, event, param1, param2, param3)
- self.events[event]:func(self, event, param1, param2, param3)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement