TwoDWalker.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. TwoDWalker.py is for controlling the avatars in a 2D scroller game environment.
  3. """
  4. from .GravityWalker import *
  5. from direct.showbase.MessengerGlobal import messenger
  6. from panda3d.core import ConfigVariableBool
  7. class TwoDWalker(GravityWalker):
  8. """
  9. The TwoDWalker is primarily for a 2D Scroller game environment. Eg - Toon Blitz minigame.
  10. TODO: This class is still work in progress.
  11. Currently Toon Blitz is using this only for jumping.
  12. Moving the Toon left to right is handled by toontown/src/minigame/TwoDDrive.py.
  13. I eventually want this class to control all the 2 D movements, possibly with a
  14. customizable input list.
  15. """
  16. notify = directNotify.newCategory("TwoDWalker")
  17. wantDebugIndicator = ConfigVariableBool('want-avatar-physics-indicator', False)
  18. wantFloorSphere = ConfigVariableBool('want-floor-sphere', False)
  19. earlyEventSphere = ConfigVariableBool('early-event-sphere', False)
  20. # special methods
  21. def __init__(self, gravity = -32.1740, standableGround=0.707,
  22. hardLandingForce=16.0):
  23. assert self.notify.debugStateCall(self)
  24. self.notify.debug('Constructing TwoDWalker')
  25. GravityWalker.__init__(self)
  26. def handleAvatarControls(self, task):
  27. """
  28. Check on the arrow keys and update the avatar.
  29. """
  30. # get the button states:
  31. jump = inputState.isSet("forward")
  32. if self.lifter.isOnGround():
  33. if self.isAirborne:
  34. self.isAirborne = 0
  35. assert self.debugPrint("isAirborne 0 due to isOnGround() true")
  36. impact = self.lifter.getImpactVelocity()
  37. messenger.send("jumpLand")
  38. assert self.isAirborne == 0
  39. self.priorParent = Vec3.zero()
  40. else:
  41. if self.isAirborne == 0:
  42. assert self.debugPrint("isAirborne 1 due to isOnGround() false")
  43. self.isAirborne = 1
  44. return Task.cont
  45. def jumpPressed(self):
  46. """This function should be called from TwoDDrive when the jump key is pressed."""
  47. if self.lifter.isOnGround():
  48. if self.isAirborne == 0:
  49. if self.mayJump:
  50. # The jump button is down and we're close enough to the ground to jump.
  51. self.lifter.addVelocity(self.avatarControlJumpForce)
  52. messenger.send("jumpStart")
  53. self.isAirborne = 1
  54. assert self.debugPrint("isAirborne 1 due to jump")