2012-02-09 06:27:29 +01:00
|
|
|
Battle = {}
|
|
|
|
|
|
|
|
--TODO
|
|
|
|
--onCreatureAppears onCreatureHealthChange onCreatureDisappears
|
|
|
|
--reloadable/disconnects
|
|
|
|
|
|
|
|
-- private variables
|
|
|
|
local battleWindow
|
|
|
|
local battleButton
|
|
|
|
local battlePanel
|
|
|
|
local lastBattleButtonSwitched
|
|
|
|
local checkCreaturesEvent
|
|
|
|
local battleButtonsByCreaturesList = {}
|
|
|
|
|
|
|
|
local mouseWidget
|
|
|
|
|
|
|
|
local hidePlayersButton
|
|
|
|
local hideNPCsButton
|
|
|
|
local hideMonstersButton
|
|
|
|
local hideSkullsButton
|
|
|
|
local hidePartyButton
|
|
|
|
|
|
|
|
local battleButtonColors = {
|
|
|
|
onIdle = {notHovered = '#888888', hovered = '#FFFFFF' },
|
|
|
|
onTargeted = {notHovered = '#FF0000', hovered = '#FF8888' },
|
|
|
|
onFollowed = {notHovered = '#00FF00', hovered = '#88FF88' }
|
|
|
|
}
|
|
|
|
|
|
|
|
local lifeBarColors = {} --Must be sorted by percentAbose
|
|
|
|
table.insert(lifeBarColors, {percentAbove = 92, color = '#00BC00' } )
|
|
|
|
table.insert(lifeBarColors, {percentAbove = 60, color = '#50A150' } )
|
|
|
|
table.insert(lifeBarColors, {percentAbove = 30, color = '#A1A100' } )
|
|
|
|
table.insert(lifeBarColors, {percentAbove = 8, color = '#3C2727' } )
|
|
|
|
table.insert(lifeBarColors, {percentAbove = 3, color = '#3C0000' } )
|
|
|
|
table.insert(lifeBarColors, {percentAbove = -1, color = '#4F0000' } )
|
|
|
|
|
|
|
|
-- public functions
|
2012-03-28 16:10:21 +02:00
|
|
|
function Battle.init()
|
2012-03-22 22:47:52 +01:00
|
|
|
battleWindow = displayUI('battle.otui', GameInterface.getRightPanel())
|
2012-03-28 16:10:21 +02:00
|
|
|
battleButton = TopMenu.addGameToggleButton('battleButton', 'Battle (Ctrl+B)', 'battle.png', Battle.toggle)
|
|
|
|
battleButton:setOn(true)
|
2012-02-09 06:27:29 +01:00
|
|
|
Keyboard.bindKeyDown('Ctrl+B', Battle.toggle)
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-03-28 16:10:21 +02:00
|
|
|
battlePanel = battleWindow:recursiveGetChildById('battlePanel')
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-03-28 16:10:21 +02:00
|
|
|
hidePlayersButton = battleWindow:recursiveGetChildById('hidePlayers')
|
|
|
|
hideNPCsButton = battleWindow:recursiveGetChildById('hideNPCs')
|
|
|
|
hideMonstersButton = battleWindow:recursiveGetChildById('hideMonsters')
|
|
|
|
hideSkullsButton = battleWindow:recursiveGetChildById('hideSkulls')
|
|
|
|
hidePartyButton = battleWindow:recursiveGetChildById('hideParty')
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
mouseWidget = createWidget('UIButton')
|
|
|
|
mouseWidget:setVisible(false)
|
2012-02-20 03:27:08 +01:00
|
|
|
mouseWidget:setFocusable(false)
|
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
connect(Creature, { onSkullChange = Battle.checkCreatureSkull,
|
2012-02-20 03:27:08 +01:00
|
|
|
onEmblemChange = Battle.checkCreatureEmblem } )
|
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
connect(g_game, { onAttackingCreatureChange = Battle.onAttack,
|
|
|
|
onFollowingCreatureChange = Battle.onFollow } )
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
addEvent(Battle.addAllCreatures)
|
|
|
|
checkCreaturesEvent = scheduleEvent(Battle.checkCreatures, 200)
|
|
|
|
end
|
|
|
|
|
2012-03-28 16:10:21 +02:00
|
|
|
function Battle.terminate()
|
2012-02-09 06:27:29 +01:00
|
|
|
Keyboard.unbindKeyDown('Ctrl+B')
|
2012-03-28 16:10:21 +02:00
|
|
|
battlePanel = nil
|
2012-02-09 06:27:29 +01:00
|
|
|
lastBattleButtonTargeted = nil
|
|
|
|
lastBattleButtonFollowed = nil
|
|
|
|
battleButtonsByCreaturesList = {}
|
|
|
|
removeEvent(checkCreaturesEvent)
|
|
|
|
hidePlayersButton = nil
|
|
|
|
hideNPCsButton = nil
|
|
|
|
hideMonstersButton = nil
|
|
|
|
hideSkullsButton = nil
|
|
|
|
hidePartyButton = nil
|
|
|
|
checkCreaturesEvent = nil
|
|
|
|
battleButton:destroy()
|
|
|
|
battleButton = nil
|
|
|
|
battleWindow:destroy()
|
|
|
|
battleWindow = nil
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
disconnect(Creature, { onSkullChange = Battle.checkCreatureSkull,
|
2012-02-20 03:27:08 +01:00
|
|
|
onEmblemChange = Battle.checkCreatureEmblem } )
|
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
disconnect(g_game, { onAttackingCreatureChange = Battle.onAttack } )
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.toggle()
|
|
|
|
local visible = not battleWindow:isExplicitlyVisible()
|
|
|
|
battleWindow:setVisible(visible)
|
|
|
|
battleButton:setOn(visible)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.addAllCreatures()
|
2012-02-20 03:27:08 +01:00
|
|
|
local spectators = {}
|
2012-03-28 16:10:21 +02:00
|
|
|
local player = g_game.getLocalPlayer()
|
|
|
|
if player then
|
|
|
|
creatures = g_map.getSpectators(player:getPosition(), false)
|
|
|
|
for i, creature in ipairs(creatures) do
|
|
|
|
if creature ~= player and Battle.doCreatureFitFilters(creature) then
|
|
|
|
table.insert(spectators, creature)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
for i, v in pairs(spectators) do
|
|
|
|
Battle.addCreature(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.doCreatureFitFilters(creature)
|
|
|
|
local hidePlayers = hidePlayersButton:isChecked()
|
|
|
|
local hideNPCs = hideNPCsButton:isChecked()
|
|
|
|
local hideMonsters = hideMonstersButton:isChecked()
|
|
|
|
local hideSkulls = hideSkullsButton:isChecked()
|
|
|
|
local hideParty = hidePartyButton:isChecked()
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
if hidePlayers and not creature:asMonster() and not creature:asNpc() then
|
|
|
|
return false
|
|
|
|
elseif hideNPCs and creature:asNpc() then
|
|
|
|
return false
|
|
|
|
elseif hideMonsters and creature:asMonster() then
|
|
|
|
return false
|
|
|
|
elseif hideSkulls and creature:getSkull() == SkullNone then
|
|
|
|
return false
|
|
|
|
elseif hideParty and creature:getShield() > ShieldWhiteBlue then
|
|
|
|
return false
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.checkCreatures()
|
|
|
|
local player = g_game.getLocalPlayer()
|
2012-02-20 03:27:08 +01:00
|
|
|
if player then
|
2012-02-09 06:27:29 +01:00
|
|
|
local spectators = {}
|
|
|
|
|
|
|
|
-- reloading list of spectators
|
2012-02-20 03:27:08 +01:00
|
|
|
local creaturesAppeared = {}
|
2012-02-09 06:27:29 +01:00
|
|
|
creatures = g_map.getSpectators(player:getPosition(), false)
|
|
|
|
for i, creature in ipairs(creatures) do
|
|
|
|
if creature ~= player and Battle.doCreatureFitFilters(creature) then
|
|
|
|
-- searching for creatures that appeared on battle list
|
|
|
|
local battleButton = battleButtonsByCreaturesList[creature:getId()]
|
|
|
|
if battleButton == nil then
|
2012-02-20 03:27:08 +01:00
|
|
|
table.insert(creaturesAppeared, creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
else
|
|
|
|
Battle.setLifeBarPercent(battleButton, creature:getHealthPercent())
|
|
|
|
end
|
|
|
|
spectators[creature:getId()] = creature
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
for i, v in pairs(creaturesAppeared) do
|
|
|
|
Battle.addCreature(v)
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
-- searching for creatures that disappeared from battle list
|
|
|
|
local creaturesDisappeared = {}
|
|
|
|
for i, creature in pairs(battleButtonsByCreaturesList) do
|
|
|
|
if spectators[creature.creatureId] == nil then
|
|
|
|
table.insert(creaturesDisappeared, creature.creature)
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
for i, v in pairs(creaturesDisappeared) do
|
|
|
|
Battle.removeCreature(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
checkCreaturesEvent = scheduleEvent(Battle.checkCreatures, 500)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.addCreature(creature)
|
|
|
|
local creatureId = creature:getId()
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButtonsByCreaturesList[creatureId] == nil then
|
2012-03-28 16:10:21 +02:00
|
|
|
local battleButton = displayUI('battleButton.otui', battlePanel)
|
2012-02-09 06:27:29 +01:00
|
|
|
local creatureWidget = battleButton:getChildById('creature')
|
|
|
|
local labelWidget = battleButton:getChildById('label')
|
|
|
|
local lifeBarWidget = battleButton:getChildById('lifeBar')
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
battleButton:setId('BattleButton_' .. creature:getName():gsub('%s','_'))
|
|
|
|
battleButton.creatureId = creatureId
|
|
|
|
battleButton.creature = creature
|
|
|
|
battleButton.isHovered = false
|
|
|
|
battleButton.isTarget = false
|
|
|
|
battleButton.isFollowed = false
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
labelWidget:setText(creature:getName())
|
2012-02-20 03:27:08 +01:00
|
|
|
creatureWidget:setCreature(creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
Battle.setLifeBarPercent(battleButton, creature:getHealthPercent())
|
|
|
|
|
|
|
|
battleButtonsByCreaturesList[creatureId] = battleButton
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
Battle.checkCreatureSkull(battleButton.creature)
|
2012-02-20 03:27:08 +01:00
|
|
|
Battle.checkCreatureEmblem(battleButton.creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.checkCreatureSkull(creature, skullId)
|
|
|
|
local battleButton = battleButtonsByCreaturesList[creature:getId()]
|
|
|
|
if battleButton then
|
|
|
|
local skullWidget = battleButton:getChildById('skull')
|
|
|
|
local labelWidget = battleButton:getChildById('label')
|
|
|
|
local creature = battleButton.creature
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
if creature:getSkull() ~= SkullNone then
|
|
|
|
skullWidget:setWidth(skullWidget:getHeight())
|
|
|
|
local imagePath = getSkullImagePath(creature:getSkull())
|
|
|
|
skullWidget:setImageSource('/game/' .. imagePath)
|
|
|
|
labelWidget:setMarginLeft(5)
|
|
|
|
else
|
|
|
|
skullWidget:setWidth(0)
|
|
|
|
if creature:getEmblem() == EmblemNone then
|
|
|
|
labelWidget:setMarginLeft(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.checkCreatureEmblem(creature, emblemId)
|
|
|
|
local battleButton = battleButtonsByCreaturesList[creature:getId()]
|
|
|
|
if battleButton then
|
|
|
|
local emblemId = emblemId or creature:getEmblem()
|
|
|
|
local emblemWidget = battleButton:getChildById('emblem')
|
|
|
|
local labelWidget = battleButton:getChildById('label')
|
|
|
|
local creature = battleButton.creature
|
|
|
|
|
|
|
|
if emblemId ~= EmblemNone then
|
|
|
|
emblemWidget:setWidth(emblemWidget:getHeight())
|
|
|
|
local imagePath = getEmblemImagePath(emblemId)
|
|
|
|
emblemWidget:setImageSource('/game/' .. imagePath)
|
|
|
|
emblemWidget:setMarginLeft(5)
|
|
|
|
labelWidget:setMarginLeft(5)
|
|
|
|
else
|
|
|
|
emblemWidget:setWidth(0)
|
|
|
|
emblemWidget:setMarginLeft(0)
|
|
|
|
if creature:getSkull() == SkullNone then
|
|
|
|
labelWidget:setMarginLeft(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.onMouseRelease(self, mousePosition, mouseButton)
|
|
|
|
if mouseButton == MouseRightButton then
|
2012-03-28 18:20:07 +02:00
|
|
|
GameInterface.createThingMenu(mousePosition, nil, nil, self.creature)
|
2012-02-09 08:26:52 +01:00
|
|
|
return true
|
|
|
|
elseif mouseButton == MouseLeftButton then
|
|
|
|
local modifiers = g_window.getKeyboardModifiers()
|
|
|
|
if modifiers == KeyboardShiftModifier then
|
|
|
|
g_game.look(self.creature)
|
|
|
|
else
|
|
|
|
if self.isTarget then
|
|
|
|
g_game.cancelAttack()
|
|
|
|
else
|
|
|
|
g_game.attack(self.creature)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
2012-02-09 08:26:52 +01:00
|
|
|
return true
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.removeCreature(creature)
|
|
|
|
local creatureId = creature:getId()
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButtonsByCreaturesList[creatureId] ~= nil then
|
|
|
|
if lastBattleButtonSwitched == battleButtonsByCreaturesList[creatureId] then
|
|
|
|
lastBattleButtonSwitched = nil
|
|
|
|
end
|
2012-03-30 00:36:55 +02:00
|
|
|
|
|
|
|
if battleButtonsByCreaturesList[creatureId].isTarget then
|
|
|
|
g_game.cancelAttack()
|
|
|
|
end
|
2012-02-09 08:26:52 +01:00
|
|
|
battleButtonsByCreaturesList[creatureId].creature:hideStaticSquare()
|
2012-02-09 06:27:29 +01:00
|
|
|
battleButtonsByCreaturesList[creatureId]:destroy()
|
|
|
|
battleButtonsByCreaturesList[creatureId] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.setLifeBarPercent(battleButton, percent)
|
|
|
|
local lifeBarWidget = battleButton:getChildById('lifeBar')
|
|
|
|
lifeBarWidget:setPercent(percent)
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
local color
|
|
|
|
for i, v in pairs(lifeBarColors) do
|
|
|
|
if percent > v.percentAbove then
|
|
|
|
color = v.color
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
lifeBarWidget:setBackgroundColor(color)
|
|
|
|
end
|
|
|
|
|
2012-03-28 16:10:21 +02:00
|
|
|
function Battle.onbattleButtonHoverChange(widget, hovered)
|
2012-02-20 03:27:08 +01:00
|
|
|
if widget.isBattleButton then
|
2012-02-09 08:26:52 +01:00
|
|
|
widget.isHovered = hovered
|
|
|
|
Battle.checkBattleButton(widget)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
function Battle.onAttack(creature)
|
|
|
|
local battleButton = creature and battleButtonsByCreaturesList[creature:getId()] or lastBattleButtonSwitched
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton then
|
|
|
|
battleButton.isTarget = creature and true or false
|
|
|
|
Battle.checkBattleButton(battleButton)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
function Battle.onFollow(creature)
|
|
|
|
local battleButton = creature and battleButtonsByCreaturesList[creature:getId()] or lastBattleButtonSwitched
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton then
|
|
|
|
battleButton.isFollowed = creature and true or false
|
|
|
|
Battle.checkBattleButton(battleButton)
|
2012-02-20 03:27:08 +01:00
|
|
|
end
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Battle.checkBattleButton(battleButton)
|
|
|
|
local color = battleButtonColors.onIdle
|
|
|
|
if battleButton.isTarget then
|
|
|
|
color = battleButtonColors.onTargeted
|
|
|
|
elseif battleButton.isFollowed then
|
|
|
|
color = battleButtonColors.onFollowed
|
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
color = battleButton.isHovered and color.hovered or color.notHovered
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton.isHovered or battleButton.isTarget or battleButton.isFollowed then
|
|
|
|
battleButton.creature:showStaticSquare(color)
|
|
|
|
battleButton:getChildById('creature'):setBorderWidth(1)
|
|
|
|
battleButton:getChildById('creature'):setBorderColor(color)
|
2012-02-20 03:27:08 +01:00
|
|
|
battleButton:getChildById('label'):setColor(color)
|
|
|
|
else
|
2012-02-09 06:27:29 +01:00
|
|
|
battleButton.creature:hideStaticSquare()
|
|
|
|
battleButton:getChildById('creature'):setBorderWidth(0)
|
2012-02-20 03:27:08 +01:00
|
|
|
battleButton:getChildById('label'):setColor(color)
|
2012-02-09 06:27:29 +01:00
|
|
|
end
|
2012-02-20 03:27:08 +01:00
|
|
|
|
2012-02-09 06:27:29 +01:00
|
|
|
if battleButton.isTarget or battleButton.isFollowed then
|
|
|
|
if lastBattleButtonSwitched and lastBattleButtonSwitched ~= battleButton then
|
|
|
|
lastBattleButtonSwitched.isTarget = false
|
|
|
|
lastBattleButtonSwitched.isFollowed = false
|
|
|
|
Battle.checkBattleButton(lastBattleButtonSwitched)
|
|
|
|
end
|
|
|
|
lastBattleButtonSwitched = battleButton
|
|
|
|
end
|
|
|
|
end
|