Advertisement
billysback

world thingy

Jun 25th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.81 KB | None | 0 0
  1. local function cloneTable(table)
  2.     local newTable = {}
  3.     for k,v in pairs(table) do
  4.         newTable[k] = v
  5.     end
  6.     return newTable
  7. end
  8.  
  9. local function genKey(length)
  10.     local str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  11.     local key = ""
  12.     for i=1,length do
  13.         local j = math.random(string.len(str))
  14.         key = key..str:sub(j, j)
  15.     end
  16.     return key
  17. end
  18.  
  19. local function createWorld()
  20.     local world = {}
  21.  
  22.     world.createItemKey = function(parent)
  23.         local items = parent.items
  24.         local i = 1
  25.         local key = genKey(7)
  26.         while i < 1000 and items[key] ~= nil do
  27.             key = genKey(7)
  28.             i = i + 1
  29.         end
  30.         if items[key] == nil then
  31.             return key
  32.         else
  33.             return nil
  34.         end
  35.     end
  36.    
  37.     world.createDefaultWorld = function(parent, width, height)
  38.         local newWorld = createWorld()
  39.         newWorld.screen = parent.createScreen(newWorld, width, height)
  40.         newWorld.items = {}
  41.         return newWorld
  42.     end
  43.    
  44.     world.createScreen = function(parent, width, height)
  45.         local screen = {}
  46.         screen.parent = parent
  47.         screen.x = 0
  48.         screen.y = 0
  49.         screen.width = 0
  50.         screen.height = 0
  51.        
  52.         screen.draw = function(self)
  53.             local items = { {}, {}, {}, {}, {} }
  54.             local curitems = cloneTable(self.parent.items)
  55.             for x=self.x, self.x+self.width do
  56.                 for y=self.y, self.y+self.height do
  57.                     local nitems = {}
  58.                     for name,item in pairs(curitems) do
  59.                         if item.check(x, y) then
  60.                             items[item.priority][#items + 1] = item
  61.                         else
  62.                             nitems[name] = item
  63.                         end
  64.                     end
  65.                     curitems = nitems
  66.                 end
  67.             end
  68.             for i=#items,1,-1 do
  69.                 local tab = items[i]
  70.                 for j=1,#tab do
  71.                     local item = tab[j]
  72.                     item:draw()
  73.                 end
  74.             end
  75.         end
  76.         return screen
  77.     end
  78.    
  79.     world.items = {}
  80.     world.createItem = function(parent, x, y, name, typ)
  81.         local item = {}
  82.         item.parent = parent
  83.         item.priority = 1
  84.         item.position = {}
  85.         item.position.x = x
  86.         item.position.y = y
  87.         item.position.width = 1
  88.         item.position.height = 1
  89.         iterm.info = {}
  90.         item.info.name = name
  91.         item.info.type = typ
  92.        
  93.         item.check = function(self, x, y)
  94.             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
  95.                 return true
  96.             end
  97.             return false
  98.         end
  99.        
  100.         item.setGraphic = function(self, graphic)
  101.             item.graphic = graphic
  102.             item.position.width = graphic.image.width
  103.             item.position.height = graphic.image.height
  104.         end
  105.        
  106.         item.draw = function(self)
  107.             self.graphic:draw(self.position.x, self.position.y)
  108.         end
  109.         return item
  110.     end
  111.     world.createPixel = function(parent, backgroundColour, textColour, char, x, y)
  112.         local pixel = {}
  113.         pixel.parent = parent
  114.         pixel.char = char
  115.         pixel.background = background
  116.         pixel.text = text
  117.         pixel.position = {}
  118.         pixel.position.x = x
  119.         pixle.position.y = y
  120.         pixel.draw = function(self, x, y)
  121.             term.setCursorPos(self.position.x+x, self.position.y+y)
  122.             term.setBackgroundColor(self.background)
  123.             term.setTextColor(self.text)
  124.             term.write(self.text)
  125.         end
  126.         return pixel
  127.     end
  128.     world.createImage = function(parent, imageString, converter)
  129.         local image = {}
  130.         image.parent = parent
  131.         for y=1,#imageString do
  132.             local line = imageString[y]
  133.             for x=1,string.len(line) do
  134.                 local char = line:sub(x, x)
  135.                 if image[x] == nil then image[x] = {} end
  136.                 local pixel = parent.createPixel(converter[char][1], converter[char][2], converter[char][3], x, y)
  137.                 image[x][y] = pixel
  138.             end
  139.             if image.width == nil then image.width = string.len(line) end
  140.         end
  141.         image.height = #imageString
  142.         image.draw = function(self, x, y)
  143.             for x=1,#image do
  144.                 local tab = image[x]
  145.                 if tab ~= nil then
  146.                     for y=1,#tab do
  147.                         local pixel = tab[y]
  148.                         pixel:draw(x, y)
  149.                     end
  150.                 end
  151.             end
  152.         end
  153.         return image
  154.     end
  155.     world.createGraphic = function(parent, images)
  156.         local graphic = {}
  157.         graphic.parent = parent
  158.         graphic.image = images[1]
  159.         graphic.animation = {}
  160.         graphic.animation.images = images
  161.         graphic.animation.frame = 1
  162.         graphic.animation.tick = function(parent)
  163.             parent.animation.frame = parent.animation.frame + 1
  164.             if parent.animation.frame > #parent.animation.images then
  165.                 parent.animation.frame = 1
  166.             end
  167.             parent.image = parent.animation.images[parent.animation.frame]
  168.         end
  169.         graphic.draw = function(self, x, y)
  170.             self.image:draw(x, y)
  171.             self.animation.tick(self)
  172.         end
  173.         graphic.drawRaw = function(self, x, y)
  174.             self.image:draw(x, y)
  175.         end
  176.         return graphic
  177.     end
  178.     world.createItemData = function(name, typ, images, convert)
  179.         local imgs = {}
  180.         for i=1,#images do
  181.             local img = {}
  182.             local imgstr = images[i][1]
  183.             img.string = imgstr
  184.             img.convert = convert
  185.             imags[#imgs + 1] = img
  186.         end
  187.         local itemdat = {}
  188.         itemdat.imgs = imgs
  189.         itemdat.name = name
  190.         itemdat.type = typ
  191.         return itemdat
  192.     end
  193.     --... is in the format {key_char, background_colour, text_colour, text}, {*}, {*} etc.
  194.     world.createConverter = function(parent, ...)
  195.         local converts = {...}
  196.         local converter = {}
  197.         converter.parent = parent
  198.         for i=1,#converts do
  199.             local tab = converts[i]
  200.             converter[tab[1]] = {tab[2],tab[3],tab[4]}
  201.         end
  202.         converter.addConvert = function(self, char, backcol, textcol, text)
  203.             self[char] = {backcol, textcol, text}
  204.         end
  205.     end
  206.    
  207.     --events is a table containing { ["event"] = event_table , ["event_2"] = event_table }
  208.     --  event_table can contain anything but must have a self function in it called "func", so
  209.     --     event_table:func(mode, event, param1, param2, param3) can run, e.g.
  210.     --         event_table.func = function(self, event, param1, param2, param3)
  211.     world.createMode(parent, name, events)
  212.         local mode = {}
  213.         mode.parent = parent
  214.         mode.name = name
  215.         mode.events = events
  216.         mode.callEvent = function(self, event, param1, param2, param3)
  217.             self.events[event]:func(self, event, param1, param2, param3)
  218.         end
  219.     end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement