No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

skills.lua 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. Skills = {}
  2. -- private variables
  3. local skillsWindow
  4. local skillsButton
  5. -- private functions
  6. local function getNumberString(number)
  7. local out = ''
  8. number = tostring(number):reverse()
  9. for i=1,#number do
  10. out = out .. number:sub(i, i)
  11. if i % 3 == 0 and i ~= #number then
  12. out = out .. ','
  13. end
  14. end
  15. out = out:reverse()
  16. return out
  17. end
  18. local function setSkillValue(id, value)
  19. local skill = skillsWindow:recursiveGetChildById(id)
  20. if skill then
  21. local widget = skill:getChildById('value')
  22. widget:setText(value)
  23. end
  24. end
  25. local function setSkillPercent(id, percent, tooltip)
  26. local skill = skillsWindow:recursiveGetChildById(id)
  27. if skill then
  28. local widget = skill:getChildById('percent')
  29. widget:setPercent(percent)
  30. if tooltip then
  31. widget:setTooltip(tooltip)
  32. end
  33. end
  34. end
  35. -- public functions
  36. function Skills.create()
  37. skillsWindow = displayUI('skills.otui', { parent = Game.gameRightPanel })
  38. skillsWindow:hide()
  39. skillsButton = TopMenu.addGameButton('skillsButton', 'Skills (Ctrl+S)', '/core_styles/icons/skills.png', Skills.toggle)
  40. Keyboard.bindKeyDown('Ctrl+S', Skills.toggle)
  41. end
  42. function Skills.destroy()
  43. Keyboard.unbindKeyDown('Ctrl+S')
  44. skillsButton:destroy()
  45. skillsButton = nil
  46. skillsWindow:destroy()
  47. skillsWindow = nil
  48. end
  49. function Skills.toggle()
  50. local visible = not skillsWindow:isExplicitlyVisible()
  51. skillsWindow:setVisible(visible)
  52. skillsButton:setOn(visible)
  53. end
  54. function Skills.onSkillButtonClick(button)
  55. local percentBar = button:getChildById('percent')
  56. if percentBar then
  57. percentBar:setVisible(not percentBar:isVisible())
  58. if percentBar:isVisible() then
  59. button:setHeight(21)
  60. else
  61. button:setHeight(21 - 6)
  62. end
  63. end
  64. end
  65. -- hooked events
  66. function Skills.onExperienceChange(value)
  67. setSkillValue('experience', getNumberString(value))
  68. end
  69. function Skills.onLevelChange(value, percent)
  70. setSkillValue('level', getNumberString(value))
  71. setSkillPercent('level', percent, 'You have ' .. (100 - percent) .. ' percent to go')
  72. end
  73. function Skills.onHealthChange(health, maxHealth)
  74. setSkillValue('health', getNumberString(health))
  75. end
  76. function Skills.onManaChange(mana, maxMana)
  77. setSkillValue('mana', getNumberString(mana))
  78. end
  79. function Skills.onSoulChange(soul)
  80. setSkillValue('soul', soul)
  81. end
  82. function Skills.onFreeCapacityChange(freeCapacity)
  83. setSkillValue('capacity', freeCapacity)
  84. end
  85. function Skills.onStaminaChange(stamina)
  86. local hours = math.floor(stamina / 60)
  87. local minutes = stamina % 60
  88. if minutes < 10 then
  89. minutes = '0' .. minutes
  90. end
  91. local percent = 100 * stamina / (42 * 60) -- max is 42 hours
  92. setSkillValue('stamina', hours .. ":" .. minutes)
  93. setSkillPercent('stamina', percent, 'You have ' .. percent .. ' percent')
  94. end
  95. function Skills.onMagicLevelChange(value, percent)
  96. setSkillValue('magiclevel', value)
  97. setSkillPercent('magiclevel', percent, 'You have ' .. (100 - percent) .. ' percent to go')
  98. end
  99. function Skills.onSkillChange(id, level, percent)
  100. setSkillValue('skillId' .. id, level)
  101. setSkillPercent('skillId' .. id, percent, 'You have ' .. (100 - percent) .. ' percent to go')
  102. end
  103. connect(Game, { onGameStart = Skills.create,
  104. onGameEnd = Skills.destroy,
  105. onExperienceChange = Skills.onExperienceChange,
  106. onLevelChange = Skills.onLevelChange,
  107. onHealthChange = Skills.onHealthChange,
  108. onManaChange = Skills.onManaChange,
  109. onSoulChange = Skills.onSoulChange,
  110. onFreeCapacityChange = Skills.onFreeCapacityChange,
  111. onStaminaChange = Skills.onStaminaChange,
  112. onMagicLevelChange = Skills.onMagicLevelChange,
  113. onSkillChange = Skills.onSkillChange })