client.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # all imports needed by the engine itself
  2. from direct.showbase.ShowBase import ShowBase
  3. from panda3d.core import KeyboardButton, NodePath, PandaNode
  4. # import our own repositories
  5. from ClientRepository import GameClientRepository
  6. from DistributedSmoothActor import DistributedSmoothActor
  7. # initialize the engine
  8. base = ShowBase()
  9. base.disableMouse()
  10. class Avatar:
  11. def __init__(self, cr):
  12. self.cr = cr
  13. self.ralph = DistributedSmoothActor(self.cr)
  14. self.cr.createDistributedObject(
  15. distObj = self.ralph,
  16. zoneId = 2)
  17. # Create a floater object, which floats 2 units above ralph. We
  18. # use this as a target for the camera to look at.
  19. self.floater = NodePath(PandaNode("floater"))
  20. self.floater.reparentTo(self.ralph)
  21. self.floater.setZ(2.0)
  22. # We will use this for checking if keyboard keys are pressed
  23. self.isDown = base.mouseWatcherNode.isButtonDown
  24. taskMgr.add(self.move, "moveTask")
  25. # Set up the camera
  26. base.camera.setPos(self.ralph.getX(), self.ralph.getY() + 10, 2)
  27. # start the avatar
  28. self.ralph.start()
  29. # Accepts arrow keys to move either the player or the menu cursor,
  30. # Also deals with grid checking and collision detection
  31. def move(self, task):
  32. # Get the time that elapsed since last frame. We multiply this with
  33. # the desired speed in order to find out with which distance to move
  34. # in order to achieve that desired speed.
  35. dt = globalClock.getDt()
  36. # If the camera-left key is pressed, move camera left.
  37. # If the camera-right key is pressed, move camera right.
  38. if self.isDown(KeyboardButton.asciiKey(b"j")):
  39. base.camera.setX(base.camera, -20 * dt)
  40. if self.isDown(KeyboardButton.asciiKey(b"k")):
  41. base.camera.setX(base.camera, +20 * dt)
  42. # If a move-key is pressed, move ralph in the specified direction.
  43. if self.isDown(KeyboardButton.asciiKey(b"a")):
  44. self.ralph.setH(self.ralph.getH() + 300 * dt)
  45. if self.isDown(KeyboardButton.asciiKey(b"d")):
  46. self.ralph.setH(self.ralph.getH() - 300 * dt)
  47. if self.isDown(KeyboardButton.asciiKey(b"w")):
  48. self.ralph.setY(self.ralph, -20 * dt)
  49. if self.isDown(KeyboardButton.asciiKey(b"s")):
  50. self.ralph.setY(self.ralph, +10 * dt)
  51. # update distributed position and rotation
  52. #self.ralph.setDistPos(self.ralph.getX(), self.ralph.getY(), self.ralph.getZ())
  53. #self.ralph.setDistHpr(self.ralph.getH(), self.ralph.getP(), self.ralph.getR())
  54. # If ralph is moving, loop the run animation.
  55. # If he is standing still, stop the animation.
  56. currentAnim = self.ralph.getCurrentAnim()
  57. if self.isDown(KeyboardButton.asciiKey(b"w")):
  58. if currentAnim != "run":
  59. self.ralph.loop("run")
  60. elif self.isDown(KeyboardButton.asciiKey(b"s")):
  61. # Play the walk animation backwards.
  62. if currentAnim != "walk":
  63. self.ralph.loop("walk")
  64. self.ralph.setPlayRate(-1.0, "walk")
  65. elif self.isDown(KeyboardButton.asciiKey(b"a")) or self.isDown(KeyboardButton.asciiKey(b"d")):
  66. if currentAnim != "walk":
  67. self.ralph.loop("walk")
  68. self.ralph.setPlayRate(1.0, "walk")
  69. else:
  70. if currentAnim is not None:
  71. self.ralph.stop()
  72. self.ralph.pose("walk", 5)
  73. self.isMoving = False
  74. # If the camera is too far from ralph, move it closer.
  75. # If the camera is too close to ralph, move it farther.
  76. camvec = self.ralph.getPos() - base.camera.getPos()
  77. camvec.setZ(0)
  78. camdist = camvec.length()
  79. camvec.normalize()
  80. if camdist > 10.0:
  81. base.camera.setPos(base.camera.getPos() + camvec * (camdist - 10))
  82. camdist = 10.0
  83. if camdist < 5.0:
  84. base.camera.setPos(base.camera.getPos() - camvec * (5 - camdist))
  85. camdist = 5.0
  86. # The camera should look in ralph's direction,
  87. # but it should also try to stay horizontal, so look at
  88. # a floater which hovers above ralph's head.
  89. base.camera.lookAt(self.floater)
  90. return task.cont
  91. base.accept("escape", exit)
  92. # initialize the client
  93. client = GameClientRepository()
  94. from direct.gui.OnscreenText import OnscreenText
  95. from panda3d.core import TextNode
  96. # Function to put instructions on the screen.
  97. def addInstructions(pos, msg):
  98. return OnscreenText(text=msg, style=1, fg=(0, 0, 0, 1), shadow=(1, 1, 1, 1),
  99. parent=base.a2dTopLeft, align=TextNode.ALeft,
  100. pos=(0.08, -pos - 0.04), scale=.06)
  101. # Function to put title on the screen.
  102. def addTitle(text):
  103. return OnscreenText(text=text, style=1, pos=(-0.1, 0.09), scale=.08,
  104. parent=base.a2dBottomRight, align=TextNode.ARight,
  105. fg=(1, 1, 1, 1), shadow=(0, 0, 0, 1))
  106. title = addTitle("Panda3D: Tutorial - Distributed Network (NOT CONNECTED)")
  107. inst1 = addInstructions(0.06, "W|A|S|D: Move avatar)")
  108. inst2 = addInstructions(0.12, "esc: Close the client")
  109. inst3 = addInstructions(0.24, "See console output")
  110. def clientJoined():
  111. title["text"] = "Panda3D: Tutorial - Distributed Network (CONNECTED)"
  112. # Setup our avatar
  113. Avatar(client)
  114. base.accept("client-joined", clientJoined)
  115. # start the client
  116. base.run()