Advertisement
MrDoubleA

kindHurtBlock

Dec 8th, 2021 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.20 KB | None | 0 0
  1. --[[
  2.  
  3.     kindHurtBlock.lua
  4.     by MrDoubleA
  5.  
  6. ]]
  7.  
  8. local blockutils = require("blocks/blockutils")
  9.  
  10. local kindHurtBlock = {}
  11.  
  12.  
  13. kindHurtBlock.lavaReduceSize = 12
  14. kindHurtBlock.normalReduceSize = 6
  15.  
  16.  
  17. kindHurtBlock.debug = false
  18.  
  19.  
  20. -- Table of blocks to reduce the size of and in which directions.
  21. kindHurtBlock.directions = {
  22.     --ID     top bottom left right
  23.     --[404]  = {0,  0,  0,  0 },
  24.  
  25.     [371]  = {1,  0,  0,  0 }, -- SMB lava top
  26.  
  27.     [511]  = {1,  0,  0,  0 }, -- SMB2 spikes
  28.  
  29.     [30]   = {1,  0,  0,  0 }, -- SMB3 lava top
  30.     [1268] = {0,  1,  0,  0 }, -- SMB3 lava bottom
  31.     [109]  = {1,  1,  1,  1 }, -- SMB3 muncher
  32.     [598]  = {1,  1,  1,  1 }, -- SMB3 jelectro
  33.     [110]  = {1,  0,  0,  0 }, -- SMB3 spikes top
  34.     [267]  = {0,  0,  0,  1 }, -- SMB3 spikes right
  35.     [268]  = {0,  1,  0,  0 }, -- SMB3 spikes bottom
  36.     [269]  = {0,  0,  1,  0 }, -- SMB3 spikes left
  37.  
  38.     [408]  = {1,  0,  0,  0 }, -- SMW pencil top
  39.     [407]  = {0,  1,  0,  0 }, -- SMW pencil bottom
  40.     [404]  = {1,  0,  0,  0 }, -- SMW castle lava top
  41.     [430]  = {1,  0,  0,  0 }, -- SMW spike top
  42.     [428]  = {0,  0,  0,  1 }, -- SMW spike right
  43.     [431]  = {0,  1,  0,  0 }, -- SMW spike bottom
  44.     [429]  = {0,  0,  1,  0 }, -- SMW spike left
  45.     [741]  = {1,  0,  0,  0 }, -- semisolid spike
  46.     [673]  = {1,  1,  1,  1 }, -- insta-kill block
  47.  
  48.     [466]  = {1,  0,  1,  0 }, -- SMW lava top left
  49.     [459]  = {1,  0,  0,  0 }, -- SMW lava top
  50.     [460]  = {1,  0,  0,  1 }, -- SMW lava top right
  51.     [463]  = {0,  0,  1,  0 }, -- SMW lava left
  52.     [461]  = {0,  0,  0,  1 }, -- SMW lava right
  53.     [465]  = {0,  1,  1,  0 }, -- SMW lava bottom left
  54.     [462]  = {0,  1,  0,  0 }, -- SMW lava bottom
  55.     [464]  = {0,  1,  0,  1 }, -- SMW lava bottom right
  56.     [471]  = {1,  0,  1,  0 }, -- SMW lava corner top left
  57.     [468]  = {1,  0,  0,  1 }, -- SMW lava corner top right
  58.     [470]  = {0,  1,  1,  0 }, -- SMW lava corner bottom left
  59.     [469]  = {0,  1,  0,  1 }, -- SMW lava corner bottom right
  60.     [480]  = {1,  -1, 0,  0 }, -- SMW lava slope top left
  61.     [482]  = {1,  -1, 0,  0 }, -- SMW lava slope top right
  62.     [486]  = {-1, 1,  0,  0 }, -- SMW lava slope bottom left
  63.     [485]  = {-1, 1,  0,  0 }, -- SMW lava slope bottom right
  64.     [481]  = {1,  0,  0,  0 }, -- SMW lava slope top left corner
  65.     [483]  = {1,  0,  0,  0 }, -- SMW lava slope top right corner
  66.     [487]  = {0,  1,  0,  0 }, -- SMW lava slope bottom left corner
  67.     [484]  = {0,  1,  0,  0 }, -- SMW lava slope bottom right corner
  68.     [472]  = {1,  -1, 0,  0 }, -- SMW lava steep slope top left
  69.     [474]  = {1,  -1, 0,  0 }, -- SMW lava steep slope top right
  70.     [476]  = {-1, 1,  0,  0 }, -- SMW lava steep slope bottom left
  71.     [479]  = {-1, 1,  0,  0 }, -- SMW lava steep slope bottom right
  72.     [473]  = {1,  0,  0,  0 }, -- SMW lava steep slope top left corner
  73.     [475]  = {1,  0,  0,  0 }, -- SMW lava steep slope top right corner
  74.     [477]  = {0,  1,  0,  0 }, -- SMW lava steep slope bottom left corner
  75.     [478]  = {0,  1,  0,  0 }, -- SMW lava steep slope bottom right corner
  76. }
  77.  
  78.  
  79. kindHurtBlock.affectedBlocks = {}
  80.  
  81.  
  82. local function getReduceSides(v,directions)
  83.     local reduceSize
  84.  
  85.     if Block.LAVA_MAP[v.id] then
  86.         reduceSize = kindHurtBlock.lavaReduceSize
  87.     else
  88.         reduceSize = kindHurtBlock.normalReduceSize
  89.     end
  90.  
  91.     --     top                      bottom                   left                     right
  92.     return directions[1]*reduceSize,directions[2]*reduceSize,directions[3]*reduceSize,directions[4]*reduceSize
  93. end
  94.  
  95.  
  96. local function checkBlocks(changeSize)
  97.     kindHurtBlock.affectedBlocks = {}
  98.  
  99.     for _,v in Block.iterate() do
  100.         local directions = kindHurtBlock.directions[v.id]
  101.  
  102.         if directions ~= nil then
  103.             if changeSize then
  104.                 local top,bottom,left,right = getReduceSides(v,directions)
  105.  
  106.                 v.x = v.x + left
  107.                 v.y = v.y + top
  108.                 v.width = v.width - left - right
  109.                 v.height = v.height - top - bottom
  110.             end
  111.  
  112.             table.insert(kindHurtBlock.affectedBlocks,v)
  113.         end
  114.     end
  115. end
  116.  
  117.  
  118. function kindHurtBlock.onStart()
  119.     checkBlocks(true)
  120. end
  121.  
  122. function kindHurtBlock.onReset(fromRespawn)
  123.     checkBlocks(false)
  124. end
  125.  
  126.  
  127. local hiddenBlocks = {}
  128. local hiddenBlockMap = {}
  129.  
  130.  
  131. local function getBlockPriority(v)
  132.     if Block.LAVA_MAP[v.id] then
  133.         return -10
  134.     elseif Block.SIZEABLE_MAP[v.id] then
  135.         return -90
  136.     else
  137.         return -65
  138.     end
  139. end
  140.  
  141. local function drawBlock(v,directions,cx1,cy1,cx2,cy2)
  142.     local top,bottom,left,right = getReduceSides(v,directions)
  143.  
  144.     -- culling stuff
  145.     local x = v.x - left
  146.     if x > cx2 then
  147.         return
  148.     end
  149.  
  150.     local y = v.y - top
  151.     if y > cy2 then
  152.         return
  153.     end
  154.  
  155.     local width = v.width + left + right
  156.     if x+width < cx1 then
  157.         return
  158.     end
  159.  
  160.     local height = v.height + top + bottom
  161.     if y+height < cy1 then
  162.         return
  163.     end
  164.  
  165.     -- Invisiblity check + hide if necessary
  166.     if not hiddenBlockMap[v] then
  167.         if v.isHidden or v:mem(0x5A,FIELD_BOOL) then
  168.             return
  169.         end
  170.  
  171.         v.isHidden = true
  172.  
  173.         table.insert(hiddenBlocks,v)
  174.         hiddenBlockMap[v] = true
  175.     end
  176.  
  177.     -- Drawing time!
  178.     local image = Graphics.sprites.block[v.id].img
  179.     local config = Block.config[v.id]
  180.  
  181.     if image == nil or config == nil then
  182.         return
  183.     end
  184.  
  185.     local priority = getBlockPriority(v) + math.clamp((v.x + 400000)/600000)*0.05
  186.  
  187.     local sourceY = blockutils.getBlockFrame(v.id)*config.height
  188.  
  189.     Graphics.drawImageToSceneWP(image,x,y,0,sourceY,width,height,priority)
  190.  
  191.     if kindHurtBlock.debug then
  192.         Graphics.drawBox{color = Color.blue.. 0.5,x = v.x,y = v.y,width = v.width,height = v.height,priority = -1,sceneCoords = true}
  193.     end
  194. end
  195.  
  196.  
  197. function kindHurtBlock.onCameraDraw(camIdx)
  198.     local c = Camera(camIdx)
  199.  
  200.     local cx1 = c.x
  201.     local cy1 = c.y
  202.     local cx2 = cx1 + c.width
  203.     local cy2 = cy1 + c.height
  204.  
  205.     local i = 1
  206.  
  207.     while (true) do
  208.         local v = kindHurtBlock.affectedBlocks[i]
  209.         if v == nil then
  210.             break
  211.         end
  212.  
  213.         local remove = false
  214.  
  215.         if v.isValid then
  216.             local directions = kindHurtBlock.directions[v.id]
  217.  
  218.             if directions ~= nil then
  219.                 drawBlock(v,directions,cx1,cy1,cx2,cy2)
  220.             else
  221.                 remove = true
  222.             end
  223.         else
  224.             remove = true
  225.         end
  226.  
  227.         if remove then
  228.             table.remove(kindHurtBlock.affectedBlocks,i)
  229.         else
  230.             i = i + 1
  231.         end
  232.     end
  233. end
  234.  
  235.  
  236. function kindHurtBlock.onDrawEnd()
  237.     -- Undo hidden blocks now that drawing's done
  238.     for i = 1,#hiddenBlocks do
  239.         local v = hiddenBlocks[i]
  240.  
  241.         if v.isValid then
  242.             v.isHidden = false
  243.         end
  244.  
  245.         hiddenBlockMap[v] = nil
  246.         hiddenBlocks[i] = nil
  247.     end
  248. end
  249.  
  250.  
  251. function kindHurtBlock.onInitAPI()
  252.     registerEvent(kindHurtBlock,"onStart")
  253.     registerEvent(kindHurtBlock,"onReset")
  254.     registerEvent(kindHurtBlock,"onCameraDraw")
  255.     registerEvent(kindHurtBlock,"onDrawEnd")
  256. end
  257.  
  258.  
  259. return kindHurtBlock
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement