Guest User

main.lua

a guest
Mar 18th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. StartDebug();
  2. local mod = RegisterMod("Spindown dice preview", 1)
  3. local json = require "json"
  4. local spriteCache = {}
  5.  
  6. ---------
  7. -- UTILITY
  8. ---
  9. function mod:getPreviousAvailableCollectibleSubType( entity, skipAmount )
  10. local subType = entity.SubType;
  11. local itemCfg = nil
  12. local skipCount = skipAmount - 1
  13.  
  14. while itemCfg == nil do
  15. if subType <= 1 then return nil end
  16. subType = subType - 1
  17.  
  18. itemCfg = Isaac.GetItemConfig():GetCollectible( subType )
  19.  
  20. -- If its a valid item, subtract from skip amount
  21. if itemCfg~= nil and itemCfg.Hidden == false then
  22. skipAmount = skipAmount - 1
  23. end
  24.  
  25. -- Dont count hidden items
  26. -- TODO: No way to check for unlocked items
  27. if (itemCfg ~= nil and itemCfg.Hidden == true) or (skipAmount > 0) then
  28. itemCfg = nil
  29. end
  30. end
  31.  
  32. return subType
  33. end
  34.  
  35. function mod:isItem( entity )
  36. return entity ~= nil and entity:ToPickup() ~= nil and entity.Variant == 100
  37. end
  38.  
  39. ------------------
  40. -- MOD CALLBACKS
  41. ----
  42. function mod:onRender(t)
  43. local skipAmount = mod:getAvailableSkipAmount()
  44.  
  45. if skipAmount == 0 then return nil end
  46.  
  47. -- Checking to see if curse of the blind is active
  48. local curseName = Game():GetLevel():GetCurseName()
  49.  
  50. -- Should not be able to display preview if curse of the blind is active
  51. if curseName ~= nil and curseName == "Curse of the Blind!" then return nil end
  52.  
  53. local player = Isaac.GetPlayer( 0 )
  54. local pickups = Isaac.FindInRadius( player.Position, 600, EntityPartition.PICKUP )
  55.  
  56. for i, entity in ipairs( pickups ) do
  57. if mod:isItem( entity ) then
  58. mod:renderCollectible( entity,skipAmount )
  59. end
  60. end
  61. end
  62.  
  63. -----------
  64. -- MOD METHODS
  65. ------
  66.  
  67. function mod:getAvailableSkipAmount()
  68. local playerCount = Game():GetNumPlayers()
  69. local currentSkipAmount = 0
  70.  
  71. for i = 0, playerCount - 1 do
  72. local p = Isaac.GetPlayer(i)
  73. local hasCarBattery = p:HasCollectible( CollectibleType.COLLECTIBLE_CAR_BATTERY )
  74. local skipAmount = 1
  75.  
  76. if p ~= nil and ( p:GetActiveItem ( ActiveSlot.SLOT_PRIMARY ) == 723 or p:GetActiveItem ( ActiveSlot.SLOT_POCKET ) == 723 ) then
  77. if hasCarBattery then
  78. skipAmount = skipAmount + 1
  79. end
  80.  
  81. currentSkipAmount = math.max( currentSkipAmount, skipAmount )
  82. end
  83. end
  84.  
  85. return currentSkipAmount
  86. end
  87.  
  88. function mod:renderCollectible(entity, skipAmount)
  89. local previousCollectibleSubType = mod:getPreviousAvailableCollectibleSubType(entity, skipAmount)
  90.  
  91. if previousCollectibleSubType == nil then return end
  92.  
  93. local itemConfig = Isaac.GetItemConfig():GetCollectible( previousCollectibleSubType )
  94.  
  95. if itemConfig ~= null then
  96. descriptor = itemConfig.GfxFileName
  97.  
  98. local itemSprite
  99.  
  100. -- So we do not have to be constantly creating and reading from spritesheet
  101. if spriteCache[ descriptor ] == nil then
  102. itemSprite = Sprite()
  103. itemSprite:Load("gfx/005.100_Collectible.anm2", true)
  104. itemSprite:ReplaceSpritesheet(1, itemConfig.GfxFileName )
  105. itemSprite:LoadGraphics()
  106. itemSprite.Color = Color(1,1,1,0.45)
  107. itemSprite:SetFrame("Idle", 8)
  108. itemSprite.Scale = Vector(0.4, 0.4)
  109.  
  110. spriteCache[descriptor] = itemSprite
  111. else
  112. itemSprite = spriteCache[descriptor]
  113. end
  114.  
  115. -- Rendering besides item
  116. local adjustedPosition = Vector(0,0)
  117.  
  118. if entity:ToPickup():IsShopItem() then
  119. adjustedPosition = entity.Position - Vector(-22, 20)
  120. else
  121. adjustedPosition = entity.Position - Vector(-22, 38)
  122. end
  123.  
  124. itemSprite:Render( Isaac.WorldToScreen( adjustedPosition ), Vector( 0,0 ), Vector( 0,0 ))
  125. end
  126. end
  127.  
  128. mod:AddCallback( ModCallbacks.MC_POST_RENDER , mod.onRender )
Advertisement
Add Comment
Please, Sign In to add comment