world.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. # Panda Bullet
  22. from panda3d.bullet import BulletWorld, BulletDebugNode
  23. # Local
  24. from core.mapLoader import MapLoader
  25. from core.character import Character
  26. from core.player import Player
  27. from core.npcsManager import NPCsManager
  28. from core.db import GenericSpawnData, PlayerData
  29. class World:
  30. def __init__(self, rootNode=None):
  31. if not rootNode: rootNode = base.render
  32. # Setup Bullet
  33. self._world = BulletWorld()
  34. self._worldNP = rootNode.attachNewNode('World')
  35. self._pendingCharacter = None
  36. self._pendingMapId = None
  37. self._player = None
  38. """ Setup test character (which is played by a Player)
  39. So this is TODO for future character creation.
  40. """
  41. """
  42. 0 characterId (npc or player-character)
  43. 1 x
  44. 2 y
  45. 3 z
  46. 4 o
  47. """
  48. spawnData = GenericSpawnData(0, [1,0,0,18,0])
  49. """
  50. 0 name
  51. 1 dirName
  52. 2 file
  53. 3 animations {}
  54. 4 stats {}
  55. 5 collisionShape {}
  56. 6 speciesId
  57. """
  58. characterData = PlayerData(_id=1, data = ['Debug', 'character_1', 'character_1.egg', {}, {}, 1])
  59. self.character = Character(characterData, spawnData)
  60. # Debuging
  61. self.debugNP = self._worldNP.attachNewNode(BulletDebugNode('Debug'))
  62. self.debugNP.show()
  63. self.debugNP.node().showNormals(True)
  64. self._world.setDebugNode(self.debugNP.node())
  65. # Map loader
  66. self._mapLoader = MapLoader(self._world, self._worldNP)
  67. # NPC's
  68. self._npcsManager = NPCsManager(self._world, self._worldNP)
  69. # Game objects
  70. # ..
  71. # NPCs
  72. # ..
  73. base.accept('x', self.toggleDebug)
  74. taskMgr.add(self.update, 'updateWorld')
  75. self.debugNP.hide()
  76. @property
  77. def npcsManager(self): return self._npcsManager
  78. @property
  79. def mapLoader(self): return self._mapLoader
  80. @property
  81. def player(self): return self._player
  82. @property
  83. def world(self): return self._world
  84. def destroy(self):
  85. taskMgr.remove('updateWorld')
  86. base.uiManager.destroyGameUI()
  87. if self.player:
  88. self.player.destroy()
  89. self._player = None
  90. self.npcsManager.clear()
  91. self.mapLoader.unload()
  92. def loadMap(self, mapId):
  93. self.mapLoader.load(mapId, self._onMapLoaded)
  94. self._pendingMapId = mapId
  95. def _onMapLoaded(self):
  96. self._player = Player(self._world, self._worldNP, self.character, self._pendingMapId)
  97. self._pendingMapId = None
  98. self.npcsManager.setPlayer(self.player)
  99. self.npcsManager.setSpawnData(
  100. self.mapLoader.currentMap.spawns
  101. )
  102. base.uiManager.loadGameUI()
  103. def toggleDebug(self):
  104. if self.debugNP.isHidden(): self.debugNP.show()
  105. else: self.debugNP.hide()
  106. def doPhysics(self, dt): self._world.doPhysics(dt, 10, 0.008)
  107. def update(self, task):
  108. dt = globalClock.getDt()
  109. self.doPhysics(dt)
  110. return task.cont