Advertisement
wolfboyft

God-awful code

Oct 4th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.60 KB | None | 0 0
  1. -- what's on screen is sent by other systems to this sytem, then what's done is acquired by other systems from this system
  2. -- eg: inventory gets what's on the ground that can be picked up, sends that to ui, ui handles any inputs, wills takes those inputs and makes them into actions and then inventory takes those actions and makes them work
  3.  
  4. local concord = require("lib.concord")
  5. local ui = concord.system({camera = {"camera", "inventory"}})
  6.  
  7. function ui:update(dt)
  8.     local camera = self.camera[1]
  9.    
  10.     self.inventoryGrids = {}
  11.    
  12.     if camera then
  13.         local cameraInventoryItems = {}
  14.         for i, v in ipairs(camera.inventory.items) do
  15.             cameraInventoryItems[i] = v
  16.         end
  17.         self.inventoryGrids.cameraInventory = {128, 20, 10, 10, cameraInventoryItems, camera.inventory.capacity}
  18.     end
  19.     if self.cameraPickupables then
  20.         self.inventoryGrids.cameraPickupables = {20, 20, 10, 10, self.cameraPickupables, #self.cameraPickupables}
  21.     end
  22.    
  23.     if not camera or not (camera and camera.inventory.isOpen) then
  24.         self.currentInventoryGrid, self.currentInventoryGridSelector, self.selectedItem, self.selectedItemPos = nil
  25.     end
  26.     if self.changeGrid or not self.currentInventoryGrid then
  27.         if camera and camera.inventory.isOpen then
  28.             if not self.currentInventoryGridSelector then
  29.                 self.currentInventoryGridSelector = self.inventoryGrids.cameraInventory and "main"
  30.             elseif self.currentInventoryGridSelector == "main" then
  31.                 if self.inventoryGrids.cameraPickupables then
  32.                     self.currentInventoryGridSelector = "pickupables"
  33.                     self.selectedItem, self.selectedItemPos = nil
  34.                 end
  35.             elseif self.currentInventoryGridSelector == "pickupables" then
  36.                 if self.inventoryGrids.cameraInventory then
  37.                     self.currentInventoryGridSelector = "main"
  38.                     self.selectedItem, self.selectedItemPos = nil
  39.                 end
  40.             end
  41.         end
  42.     end
  43.    
  44.     if self.currentInventoryGridSelector == "main" then
  45.         self.currentInventoryGrid = self.inventoryGrids.cameraInventory
  46.     elseif self.currentInventoryGridSelector == "pickupables" then
  47.         self.currentInventoryGrid = self.inventoryGrids.cameraPickupables
  48.     else
  49.         self.currentInventoryGrid = nil
  50.     end
  51.    
  52.     if self.currentInventoryGrid then
  53.         if #self.currentInventoryGrid[5] == 0 then
  54.             self.selectedItemPos = nil
  55.             self.selectedItem = nil
  56.         else
  57.             if self.selectedItem then
  58.                 self.selectedItemPos = nil
  59.                 for i, v in ipairs(self.currentInventoryGrid[5]) do
  60.                     if v == self.selectedItem then
  61.                         self.selectedItemPos = i
  62.                     end
  63.                 end
  64.                 if not self.selectedItemPos then
  65.                     self.selectedItem = nil
  66.                 end
  67.             end
  68.             if not self.selectedItemPos then
  69.                 -- if self.selectorMove then
  70.                     self.selectedItemPos = 1
  71.                 -- end
  72.             elseif self.selectorMove == "up" then
  73.                 self.selectedItemPos = math.max(self.selectedItemPos - self.currentInventoryGrid[1], 1)
  74.             elseif self.selectorMove == "down" then
  75.                 self.selectedItemPos = math.min(self.selectedItemPos + self.currentInventoryGrid[1], #self.currentInventoryGrid[5])
  76.             elseif self.selectorMove == "left" then
  77.                 self.selectedItemPos = math.max(self.selectedItemPos - 1, 1)
  78.             elseif self.selectorMove == "right" then
  79.                 self.selectedItemPos = math.min(self.selectedItemPos + 1, #self.currentInventoryGrid[5])
  80.             end
  81.             if self.selectedItemPos then
  82.                 self.selectedItem = self.currentInventoryGrid[5][self.selectedItemPos]
  83.             end
  84.         end
  85.     end
  86.    
  87.     if self.select and self.selectedItem then
  88.         local moved = false
  89.         if self.currentInventoryGridSelector == "pickupables" then
  90.             if self:getWorld().inventory:giveItem(camera, self.selectedItem) then
  91.                 self:getWorld():removeEntity(self.selectedItem)
  92.                 moved = true
  93.             end
  94.         elseif self.currentInventoryGridSelector == "main" then
  95.             if camera.position then
  96.                 local item = self:getWorld().inventory:takeItem(camera, self.selectedItem)
  97.                 self:getWorld().map:addItemToWorld(camera.position.x, camera.position.y, item)
  98.                 moved = true
  99.             end
  100.         end
  101.         if moved then
  102.             table.remove(self.currentInventoryGrid[5], self.selectedItemPos)
  103.             self.selectedItem.moved = true -- necessary?
  104.             if #self.currentInventoryGrid[5] > 0 then
  105.                 self.selectedItemPos = math.min(self.selectedItemPos, #self.currentInventoryGrid[5])
  106.                 self.selectedItem = self.currentInventoryGrid[5][self.selectedItemPos]
  107.             else
  108.                 self.selectedItem = nil
  109.                 self.selectedItemPos = nil
  110.             end
  111.         end
  112.     end
  113.    
  114.     if self.selectedItemPos then
  115.         self.currentInventoryGrid[7] = self.selectedItemPos
  116.     end
  117.    
  118.     for _, v in pairs(self.inventoryGrids) do
  119.         v[8] = v == self.currentInventoryGrid
  120.     end
  121.    
  122.     self.cameraPickupables = nil
  123.     self.changeGrid = nil
  124.     self.selectorMove = nil
  125.     self.select = nil
  126. end
  127.  
  128. return ui
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement