worldMouse.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from panda3d.core import CollisionNode, CollisionTraverser, GeomNode
  22. from panda3d.core import CollisionHandlerQueue, CollisionRay
  23. from direct.showbase import DirectObject
  24. class WorldMouse(DirectObject.DirectObject):
  25. def __init__(self, node):
  26. self._node = node
  27. self.myHandler = CollisionHandlerQueue()
  28. self.myTraverser = CollisionTraverser('mouseTraverser') # picker
  29. self.pickerNode = CollisionNode('mouseRay')
  30. self.pickerNP = camera.attachNewNode(self.pickerNode)
  31. self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
  32. self.pickerRay = CollisionRay()
  33. self.pickerNode.addSolid(self.pickerRay)
  34. self.myTraverser.addCollider(self.pickerNP, self.myHandler)
  35. self._hoverObjs = []
  36. def getClosest(self):
  37. mpos = base.mouseWatcherNode.getMouse()
  38. self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
  39. self.myTraverser.traverse(render)
  40. if self.myHandler.getNumEntries() > 0:
  41. self.myHandler.sortEntries() # Sort so the closest object is at index 0
  42. pickedObj = self.myHandler.getEntry(0).getIntoNodePath()
  43. return pickedObj
  44. return None
  45. class NPCMouse(WorldMouse):
  46. def __init__(self, node, model):
  47. WorldMouse.__init__(self, node)
  48. self._selectModel = model
  49. base.mouseHandler.connect('mouse1', self.onLeftClick, priority=9)
  50. #base.mouseHandler.connect('mouse3', self.onRightClick, priority=9)
  51. @property
  52. def selectModel(self): return self._selectModel
  53. def onLeftClick(self):
  54. """ Select NPC
  55. """
  56. pickedObj = self.getClosest()
  57. if pickedObj:
  58. pickedObjTag = pickedObj.findNetTag('characterId')
  59. if not pickedObjTag.isEmpty():
  60. selectedId = int(pickedObjTag.getTag('spawnId'))
  61. if selectedId != 0: # spawnId 0 == player
  62. self._selectModel.selectedId = int(pickedObjTag.getTag('spawnId'))
  63. return True
  64. self._selectModel.selectedId = -1
  65. return False
  66. def onRightClick(self):
  67. """ NPC interact
  68. """
  69. pickedObj = self.getClosest()
  70. if pickedObj:
  71. pickedObjTag = pickedObj.findNetTag('characterId')
  72. if not pickedObjTag.isEmpty():
  73. selectedId = int(pickedObjTag.getTag('spawnId'))
  74. if selectedId != 0: # spawnId 0 == player
  75. #self._selectModel.selectedId = int(pickedObjTag.getTag('spawnId'))
  76. return True
  77. #self._selectModel.selectedId = -1
  78. return False