EeveeEuphoria

scHUD.lua v2

Feb 6th, 2021 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | None | 0 0
  1. -------------------------------
  2. --  Simple Star Coin HUD v2  --
  3. --     by EeveeEuphoria      --
  4. -------------------------------
  5.  
  6. --[[ Usage:
  7.    
  8.     This is meant to be used if the HUD is disabled, such as by mods like ModernStyledHud.lua
  9.     This can also be used if you manually disable the star coin counter.
  10.    
  11.     Place this somewhere near the top in your global episode/local level lunalua file:
  12.     local scHUD = require("scHUD")
  13.    
  14.     After that line, use these to specify the HUD coordinates (based off the top-left pixel):
  15.     scHUD.x = 40
  16.     scHUD.y = 220
  17.     If nothing is provided, they will default to 20 and 200 respectively.
  18.    
  19.     scHUD.levelstable = levelstarcoincounter
  20.     If provided, this table will be used to represent how many star coins are in a specified level above a warp (door/pipe); before a player actually visits it to populate the save data with the actual value.
  21.     Do it like this:
  22.     local levelstarcoincounter = {
  23.     ["cool level.lvl"] = 3,
  24.     ["funky town.lvlx"] = 2,
  25.     ["souper mayro.lvl"] = 3,
  26.     }
  27.     Be sure that it corresponds to the file name of your level in question, down to the file extension! .lvl is not the same as .lvlx!
  28.     Also, whenever the player actually visits the level, it will check the values found in the save data rather than the provided table; this is really just a workaround.
  29.    
  30.             Other options include:
  31.     scHUD.offset = 20
  32.     This will determine how much should be added in-between each star-coin entry. Note that this isn't the space in-between, it's the total length before it starts drawing the next sprite.
  33.     If not provided, it will default to 16.
  34.    
  35.     scHUD.center1 = 372
  36.     scHUD.center2 = 427
  37.     This will center the star coin hud according to two positions given; useful for centering on the item-drop box!
  38.     If this is provided, scHUD.x will be ignored.
  39.     It's recommended to use the left-most value first, with the right-most value second! (i.e. lowest value first, highest value second)
  40.    
  41.     scHUD.active = Graphics.loadImage(Misc.resolveFile("GFX/redcoina.png"))
  42.     scHUD.inactive = Graphics.loadImage(Misc.resolveFile("GFX/redcoini.png"))
  43.     scHUD.unclaimed = Graphics.loadImage(Misc.resolveFile("GFX/blue coin.png"))
  44.     This will change the provided graphics used for the active/inactive (collected/unclaimed/unclaimed) star coins.
  45.     Do note it will be assumed the length of both is the same as the inactive sprite; if they're not the same, it will not center properly!
  46.     Another notre is that if "scHUD.unclaimed" is not declared, it will default to whatever scHUD.inactive is set to instead.
  47.    
  48.     scHUD.scHUD.playery = 60
  49.     This sets the distance above the player the HUD should be if they're in front of a warp that leads to a level with star-coins.
  50.     It will default to 48 if nothing is provided.
  51.    
  52.     scHUD.disablehud = true
  53.     You can use this to disable the HUD portion if you just want to use the HUD used for warps.
  54.     Enabling this will render scHUD.x, scHUD.y, scHUD.levelstable, and scHUD.center1/2 useless.
  55.    
  56.     scHUD.disablewarphud = true
  57.     Use this to disable rendering the coins above the player when near a level warp w/ star coins.
  58.     Use this combined with disablehud to have a useless mod that just wastes CPU cycles!
  59. ]]
  60.  
  61. --[[ Changelog for v2:
  62.     - Now warp doors uses GFX to indicate if the player has collected a coin in a level, but hasn't saved their progress.
  63.     - Code is a bit tidier now!
  64. ]]
  65.  
  66. local scHUD = {}
  67.  
  68. scHUD.active = Graphics.sprites.hardcoded["51-1"].img
  69. scHUD.inactive = Graphics.sprites.hardcoded["51-0"].img
  70. --scHUD.unclaimed = Graphics.loadImage(Misc.resolveFile("GFX/blue coin.png"))
  71.  
  72. function scHUD.onInitAPI()
  73.     registerEvent(scHUD, "onDraw")
  74.     registerEvent(scHUD, "onTick")
  75. end
  76.  
  77. scHUD.x = 20
  78. scHUD.y = 200
  79. scHUD.offset = 16
  80. scHUD.playery = 48
  81.  
  82. local function scDDT(value, playerhud)
  83.     if not playerhud then
  84.         if value == 0 then
  85.             return scHUD.inactive
  86.         elseif value == 1 then
  87.             return scHUD.active
  88.         elseif value == 2 and scHUD.unclaimed then
  89.             return scHUD.unclaimed
  90.         else
  91.             return scHUD.inactive
  92.         end
  93.     else
  94.         if value == 0 then
  95.             return scHUD.inactive
  96.         else
  97.             return scHUD.active
  98.         end
  99.     end
  100. end
  101.  
  102. function scHUD.onDraw()
  103.     local starCoinsT = SaveData._basegame.starcoin[Level.filename()]
  104.     if not scHUD.disablehud and (starCoinsT ~= nil and #starCoinsT ~= 0) then
  105.         for index, value in ipairs(starCoinsT) do
  106.             if scHUD.center1 and scHUD.center2 then
  107.                 local length = scHUD.offset*(#starCoinsT-1) + scHUD.inactive.width
  108.                 local xcen = math.ceil((math.abs(scHUD.center1 - scHUD.center2) - length)*0.5)
  109.                 Graphics.drawImage(scDDT(value, true), scHUD.center1 + xcen + (scHUD.offset*(index-1)), scHUD.y)
  110.             else
  111.                 Graphics.drawImage(scDDT(value, true), scHUD.x + (scHUD.offset*index), scHUD.y)
  112.             end
  113.         end
  114.     end
  115.     if not scHUD.disablewarphud then
  116.         for _,plyr in ipairs(Player.get()) do
  117.             if plyr:mem(0x5A,FIELD_WORD) > 0 then
  118.                 local warp = Warp(plyr:mem(0x5A,FIELD_WORD)-1)
  119.                 if warp.levelFilename ~= "" then
  120.                     local starCoinsT2 = SaveData._basegame.starcoin[warp.levelFilename]
  121.                     if starCoinsT2 ~= nil and #starCoinsT2 ~= 0 then
  122.                         for index, value in ipairs(starCoinsT2) do
  123.                             local length = scHUD.offset*(#starCoinsT2-1) + scHUD.inactive.width
  124.                             local xcen = math.floor((plyr.width - length)*0.5)
  125.                             Graphics.drawImageToScene(scDDT(value), plyr.x + xcen + scHUD.offset*(index-1), plyr.y - scHUD.playery)
  126.                             --Text.print(value, 20, 220 + (20*index))
  127.                         end
  128.                     elseif type(scHUD.levelstable) == "table" and scHUD.levelstable[warp.levelFilename] then
  129.                         local length = scHUD.offset*(scHUD.levelstable[warp.levelFilename]-1) + scHUD.inactive.width
  130.                         local xcen = math.floor((plyr.width - length)*0.5)
  131.                         for i=scHUD.levelstable[warp.levelFilename],1,-1 do
  132.                             Graphics.drawImageToScene(scHUD.inactive, plyr.x + xcen + scHUD.offset*(i-1), plyr.y - scHUD.playery)
  133.                         end
  134.                     end
  135.                 end
  136.             end
  137.         end
  138.     end
  139. end
  140.  
  141. return scHUD
Advertisement
Add Comment
Please, Sign In to add comment