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.

game.lua 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. -- private variables
  2. local m_mouseGrabberWidget
  3. -- private functions
  4. local function onGameKeyPress(self, keyCode, keyboardModifiers)
  5. if keyboardModifiers == KeyboardCtrlModifier then
  6. if keyCode == KeyG then
  7. CharacterList.show()
  8. return true
  9. elseif keyCode == KeyQ then
  10. Game.logout(false)
  11. return true
  12. end
  13. end
  14. return false
  15. end
  16. local function onUseWithMouseRelease(self, mousePosition, mouseButton)
  17. if Game.selectedThing == nil then return false end
  18. if mouseButton == MouseLeftButton then
  19. local clickedWidget = Game.gameUi:recursiveGetChildByPos(mousePosition)
  20. if clickedWidget then
  21. if clickedWidget:getClassName() == 'UIMap' then
  22. local tile = clickedWidget:getTile(mousePosition)
  23. if tile then
  24. Game.useWith(Game.selectedThing, tile:getTopMultiUseThing())
  25. end
  26. elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then
  27. Game.useWith(Game.selectedThing, clickedWidget:getItem())
  28. end
  29. end
  30. end
  31. Game.selectedThing = nil
  32. Mouse.restoreCursor()
  33. self:ungrabMouse()
  34. return true
  35. end
  36. -- public functions
  37. function Game.startUseWith(thing)
  38. Game.selectedThing = thing
  39. m_mouseGrabberWidget:grabMouse()
  40. Mouse.setTargetCursor()
  41. end
  42. function Game.createInterface()
  43. Background.hide()
  44. CharacterList.destroyLoadBox()
  45. Game.gameUi = displayUI('game.otui')
  46. --Keyboard.bindKeyPress('Up', function() Game.walk(North) end)
  47. --Keyboard.bindKeyPress('Down', function() Game.walk(South) end)
  48. --Keyboard.bindKeyPress('Left', function() Game.walk(West) end)
  49. --Keyboard.bindKeyPress('Right', function() Game.walk(East) end)
  50. Keyboard.bindKeyPress('Ctrl+Shift+Up', function() Game.forceWalk(North) end)
  51. Keyboard.bindKeyPress('Ctrl+Shift+Down', function() Game.forceWalk(South) end)
  52. Keyboard.bindKeyPress('Ctrl+Shift+Left', function() Game.forceWalk(West) end)
  53. Keyboard.bindKeyPress('Ctrl+Shift+Right', function() Game.forceWalk(East) end)
  54. rootWidget:moveChildToIndex(Game.gameUi, 1)
  55. Game.gameMapPanel = Game.gameUi:getChildById('gameMapPanel')
  56. Game.gameRightPanel = Game.gameUi:getChildById('gameRightPanel')
  57. Game.gameBottomPanel = Game.gameUi:getChildById('gameBottomPanel')
  58. m_mouseGrabberWidget = Game.gameUi:getChildById('mouseGrabber')
  59. connect(Game.gameUi, { onKeyPress = onGameKeyPress })
  60. connect(m_mouseGrabberWidget, { onMouseRelease = onUseWithMouseRelease })
  61. end
  62. function Game.destroyInterface()
  63. if Game.gameUi then
  64. Game.gameUi:destroy()
  65. Game.gameUi = nil
  66. end
  67. Background.show()
  68. CharacterList.show()
  69. end
  70. function Game.show()
  71. Game.gameUi:show()
  72. Game.gameUi:focus()
  73. Game.gameMapPanel:focus()
  74. end
  75. function Game.hide()
  76. Game.gameUi:hide()
  77. end
  78. -- hooked events
  79. function Game.onLoginError(message)
  80. CharacterList.destroyLoadBox()
  81. local errorBox = displayErrorBox("Login Error", "Login error: " .. message)
  82. connect(errorBox, { onOk = CharacterList.show })
  83. end
  84. function Game.onConnectionError(message)
  85. CharacterList.destroyLoadBox()
  86. local errorBox = displayErrorBox("Login Error", "Connection error: " .. message)
  87. connect(errorBox, { onOk = CharacterList.show })
  88. end
  89. local function onApplicationClose()
  90. if Game.isOnline() then
  91. Game.logout(true)
  92. else
  93. exit()
  94. end
  95. end
  96. setonclose(onApplicationClose)
  97. connect(Game, { onGameStart = Game.createInterface }, true)
  98. connect(Game, { onGameEnd = Game.destroyInterface })