2
0

ClientRepository.py 4.4 KB

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