Advertisement
Enjl

LightHitPoint

Jul 27th, 2019 (edited)
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. local lhp = {}
  2.  
  3. lhp.iFrames = 25
  4.  
  5. local harmtypedamage = {
  6.  
  7. }
  8.  
  9. local npcdamage = {
  10.  
  11. }
  12.  
  13. local npchp = {
  14.  
  15. }
  16.  
  17. local harmtypesound = {
  18.  
  19. }
  20.  
  21. local callbacks = {}
  22.  
  23. function lhp.registerCallback(NPCID, func)
  24.     callbacks[NPCID] = func
  25. end
  26.  
  27. function lhp.setHarmtypeDamage(HARMTYPE, value)
  28.     harmtypedamage[HARMTYPE] = value
  29. end
  30.  
  31. function lhp.setHarmtypeSound(HARMTYPE, value)
  32.     harmtypesound[HARMTYPE] = value
  33. end
  34.  
  35. function lhp.setNPCDamage(NPCID, value)
  36.     npcdamage[NPCID] = value
  37. end
  38.  
  39. function lhp.setHP(NPCID, value)
  40.     npchp[NPCID] = value
  41. end
  42.  
  43. function lhp.onInitAPI()
  44.     registerEvent(lhp, "onNPCHarm")
  45.     registerEvent(lhp, "onCameraDraw")
  46. end
  47.  
  48. function lhp.onNPCHarm(e, v, r, c)
  49.     if (r ~= HARM_TYPE_OFFSCREEN) and npchp[v.id] then
  50.         if v.data.lhphp == nil then
  51.             v.data.lhphp = npchp[v.id]
  52.         end
  53.         local damage = 1
  54.         damage = damage * (harmtypedamage[r] or 1)
  55.         if c and c.__type == "NPC" then
  56.             damage = damage * (npcdamage[c.id] or 1)
  57.         end
  58.  
  59.         if callbacks[v.id] then
  60.             local a = callbacks[v.id](v, r, c, v.data.lhphp, damage)
  61.             if a then
  62.                 damage = a
  63.             end
  64.         end
  65.  
  66.         if v.data.iFrames and v.data.iFrames > lunatime.tick() - lhp.iFrames then
  67.             damage = 0
  68.         else
  69.             v.data.iFrames = lunatime.tick()
  70.         end
  71.         v.data.lhphp = math.max(v.data.lhphp - damage, 0)
  72.  
  73.         if damage ~= 0 and v.data.lhphp > 0 then
  74.             if harmtypesound[r] then
  75.                 SFX.play(harmtypesound[r])
  76.             end
  77.         end
  78.         if v.data.lhphp > 0 then
  79.             e.cancelled = true
  80.         end
  81.     end
  82. end
  83.  
  84. function lhp.drawHPBar(v)
  85.     local x = v.x + 0.5 * v.width
  86.     local y = v.y - 10
  87.  
  88.     local width = 10 + 5 * math.ceil((npchp[v.id] or 1) * 0.2)
  89.    
  90.     Graphics.drawBox{
  91.         x = x - 0.5 * width,
  92.         width = width,
  93.         y = y - 2,
  94.         height = 4,
  95.         color = Color.grey,
  96.         sceneCoords = true,
  97.         priority = -45
  98.     }
  99.  
  100.     Graphics.drawBox{
  101.         x = x - 0.5 * width,
  102.         width = width * (v.data.lhphp / (npchp[v.id] or 1)),
  103.         y = y - 2,
  104.         height = 4,
  105.         color = Color.yellow,
  106.         sceneCoords = true,
  107.         priority = -45
  108.     }
  109. end
  110.  
  111. function lhp.onCameraDraw()
  112.     for k,v in ipairs(NPC.getIntersecting(camera.x, camera.y, camera.x + camera.width, camera.y + camera.height)) do
  113.         if NPC.HITTABLE_MAP[v.id] and v:mem(0x12A, FIELD_WORD) > 0 and not v.friendly then
  114.             if v.data.lhphp then
  115.                 lhp.drawHPBar(v)
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121. return lhp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement