world.py 3.7 KB

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