main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ########################################################################
  2. # Hello Worlds - Libre 3D RPG game.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Hello Worlds.
  6. #
  7. # Hello Worlds is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Hello Worlds is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. # Global
  22. import sys
  23. sys.path.insert(0, "ui/lui")
  24. # Lui
  25. from LUISkin import LUIDefaultSkin
  26. from LUIRegion import LUIRegion
  27. from LUIInputHandler import LUIInputHandler
  28. # UI
  29. from ui.manager import UI_Manager
  30. from core.world import World
  31. from core.mouse import MouseHandler
  32. from core import db # init db
  33. # Panda
  34. from direct.showbase.ShowBase import ShowBase
  35. from panda3d.core import load_prc_file_data
  36. load_prc_file_data("", """
  37. text-minfilter linear
  38. text-magfilter linear
  39. text-pixels-per-unit 32
  40. sync-video #f
  41. textures-power-2 none
  42. show-frame-rate-meter #t
  43. win-size 1280 720
  44. window-title Hello Worlds
  45. support-threads #t
  46. task-timer-verbose #t
  47. """)
  48. class Game(ShowBase):
  49. def __init__(self):
  50. ShowBase.__init__(self)
  51. # Show FPS
  52. self.setFrameRateMeter(True)
  53. # Disable default mouse look behaviour
  54. self.disableMouse()
  55. """ Db assets path
  56. """
  57. db.AssetsPath.set('./assets/')
  58. """ Mouse handler
  59. """
  60. self.mouseHandler = MouseHandler()
  61. """ World
  62. """
  63. self.world = World()
  64. """ LUI
  65. """
  66. # Construct a new LUIRegion
  67. # Note: base.luiRegion and self.luiRegion are the same
  68. self.luiRegion = LUIRegion.make("LUI", base.win)
  69. # Construct a new InputHandler to catch and process events
  70. self.luiInputHandler = LUIInputHandler()
  71. self.mouseWatcher.getParent().attach_new_node(self.luiInputHandler)
  72. self.luiRegion.set_input_handler(self.luiInputHandler)
  73. # Load the default LUI skin
  74. skin = LUIDefaultSkin()
  75. skin.load()
  76. """ UI manager
  77. """
  78. self.uiManager = UI_Manager()
  79. """ Main shortcuts
  80. """
  81. self.accept('escape', self.toggleStartMenu)
  82. """ Connections
  83. """
  84. self.accept('UI_STARTMENU_START_PRESSED', self._startClicked)
  85. self.accept('UI_STARTMENU_EXIT_PRESSED', self._exitClicked)
  86. """ Window event task
  87. """
  88. self._currentRatio = base.getAspectRatio()
  89. taskMgr.doMethodLater(1, self._windowEventCheck, 'windowEvent')
  90. def _startClicked(self):
  91. self.uiManager.startMenu.hide()
  92. self.world.loadMap(1)
  93. def _exitClicked(self):
  94. self.world.destroy()
  95. self.uiManager.close()
  96. self.finalizeExit()
  97. def toggleStartMenu(self):
  98. self.uiManager.startMenu.hide() if self.uiManager.startMenu.isVisible() else self.uiManager.startMenu.show()
  99. def _windowEventCheck(self, task):
  100. if self._currentRatio != base.getAspectRatio():
  101. self._currentRatio = base.getAspectRatio()
  102. messenger.send('WINDOW_RESIZED')
  103. return task.again
  104. game = Game()
  105. game.run()