Guest User

lemegeton item list save fix

a guest
Jun 30th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.52 KB | None | 0 0
  1. local mod = RegisterMod("Lemegeton Item List", 1)
  2. local json = require("json")
  3.  
  4. local renderItems = false
  5. local game = Game()
  6. local row = 0
  7. local updateTheList = false
  8. local listWasUpdated = false
  9. local showedMessage = false
  10. local renderMessage = false
  11. local timer = 0
  12. local alpha = 1
  13.  
  14. local POSITION_LEFT = 1.26
  15. local POSITION_CENTER = 1.6
  16. local POSITION_RIGHT = 2.23
  17.  
  18. local LemegetonItems = {}
  19. local clearedItems = {}
  20.  
  21. local itemSprite = Sprite()
  22. itemSprite:Load("gfx/item.anm2", true)
  23. itemSprite:Play("Idle", true)
  24. for i=1, 24 do
  25.     itemSprite:SetLayerFrame(i, 1)
  26. end
  27.  
  28. local paper = Sprite()
  29. paper:Load("gfx/bg paper.anm2", true)
  30. paper:Play("Idle", true)
  31.  
  32. local setting = {
  33.     Scale = 0.85,
  34.     Position = POSITION_LEFT,
  35.     Opacity = 1
  36. }
  37.  
  38. if ModConfigMenu then
  39.    
  40.     local function AnIndexOf(t, val)
  41.         for k, v in ipairs(t) do
  42.             if v == val then
  43.                 return k
  44.             end
  45.         end
  46.         return 1
  47.     end
  48.    
  49.     local modName = "Lemegeton Item List"
  50.  
  51.     -- avoid duplicating settings
  52.     ModConfigMenu.RemoveCategory(modName)
  53.  
  54.     ModConfigMenu.UpdateCategory(modName, {
  55.         Info = "Lemegeton Item List settings"
  56.     })
  57.     ModConfigMenu.AddTitle(modName, "Settings", " ")
  58.     local paperPositions = {POSITION_LEFT, POSITION_CENTER, POSITION_RIGHT}
  59.     ModConfigMenu.AddSetting(
  60.         modName,
  61.         "Settings",
  62.         {
  63.             Type = ModConfigMenu.OptionType.NUMBER,
  64.             CurrentSetting = function()
  65.                 return AnIndexOf(paperPositions, setting.Position)
  66.             end,
  67.             Minimum = 1,
  68.             Maximum = #paperPositions,
  69.             Display = function()
  70.                 local pos
  71.                 if setting.Position == POSITION_LEFT then
  72.                     pos = "Left"
  73.                 elseif setting.Position == POSITION_CENTER then
  74.                     pos = "Center"
  75.                 elseif setting.Position == POSITION_RIGHT then
  76.                     pos = "Right"
  77.                 end
  78.                 return "List position: " .. pos
  79.             end,
  80.             OnChange = function(currentNum)
  81.                 setting.Position = paperPositions[currentNum]
  82.                 mod:SaveData(json.encode(setting))
  83.             end,
  84.             Info = {"Change the position of the item list (choose between left, center and right)"}
  85.         }
  86.     )
  87.     --[[ModConfigMenu.AddSetting(modName, "Settings", {
  88.         Type = ModConfigMenu.OptionType.KEYBIND_KEYBOARD,
  89.         CurrentSetting = keybind,
  90.         Default = Keyboard.KEY_G,
  91.         Display = "Not working yet",
  92.         --Display = "Open List: " .. keybind,
  93.         OnChange = function(key)
  94.             keybind = key
  95.         end,
  96.         Info = "Press " .. keybind .. " to open a list of items acquired with Lemegeton."
  97.     })--]]
  98.     local scalesArray = {0.75, 0.8, 0.85, 0.9, 0.95, 1}
  99.     ModConfigMenu.AddSetting(
  100.         modName,
  101.         "Settings",
  102.         {
  103.             Type = ModConfigMenu.OptionType.NUMBER,
  104.             CurrentSetting = function()
  105.                 return AnIndexOf(scalesArray, setting.Scale)
  106.             end,
  107.             Minimum = 1,
  108.             Maximum = 6,
  109.             Display = function()
  110.                 return "Size: " .. setting.Scale
  111.             end,
  112.             OnChange = function(currentNum)
  113.                 setting.Scale = scalesArray[currentNum]
  114.                 mod:SaveData(json.encode(setting))
  115.             end,
  116.             Info = {"Change the size of the Lemegeton items list"}
  117.         }
  118.     )
  119.     local opacityArray = {0, 1, 2}
  120.     ModConfigMenu.AddSetting(
  121.         modName,
  122.         "Settings",
  123.         {
  124.             Type = ModConfigMenu.OptionType.NUMBER,
  125.             CurrentSetting = function()
  126.                 return AnIndexOf(opacityArray, setting.Opacity)
  127.             end,
  128.             Minimum = 1,
  129.             Maximum = 3,
  130.             Display = function()
  131.                 local opacity
  132.                 if setting.Opacity == 0 then
  133.                     opacity = "Visible"
  134.                 elseif setting.Opacity == 1 then
  135.                     opacity = "Semi visible"
  136.                 elseif setting.Opacity == 2 then
  137.                     opacity = "Barely visible"
  138.                 end
  139.                 return "Opacity: " .. opacity
  140.             end,
  141.             OnChange = function(currentNum)
  142.                 setting.Opacity = opacityArray[currentNum]
  143.                 mod:SaveData(json.encode(setting))
  144.             end,
  145.             Info = {"Change the opacity of the Lemegeton items list"}
  146.         }
  147.     )
  148. end
  149.  
  150. local function GetScreenSize() --Made by Kilburn
  151.     local room = game:GetRoom()
  152.     local pos = room:WorldToScreenPosition(Vector(0,0)) - room:GetRenderScrollOffset() - game.ScreenShakeOffset
  153.    
  154.     local rx = pos.X + 60 * 26 / 40
  155.     local ry = pos.Y + 140 * (26 / 40)
  156.    
  157.     return rx*2 + 13*26, ry*2 + 7*26
  158. end
  159.  
  160. local function updateList()
  161.     clearedItems = {}
  162.     for i=1, #LemegetonItems do
  163.         if LemegetonItems[i] ~= nil then
  164.             local pos = (#clearedItems % 24) + 1
  165.             clearedItems[pos] = LemegetonItems[i]
  166.         end
  167.     end
  168. end
  169.  
  170. local function updateLemegetonList()
  171.     local wisps = Isaac.FindByType(EntityType.ENTITY_FAMILIAR, FamiliarVariant.ITEM_WISP)
  172.     for i=1, #wisps do
  173.         if wisps[i].FrameCount == 0 then
  174.             local wispItem = wisps[i].SubType
  175.             LemegetonItems[#LemegetonItems + 1] = wispItem
  176.             updateList()
  177.         end
  178.     end
  179. end
  180.  
  181. function mod:gameStart(fromSave)
  182.     if (mod:HasData()) then
  183.         setting = json.decode(mod:LoadData())
  184.     end
  185.     if fromSave then
  186.         updateLemegetonList()
  187.     else
  188.         LemegetonItems = {}
  189.     end
  190. end
  191.  
  192. mod:AddCallback(ModCallbacks.MC_POST_GAME_STARTED, mod.gameStart)
  193.  
  194. function mod:newRoom()
  195.     if #LemegetonItems > 0 then
  196.         updateList()
  197.     end
  198. end
  199.  
  200. mod:AddCallback(ModCallbacks.MC_POST_NEW_ROOM, mod.newRoom)
  201.  
  202. local function renderItem() --Render the items
  203.     local sx, sy = GetScreenSize()
  204.     local ypos = sy - sy/1.35
  205.     local xpos = sx - sx/setting.Position
  206.     local itemScale = Vector(setting.Scale, setting.Scale)
  207.     local paperScale = Vector(setting.Scale+0.09, setting.Scale)
  208.     local sprPos = Vector(xpos + 55, ypos + 25)
  209.    
  210.     paper:Render(Vector(xpos + 60, ypos + 30), Vector.Zero, Vector.Zero)
  211.     paper.Scale = paperScale
  212.     paper:SetFrame(setting.Opacity)
  213.    
  214.    
  215.    
  216.     if updateTheList and not listWasUpdated then
  217.         updateList()
  218.         listWasUpdated = true
  219.     end
  220.  
  221.     for index=1, #clearedItems do
  222.         local icon = Isaac.GetItemConfig():GetCollectible(clearedItems[index]).GfxFileName
  223.         itemSprite:ReplaceSpritesheet(index, icon)
  224.         itemSprite:SetLayerFrame(index, 0) --Make the item visible
  225.     end
  226.     if #clearedItems < 24 then
  227.         for i = (#clearedItems+1) , 24 do
  228.             itemSprite:SetLayerFrame(i, 1) --Make the empty item spaces invisible
  229.         end
  230.     end
  231.     itemSprite:LoadGraphics()
  232.     itemSprite.Scale = itemScale
  233.     itemSprite:Render(sprPos, Vector.Zero, Vector.Zero)
  234. end
  235.  
  236. function mod:render()
  237.     if renderItems then
  238.         renderItem()
  239.     end
  240.     if renderMessage and timer ~= 0 then
  241.         local sx, sy = GetScreenSize()
  242.         Isaac.RenderScaledText("Press the map button to see your current wisp items!", sx-sx/1.24, sy-sy/3, 0.98, 0.98, 1, 0, 1, timer/100)
  243.     end
  244. end
  245.  
  246. mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.render)
  247.  
  248. function mod:playerupdate(player)
  249.    
  250.     if timer > 0 then
  251.         timer = timer - 1
  252.     end
  253.    
  254.     if player:HasCollectible(CollectibleType.COLLECTIBLE_LEMEGETON) and not showedMessage then
  255.         renderMessage = true
  256.         timer = 160
  257.         showedMessage = true
  258.     end
  259.     if #LemegetonItems > 0 then
  260.         if Input.IsActionPressed(ButtonAction.ACTION_MAP, player.ControllerIndex) then
  261.             if listWasUpdated then
  262.                 updateTheList = false
  263.             else updateTheList = true end
  264.             renderItems = true
  265.         else
  266.             listWasUpdated = false
  267.             updateTheList = false
  268.             renderItems = false
  269.         end
  270.     end
  271.    
  272. end
  273.  
  274. mod:AddCallback(ModCallbacks.MC_POST_PLAYER_UPDATE, mod.playerupdate)
  275.  
  276. function mod:wispDeath(fam) --Remove the item wisp from the list when it dies
  277.     if fam.Variant == FamiliarVariant.ITEM_WISP then
  278.         for i=1, #LemegetonItems do
  279.             if LemegetonItems[i] == fam.SubType then
  280.                 LemegetonItems[i] = nil
  281.                 updateList()
  282.                 return
  283.             end
  284.         end
  285.     end
  286. end
  287.  
  288. mod:AddCallback(ModCallbacks.MC_POST_ENTITY_REMOVE, mod.wispDeath, EntityType.ENTITY_FAMILIAR)
  289.  
  290. function mod:onLemegeton(_, _, _) --Add the item wisp to the list when it spawns
  291.     updateLemegetonList()
  292. end
  293.  
  294. mod:AddCallback(ModCallbacks.MC_USE_ITEM, mod.onLemegeton, CollectibleType.COLLECTIBLE_LEMEGETON)
  295.  
Advertisement
Add Comment
Please, Sign In to add comment