Advertisement
Kijan

IA Heroes Bar

Nov 8th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.07 KB | None | 0 0
  1. broadcast = true
  2.  
  3. function onLoad(save_state)
  4.   local loaded = JSON.decode(save_state)
  5.   if loaded ~= nil then
  6.     Wait.frames(function() loadHP(loaded) end, 5)
  7.     broadcast = loaded.broadcast
  8.   end
  9. end
  10.  
  11. function loadHP(hp)
  12.   Global.UI.setAttribute("HP Bars", "height", hp.tableHeight)
  13.   for i=1, 4 do
  14.     if hp["P" .. i] ~= nil then
  15.       Global.UI.setAttribute("P" .. i .. "_Table", "active", hp["P" .. i].table)
  16.       Global.UI.setAttribute("P" .. i .. "_name", "text", hp["P" .. i].name)
  17.       Global.UI.setAttribute("P" .. i .. "_totalhp", "text", hp["P" .. i].hp)
  18.       Global.UI.setAttribute("P" .. i .. "_hp_bar", "percentage", tonumber(hp["P" .. i].hp:match("(%d+)/")) / tonumber(hp["P" .. i].hp:match("/(%d+)")) * 100)
  19.       Global.UI.setAttribute("P" .. i .. "_totalstam", "text", hp["P" .. i].stam)
  20.       Global.UI.setAttribute("P" .. i .. "_stam_bar", "percentage", tonumber(hp["P" .. i].stam:match("(%d+)/")) / tonumber(hp["P" .. i].stam:match("/(%d+)")) * 100)
  21.       Global.UI.setAttribute("F" .. i .. "_Table", "active", hp["F" .. i].table)
  22.       Global.UI.setAttribute("F" .. i .. "_name", "text", hp["F" .. i].name)
  23.       Global.UI.setAttribute("F" .. i .. "_totalhp", "text", hp["F" .. i].hp)
  24.       Global.UI.setAttribute("F" .. i .. "_hp_bar", "percentage", tonumber(hp["F" .. i].hp:match("(%d+)/")) / tonumber(hp["F" .. i].hp:match("/(%d+)")) * 100)
  25.     end
  26.   end
  27. end
  28.  
  29. function onSave()
  30.   local saveState = {
  31.       P1 = {}
  32.       , P2 = {}
  33.       , P3 = {}
  34.       , P4 = {}
  35.       , F1 = {}
  36.       , F2 = {}
  37.       , F3 = {}
  38.       , F4 = {}
  39.       , tableHeight = Global.UI.getAttribute("HP Bars", "height")
  40.       , broadcast = broadcast
  41.   }
  42.   for i=1, 4 do
  43.     saveState["P" .. i].table = Global.UI.getAttribute("P" .. i .. "_Table", "active")
  44.     saveState["P" .. i].name = Global.UI.getAttribute("P" .. i .. "_name", "text")
  45.     saveState["P" .. i].hp = Global.UI.getAttribute("P" .. i .. "_totalhp", "text")
  46.     saveState["P" .. i].stam = Global.UI.getAttribute("P" .. i .. "_totalstam", "text")
  47.     saveState["F" .. i].table = Global.UI.getAttribute("F" .. i .. "_Table", "active")
  48.     saveState["F" .. i].name = Global.UI.getAttribute("F" .. i .. "_name", "text")
  49.     saveState["F" .. i].hp = Global.UI.getAttribute("F" .. i .. "_totalhp", "text")
  50.   end
  51.   saved_data = JSON.encode(saveState)
  52.   self.script_state = saved_data
  53. end
  54.  
  55. function showHPBar(player, value, id)
  56.   if Global.UI.getAttribute("Bars Config", "active") == "false" then
  57.     for i=1, 4 do
  58.       if Global.UI.getAttribute("P" .. i .. "_Table", "active") == "true" or Global.UI.getAttribute("P" .. i .. "_Table", "active") == "True" then
  59.         Global.UI.setAttribute("P" .. i .. "Name", "text", Global.UI.getAttribute("P" .. i .. "_name", "text"))
  60.         Global.UI.setAttribute("P" .. i, "isOn", "true")
  61.         if Global.UI.getAttribute("F" .. i .. "_Table", "active") == "true" or Global.UI.getAttribute("F" .. i .. "_Table", "active") == "True" then
  62.           Global.UI.setAttribute("F" .. i .. "Name", "text", Global.UI.getAttribute("F" .. i .. "_name", "text"))
  63.           Global.UI.setAttribute("F" .. i, "isOn", "true")
  64.         end
  65.       end
  66.     end
  67.     Global.UI.show("Bars Config")
  68.     Global.UI.setAttribute("Bars Config", "active", "true")
  69.   else
  70.     Global.UI.hide("Bars Config")
  71.     Global.UI.setAttribute("Bars Config", "active", "false")
  72.   end
  73. end
  74.  
  75. function onClick(player, value, id)
  76.   local classes = {
  77.     P1 = {
  78.       -- colorHex = "#B22A28", color = {0.70, 0.16, 0.15}, class = Global.UI.getAttribute("P1_name", "text")
  79.       colorHex = "#B22A28", color = {1, 1, 1}, class = Global.UI.getAttribute("P1_name", "text"), companion = Global.UI.getAttribute("F1_name", "text")
  80.     },
  81.     P2 = {
  82.       colorHex = "#B22A28", color = {1, 1, 1}, class = Global.UI.getAttribute("P2_name", "text"), companion = Global.UI.getAttribute("F2_name", "text")
  83.     },
  84.     P3 = {
  85.       colorHex = "#B22A28", color = {1, 1, 1}, class = Global.UI.getAttribute("P3_name", "text"), companion = Global.UI.getAttribute("F3_name", "text")
  86.     },
  87.     P4 = {
  88.       colorHex = "#B22A28", color = {1, 1, 1}, class = Global.UI.getAttribute("P4_name", "text"), companion = Global.UI.getAttribute("F4_name", "text")
  89.     },
  90.     stamina = {
  91.       colorHex = "#E8CE74", color = {1, 1, 1}
  92.     },
  93.     companion = {
  94.       colorHex = "#721413", color = {1, 1, 1}
  95.     },
  96.     stam = "Strain",
  97.     hp = "Health"
  98.   }
  99.  
  100.   classes["hp1"] = " gained 1 Health"
  101.   classes["hp-1"] = " lost 1 Health"
  102.   classes["stam1"] = " gained 1 Strain"
  103.   classes["stam-1"] = " used 1 Strain"
  104.  
  105.   local bar = id:sub(1,1) == "H" and "hp" or "stam"
  106.   local max = id:sub(2,2) == "M" and "max" or "act"
  107.   local change = id:sub(3,3) == "U" and 1 or -1
  108.   local barText = Global.UI.getAttribute(value .. "_total" .. bar, "text")
  109.   local stats = {act = barText:match("(%d+)/"), max = barText:match("/(%d+)")}
  110.   stats[max] = stats[max] + change
  111.   if tonumber(stats[max]) < 0 then stats[max] = 0 end
  112.   if max == "max" then
  113.     stats.act = stats.max
  114.   else
  115.     -- if tonumber(stats.act) > tonumber(stats.max) then stats.act = stats end
  116.     if tonumber(stats.act) > tonumber(stats.max) then return end
  117.     if broadcast then
  118.       if value:sub(1,1) == "P" then
  119.         broadcastToAll(classes[value].class .. classes[bar .. change], classes[value].color)
  120.       else
  121.         broadcastToAll(classes["P" .. value:sub(2,2)].companion .. ", the Companion of " .. classes["P" .. value:sub(2,2)].class .. ", " .. classes[bar .. change], classes.companion.color)
  122.       end
  123.     end
  124.   end
  125.   Global.UI.setAttribute(value .. "_total" .. bar, "text", stats.act .. "/" .. stats.max)
  126.   Global.UI.setAttribute(value .. "_" .. bar .. "_bar", "percentage", stats.act / stats.max * 100)
  127. end
  128.  
  129. function toggle(player, value, id)
  130.   Global.UI.setAttribute(id, "isOn", value)
  131. end
  132.  
  133. function onValueChanged(player, value, id)
  134.   Global.UI.setAttribute(id, "value", value)
  135. end
  136.  
  137. function toggleBroadcast()
  138.   broadcast = not broadcast
  139.   if broadcast then
  140.     broadcastToAll("Broadcast is now activated", {0,1,0})
  141.   else
  142.     broadcastToAll("Broadcast is now deactivated", {1,0,0})
  143.   end
  144. end
  145.  
  146. function changeBars(player, value, id)
  147.   local totalHeight = 0
  148.   for i=1,4 do
  149.     local height = 100
  150.     if Global.UI.getAttribute("F" .. i, "isOn") == "True" or Global.UI.getAttribute("F" .. i, "isOn") == "true" then
  151.       Global.UI.setAttribute("F" .. i .. "_name", "text", Global.UI.getAttribute("F" .. i .. "Name", "value"))
  152.       height = 150
  153.     end
  154.     if Global.UI.getAttribute("P" .. i, "isOn") == "True" or Global.UI.getAttribute("P" .. i, "isOn") == "true" then
  155.       Global.UI.setAttribute("P" .. i .. "_Table", "minHeight", height)
  156.       Global.UI.setAttribute("P" .. i .. "_name", "text", Global.UI.getAttribute("P" .. i .. "Name", "value"))
  157.       totalHeight = totalHeight + height
  158.     end
  159.     Global.UI.setAttribute("P" .. i .. "_Table", "active", Global.UI.getAttribute("P" .. i, "isOn"))
  160.     Global.UI.setAttribute("F" .. i .. "_Table", "active", Global.UI.getAttribute("F" .. i, "isOn"))
  161.   end
  162.   Global.UI.setAttribute("HP Bars", "height", totalHeight)
  163.   showHPBar()
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement