Advertisement
Enjl

customblockanim

Apr 21st, 2019 (edited)
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. --customblockanim.lua by Enjl
  2. --simple block animation helper to make multi-frame blocks easier as per version SMBX2bmx3
  3. --ver 1.0
  4.  
  5. local cba = {}
  6.  
  7. local blocks = {}
  8.  
  9. local stock0 = Graphics.loadImage(Misc.resolveFile("graphics/stock-0.png"))
  10.  
  11. local deferred = {}
  12.  
  13. local firstFrame = true
  14.  
  15. local function registerBlock(id, args)
  16. args = args or {}
  17. if id == nil then
  18. error("ID must be specified when registering new block animation.")
  19. return
  20. end
  21. local image = args.image
  22. if image == nil then
  23. image = Graphics.sprites.block[id].img
  24. Graphics.sprites.block[id].img = stock0
  25. end
  26. local frames = args.frames or 4
  27. local framespeed = args.framespeed or 8
  28. local opacity = args.opacity or 1
  29. local priority = args.priority or -65
  30. local w = image.width
  31. local h = image.height / frames
  32. blocks[id] = {f = frames, fs = framespeed, img = image, w = w, h = h, o = opacity, p = priority}
  33.  
  34. for k,v in ipairs(Block.get(id)) do
  35. v.width = w
  36. v.height = h
  37. end
  38. end
  39.  
  40. -- Call this. ID is mandatory, args are:
  41. ---frames
  42. ---framespeed
  43. ---opacity
  44. ---priority
  45. ---image
  46.  
  47. function cba.register(id, args)
  48. if firstFrame then
  49. table.insert(deferred, {id = id, args = args})
  50. else
  51. registerBlock(id, args)
  52. end
  53. end
  54.  
  55. function cba.onInitAPI()
  56. registerEvent(cba, "onCameraDraw")
  57. registerEvent(cba, "onStart")
  58. end
  59.  
  60. local timer = 0
  61.  
  62. function cba.onStart()
  63. for k,v in ipairs(deferred) do
  64. registerBlock(v.id, v.args)
  65. end
  66. end
  67.  
  68. function cba.onCameraDraw()
  69. timer = timer + 1
  70. for k,v in ipairs(Block.getIntersecting(camera.x, camera.y, camera.x + camera.width, camera.y + camera.height)) do
  71. if blocks[v.id] then
  72. if (not v.isHidden) and v:mem(0x5A, FIELD_WORD) == 0 then
  73. local bv = blocks[v.id]
  74. Graphics.drawImageToSceneWP(bv.img, v.x, v.y + v:mem(0x56, FIELD_WORD), 0, bv.h * (math.floor(timer/bv.fs)%bv.f), bv.w, bv.h, bv.o, bv.p)
  75. end
  76. end
  77. end
  78. end
  79.  
  80. return cba
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement