ClientRepository.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from direct.distributed.ClientRepository import ClientRepository
  2. from panda3d.core import URLSpec, ConfigVariableInt, ConfigVariableString
  3. from DistributedSmoothActor import DistributedSmoothActor
  4. class GameClientRepository(ClientRepository):
  5. def __init__(self):
  6. dcFileNames = ['../direct.dc', 'sample.dc']
  7. # a distributed object of our game.
  8. self.distributedObject = None
  9. self.aiDGameObect = None
  10. ClientRepository.__init__(
  11. self,
  12. dcFileNames = dcFileNames,
  13. threadedNet = True)
  14. # Set the same port as configured on the server to be able to connect
  15. # to it
  16. tcpPort = ConfigVariableInt('server-port', 4400).getValue()
  17. # Set the IP or hostname of the server we want to connect to
  18. hostname = ConfigVariableString('server-host', '127.0.0.1').getValue()
  19. # Build the URL from the server hostname and port. If your server
  20. # uses another protocol then http you should change it accordingly.
  21. # Make sure to pass the connectMethod to the ClientRepository.__init__
  22. # call too. Available connection methods are:
  23. # self.CM_HTTP, self.CM_NET and self.CM_NATIVE
  24. self.url = URLSpec('http://{}:{}'.format(hostname, tcpPort))
  25. # Attempt a connection to the server
  26. self.connect([self.url],
  27. successCallback = self.connectSuccess,
  28. failureCallback = self.connectFailure)
  29. def lostConnection(self):
  30. """ This should be overridden by a derived class to handle an
  31. unexpectedly lost connection to the gameserver. """
  32. # Handle the disconnection from the server. This can be a reconnect,
  33. # simply exiting the application or anything else.
  34. exit()
  35. def connectFailure(self, statusCode, statusString):
  36. """ Something went wrong """
  37. exit()
  38. def connectSuccess(self):
  39. """ Successfully connected. But we still can't really do
  40. anything until we've got the doID range. """
  41. # Make sure we have interest in the by the AIRepository defined
  42. # TimeManager zone, so we always see it even if we switch to
  43. # another zone.
  44. self.setInterestZones([1])
  45. # We must wait for the TimeManager to be fully created and
  46. # synced before we can enter another zone and wait for the
  47. # game object. The uniqueName is important that we get the
  48. # correct, our sync message from the TimeManager and not
  49. # accidentaly a message from another client
  50. self.acceptOnce(self.uniqueName('gotTimeSync'), self.syncReady)
  51. def syncReady(self):
  52. """ Now we've got the TimeManager manifested, and we're in
  53. sync with the server time. Now we can enter the world. Check
  54. to see if we've received our doIdBase yet. """
  55. # This method checks whether we actually have a valid doID range
  56. # to create distributed objects yet
  57. if self.haveCreateAuthority():
  58. # we already have one
  59. self.gotCreateReady()
  60. else:
  61. # Not yet, keep waiting a bit longer.
  62. self.accept(self.uniqueName('createReady'), self.gotCreateReady)
  63. def gotCreateReady(self):
  64. """ Ready to enter the world. Expand our interest to include
  65. any other zones """
  66. # This method checks whether we actually have a valid doID range
  67. # to create distributed objects yet
  68. if not self.haveCreateAuthority():
  69. # Not ready yet.
  70. return
  71. # we are ready now, so ignore further createReady events
  72. self.ignore(self.uniqueName('createReady'))
  73. self.join()
  74. print("Client Ready")
  75. def join(self):
  76. """ Join a game/room/whatever """
  77. # set our intersted zones to let the client see all distributed obects
  78. # in those zones
  79. self.setInterestZones([1, 2])
  80. base.messenger.send('client-joined')
  81. print("Joined")