Advertisement
HugoBDesigner

Slots example (written for LÖVE 11.0.0)

May 10th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. --Friendly reminder that this is only an example of code that should be read and analyzed so that you understand the core concepts behind it
  2. --Pasting it on top of your code will not fix whatever issue you're having :P
  3. function love.load()
  4.     --Decoration
  5.     width = 25
  6.     spacing = 5
  7.    
  8.     slots = {} --Where your items go
  9.     curSlot = 1 --Selected slot
  10.     numOfSlots = 7 --Total slots size
  11.    
  12.     for i = 1, numOfSlots do
  13.         slots[i] = {inUse = true} --By default, each slot is in use
  14.     end
  15.    
  16.     love.window.setMode((width+spacing)*numOfSlots+spacing, spacing*2+width)
  17. end
  18.  
  19. function love.draw()
  20.     for i, v in ipairs(slots) do
  21.         love.graphics.setColor(.5, .5, .5, 1) --Sets default slot color
  22.         if not v.inUse then --Sets inactive slot color
  23.             love.graphics.setColor(.25, .25, .25, 1)
  24.         end
  25.         if curSlot == i then --Sets highlighted slot color
  26.             love.graphics.setColor(.75, .75, .25, 1)
  27.         end
  28.         love.graphics.rectangle("fill", spacing + (i-1)*(width + spacing), spacing, width, width) --Draw slot
  29.        
  30.         --This is just the make-shift item representation via colored balls
  31.         love.graphics.setColor(.75, .1, 0, 1)
  32.         if v.inUse then
  33.             love.graphics.setColor(.1, .75, 0, 1)
  34.         end
  35.         love.graphics.circle("fill", spacing + (i-1)*(width + spacing)+width/2, spacing+width/2, width/4, 32)
  36.         love.graphics.setLineWidth(2)
  37.         if curSlot == i then
  38.             love.graphics.setColor(.3, 1, 0, 1)
  39.         end
  40.         love.graphics.circle("line", spacing + (i-1)*(width + spacing)+width/2, spacing+width/2, width/4, 32)
  41.        
  42.         if curSlot == i then --Highlight slot
  43.             love.graphics.setColor(.25, .25, 0, 1)
  44.             love.graphics.setLineWidth(5)
  45.             love.graphics.rectangle("line", spacing + (i-1)*(width + spacing), spacing, width, width)
  46.         end
  47.     end
  48. end
  49.  
  50. function love.wheelmoved(x, y)
  51.     if y ~= 0 then
  52.         y = y < 0 and -1 or 1
  53.         local foundSlot = false
  54.        
  55.         for i = 1, numOfSlots do --Ensures you try all items
  56.             if curSlot then
  57.                 curSlot = curSlot + y --Moves the selected slot up or down depending on the mouse wheel movement
  58.             else
  59.                 curSlot = 1 --If no slot was selected, make it start at 1
  60.             end
  61.            
  62.             if curSlot > numOfSlots then --This makes the slot selection wrap around if you reach the end
  63.                 curSlot = 1
  64.             elseif curSlot < 1 then
  65.                 curSlot = numOfSlots
  66.             end
  67.            
  68.             if slots[curSlot].inUse then --It'll go on until it finds a valid slot. Once it's found, leave it selected and exit the loop
  69.                 foundSlot = true
  70.                 break
  71.             end
  72.         end
  73.        
  74.         if not foundSlot then --In case no slot is in use, we don't highlight any of them
  75.             curSlot = false
  76.         end
  77.     end
  78. end
  79.  
  80. function love.keypressed(key)
  81.     local number = false
  82.    
  83.     --This is a cheap way of checking if the key pressed is 1, 2, 3... or kp1, kp2, kp3...
  84.     if string.len(key) == 3 and string.sub(key, 1, 2) == "kp" and tonumber(string.sub(key, -1, -1)) then
  85.         number = tonumber(string.sub(key, -1, -1))
  86.     elseif tonumber(key) then
  87.         number = tonumber(key)
  88.     end
  89.    
  90.     if number and number >= 1 and number <= numOfSlots then
  91.         slots[number].inUse = not slots[number].inUse
  92.        
  93.         if slots[number].inUse == false and curSlot == number then --Guarantees that the selection will be over a usable slot
  94.             love.wheelmoved(0, 1) --Kind of a cheat
  95.         elseif slots[number].inUse == true and curSlot == false then
  96.             curSlot = number
  97.         end
  98.     end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement