billysback

Pressure

Jul 9th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1.  
  2. local function createBlock(x, y, weight, structure, map)
  3.     local block = {}
  4.     block.parent = structure
  5.     block.weight = weight
  6.     block.pressure = 0
  7.     block.map = map
  8.     block.x = x
  9.     block.y = y
  10.     block.updateWeight = function(self)
  11.         self.pressure = self.parent.pressure
  12.         if self.map[self.x] ~= nil then
  13.             if self.map[self.x][self.y] ~= nil then
  14.                 local block = self.map[self.x][self.y + 1]
  15.                 block:addPressure(block.pressure + self.weigth + self.pressure)
  16.                 blobk.parent:update()
  17.             end
  18.         end
  19.     end
  20.     block.addPressure = function(self, pressure)
  21.         self.parent.pressure = self.parent.pressure + pressure
  22.         self.pressure = self.parent.pressure
  23.     end
  24.     block.reload = function(self)
  25.         local x = self.x
  26.         local y = self.y
  27.         local map = self.map
  28.         if map[x] == nil then map[x] = {} end
  29.         map[x][y] = self
  30.         --print("block @ "..x..","..y)
  31.     end
  32.     block.draw = function(self, ox, oy)
  33.         term.setCursorPos(self.x + ox, self.y + oy)
  34.         term.setBackgroundColor(self.parent.col.back)
  35.         term.setTextColor(self.parent.col.text)
  36.         term.write(self.parent.col.char)
  37.     end
  38.     return block
  39. end
  40.  
  41. local function createStructure(pos, length, dir, map, weight)
  42.     local struct = {}
  43.     for i=1,length do
  44.         local p = {pos[1], pos[2]}
  45.         p[dir] = p[dir] + (i - 1)
  46.         local block = createBlock(p[1], p[2], weight, struct, map)
  47.         struct[i] = block
  48.         --print("Created block "..i)
  49.     end
  50.     struct.length = length
  51.     struct.x = pos[1]
  52.     struct.y = pos[2]
  53.     struct.weight = weight
  54.     struct.map = map
  55.     struct.col = {}
  56.     struct.col.back = colors.white
  57.     struct.col.text = colors.black
  58.     struct.col.char = " "
  59.     struct.pressure = 0
  60.     struct.reset = function(self)
  61.         for i=1,self.length do
  62.             local block = self[i]
  63.             block.weight = self.weight
  64.             block.pressure = 0
  65.         end
  66.         struct.pressure = 0
  67.     end
  68.     struct.update = function(self)
  69.         for i=1,self.length do
  70.             local block = self[i]
  71.             block:updateWeight()
  72.         end
  73.     end
  74.     struct.reload = function(self)
  75.         --print("Reloading..")
  76.         for i=1,self.length do
  77.             local block = self[i]
  78.             block:reload()
  79.         end
  80.     end
  81.     struct:reload()
  82.     return struct
  83. end
  84.  
  85. local function createMap()
  86.     local map = {}
  87.     map.offset = {0,0}
  88.     map.draw = function(self)
  89.         --sleep(4)
  90.         term.setBackgroundColor(colors.black)
  91.         term.clear()
  92.         term.setCursorPos(1,1)
  93.         for x,ytab in pairs(map) do
  94.             --if type(ytab) == "function" then ytab() end
  95.             for y,block in pairs(ytab) do
  96.                 block:draw(map.offset[1], map.offset[2])
  97.             end
  98.         end
  99.     end
  100.     return map
  101. end
  102.  
  103. local length = 3
  104. local map = createMap()
  105. while true do
  106.     local event, button, x, y = os.pullEvent("mouse_click")
  107.     local struct = createStructure({x, y}, 3, button, map, 10)
  108.     map:draw()
  109. end
Advertisement
Add Comment
Please, Sign In to add comment