tibia-client/modules/game/game.lua

119 lines
3.7 KiB
Lua
Raw Normal View History

-- private variables
local m_mouseGrabberWidget
-- private functions
local function onGameKeyPress(self, keyCode, keyboardModifiers)
if keyboardModifiers == KeyboardCtrlModifier then
if keyCode == KeyG then
CharacterList.show()
return true
elseif keyCode == KeyQ then
2012-02-08 22:23:15 +01:00
g_game.safeLogout()
return true
end
end
return false
end
local function onUseWithMouseRelease(self, mousePosition, mouseButton)
2012-02-08 22:23:15 +01:00
if g_game.selectedThing == nil then return false end
if mouseButton == MouseLeftButton then
2012-02-08 22:23:15 +01:00
local clickedWidget = g_game.gameUi:recursiveGetChildByPos(mousePosition)
if clickedWidget then
2012-02-01 20:37:40 +01:00
if clickedWidget:getClassName() == 'UIMap' then
local tile = clickedWidget:getTile(mousePosition)
if tile then
2012-02-08 22:23:15 +01:00
g_game.useWith(g_game.selectedThing, tile:getTopMultiUseThing())
end
elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then
2012-02-08 22:23:15 +01:00
g_game.useWith(g_game.selectedThing, clickedWidget:getItem())
elseif clickedWidget.isBattleButton or clickedWidget.battleButtonChild then
local battleButton = clickedWidget
if clickedWidget.battleButtonChild then
battleButton = clickedWidget:getParent()
end
g_game.useWith(g_game.selectedThing, battleButton.creature)
end
end
end
2012-02-08 22:23:15 +01:00
g_game.selectedThing = nil
Mouse.restoreCursor()
self:ungrabMouse()
return true
end
-- public functions
2012-02-08 22:23:15 +01:00
function g_game.startUseWith(thing)
g_game.selectedThing = thing
m_mouseGrabberWidget:grabMouse()
Mouse.setTargetCursor()
end
2012-02-08 22:23:15 +01:00
function g_game.createInterface()
Background.hide()
CharacterList.destroyLoadBox()
2012-02-08 22:23:15 +01:00
g_game.gameUi = displayUI('game.otui')
2012-02-08 22:23:15 +01:00
--Keyboard.bindKeyPress('Up', function() g_game.walk(North) end)
--Keyboard.bindKeyPress('Down', function() g_game.walk(South) end)
--Keyboard.bindKeyPress('Left', function() g_game.walk(West) end)
--Keyboard.bindKeyPress('Right', function() g_game.walk(East) end)
2012-02-08 22:23:15 +01:00
Keyboard.bindKeyPress('Ctrl+Shift+Up', function() g_game.forceWalk(North) end)
Keyboard.bindKeyPress('Ctrl+Shift+Down', function() g_game.forceWalk(South) end)
Keyboard.bindKeyPress('Ctrl+Shift+Left', function() g_game.forceWalk(West) end)
Keyboard.bindKeyPress('Ctrl+Shift+Right', function() g_game.forceWalk(East) end)
2012-02-08 22:23:15 +01:00
rootWidget:moveChildToIndex(g_game.gameUi, 1)
g_game.gameMapPanel = g_game.gameUi:getChildById('gameMapPanel')
g_game.gameRightPanel = g_game.gameUi:getChildById('gameRightPanel')
g_game.gameBottomPanel = g_game.gameUi:getChildById('gameBottomPanel')
m_mouseGrabberWidget = g_game.gameUi:getChildById('mouseGrabber')
connect(g_game.gameUi, { onKeyPress = onGameKeyPress })
connect(m_mouseGrabberWidget, { onMouseRelease = onUseWithMouseRelease })
2011-08-29 05:44:02 +02:00
end
2012-02-08 22:23:15 +01:00
function g_game.destroyInterface()
if g_game.gameUi then
g_game.gameUi:destroy()
g_game.gameUi = nil
end
Background.show()
CharacterList.show()
end
2012-02-08 22:23:15 +01:00
function g_game.show()
g_game.gameUi:show()
g_game.gameUi:focus()
g_game.gameMapPanel:focus()
end
2012-02-08 22:23:15 +01:00
function g_game.hide()
g_game.gameUi:hide()
end
-- hooked events
2012-02-08 22:23:15 +01:00
function g_game.onLoginError(message)
2011-08-30 01:20:30 +02:00
CharacterList.destroyLoadBox()
local errorBox = displayErrorBox("Login Error", "Login error: " .. message)
2012-02-06 20:19:47 +01:00
connect(errorBox, { onOk = CharacterList.show })
2011-08-30 01:20:30 +02:00
end
2012-02-08 22:23:15 +01:00
function g_game.onConnectionError(message)
2011-08-29 20:38:01 +02:00
CharacterList.destroyLoadBox()
local errorBox = displayErrorBox("Login Error", "Connection error: " .. message)
2012-02-06 20:19:47 +01:00
connect(errorBox, { onOk = CharacterList.show })
2011-08-29 20:38:01 +02:00
end
2012-01-07 06:35:50 +01:00
local function onApplicationClose()
2012-02-08 22:23:15 +01:00
if g_game.isOnline() then
g_game.forceLogout()
2012-01-07 06:35:50 +01:00
else
exit()
end
end
setonclose(onApplicationClose)
2012-02-08 22:23:15 +01:00
connect(g_game, { onGameStart = g_game.createInterface }, true)
connect(g_game, { onGameEnd = g_game.destroyInterface })