mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-03 18:31:55 -04:00
New TwoDWalker controls for the controlManager. Use this for any 2D Scroller type game, if you need jump.
This commit is contained in:
parent
976e367e52
commit
003a3bf2f5
@ -28,125 +28,34 @@ class TwoDWalker(GravityWalker):
|
|||||||
def handleAvatarControls(self, task):
|
def handleAvatarControls(self, task):
|
||||||
"""
|
"""
|
||||||
Check on the arrow keys and update the avatar.
|
Check on the arrow keys and update the avatar.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# get the button states:
|
# get the button states:
|
||||||
run = inputState.isSet("run")
|
jump = inputState.isSet("forward")
|
||||||
forward = inputState.isSet("forward")
|
|
||||||
reverse = inputState.isSet("reverse")
|
|
||||||
turnLeft = inputState.isSet("turnLeft")
|
|
||||||
turnRight = inputState.isSet("turnRight")
|
|
||||||
slideLeft = inputState.isSet("slideLeft")
|
|
||||||
slideRight = inputState.isSet("slideRight")
|
|
||||||
jump = inputState.isSet("forward")
|
|
||||||
|
|
||||||
# Check for Auto-Run
|
|
||||||
if 'localAvatar' in __builtins__:
|
|
||||||
if base.localAvatar and base.localAvatar.getAutoRun():
|
|
||||||
forward = 1
|
|
||||||
reverse = 0
|
|
||||||
|
|
||||||
# Determine what the speeds are based on the buttons:
|
|
||||||
self.speed=(forward and self.avatarControlForwardSpeed or
|
|
||||||
reverse and -self.avatarControlReverseSpeed)
|
|
||||||
# Slide speed is a scaled down version of forward speed
|
|
||||||
# Note: you can multiply a factor in here if you want slide to
|
|
||||||
# be slower than normal walk/run. Let's try full speed.
|
|
||||||
#self.slideSpeed=(slideLeft and -self.avatarControlForwardSpeed*0.75 or
|
|
||||||
# slideRight and self.avatarControlForwardSpeed*0.75)
|
|
||||||
self.slideSpeed=(reverse and slideLeft and -self.avatarControlReverseSpeed*0.75 or
|
|
||||||
reverse and slideRight and self.avatarControlReverseSpeed*0.75 or
|
|
||||||
slideLeft and -self.avatarControlForwardSpeed*0.75 or
|
|
||||||
slideRight and self.avatarControlForwardSpeed*0.75)
|
|
||||||
self.rotationSpeed=not (slideLeft or slideRight) and (
|
|
||||||
(turnLeft and self.avatarControlRotateSpeed) or
|
|
||||||
(turnRight and -self.avatarControlRotateSpeed))
|
|
||||||
|
|
||||||
debugRunning = inputState.isSet("debugRunning")
|
|
||||||
if(debugRunning):
|
|
||||||
self.speed*=base.debugRunningMultiplier
|
|
||||||
self.slideSpeed*=base.debugRunningMultiplier
|
|
||||||
self.rotationSpeed*=1.25
|
|
||||||
|
|
||||||
if self.needToDeltaPos:
|
|
||||||
self.setPriorParentVector()
|
|
||||||
self.needToDeltaPos = 0
|
|
||||||
if self.wantDebugIndicator:
|
|
||||||
self.displayDebugInfo()
|
|
||||||
if self.lifter.isOnGround():
|
if self.lifter.isOnGround():
|
||||||
if self.isAirborne:
|
if self.isAirborne:
|
||||||
self.isAirborne = 0
|
self.isAirborne = 0
|
||||||
assert self.debugPrint("isAirborne 0 due to isOnGround() true")
|
assert self.debugPrint("isAirborne 0 due to isOnGround() true")
|
||||||
impact = self.lifter.getImpactVelocity()
|
impact = self.lifter.getImpactVelocity()
|
||||||
if impact < -30.0:
|
messenger.send("jumpLand")
|
||||||
messenger.send("jumpHardLand")
|
if impact < -5.0:
|
||||||
self.startJumpDelay(0.3)
|
self.startJumpDelay(0.2)
|
||||||
else:
|
# else, ignore the little potholes.
|
||||||
messenger.send("jumpLand")
|
|
||||||
if impact < -5.0:
|
|
||||||
self.startJumpDelay(0.2)
|
|
||||||
# else, ignore the little potholes.
|
|
||||||
assert self.isAirborne == 0
|
assert self.isAirborne == 0
|
||||||
self.priorParent = Vec3.zero()
|
self.priorParent = Vec3.zero()
|
||||||
if jump and self.mayJump:
|
|
||||||
# The jump button is down and we're close
|
|
||||||
# enough to the ground to jump.
|
|
||||||
self.lifter.addVelocity(self.avatarControlJumpForce)
|
|
||||||
messenger.send("jumpStart")
|
|
||||||
self.isAirborne = 1
|
|
||||||
assert self.debugPrint("isAirborne 1 due to jump")
|
|
||||||
else:
|
else:
|
||||||
if self.isAirborne == 0:
|
if self.isAirborne == 0:
|
||||||
assert self.debugPrint("isAirborne 1 due to isOnGround() false")
|
assert self.debugPrint("isAirborne 1 due to isOnGround() false")
|
||||||
self.isAirborne = 1
|
self.isAirborne = 1
|
||||||
|
|
||||||
self.__oldPosDelta = self.avatarNodePath.getPosDelta(render)
|
return Task.cont
|
||||||
# How far did we move based on the amount of time elapsed?
|
|
||||||
self.__oldDt = ClockObject.getGlobalClock().getDt()
|
def jumpPressed(self):
|
||||||
dt=self.__oldDt
|
"""This function should be called from TwoDDrive when the jump key is pressed."""
|
||||||
|
if self.lifter.isOnGround():
|
||||||
# Check to see if we're moving at all:
|
if self.isAirborne == 0:
|
||||||
self.moving = self.speed or self.slideSpeed or self.rotationSpeed or (self.priorParent!=Vec3.zero())
|
if self.mayJump:
|
||||||
'''
|
# The jump button is down and we're close enough to the ground to jump.
|
||||||
if self.moving:
|
self.lifter.addVelocity(self.avatarControlJumpForce)
|
||||||
distance = dt * self.speed
|
messenger.send("jumpStart")
|
||||||
slideDistance = dt * self.slideSpeed
|
self.isAirborne = 1
|
||||||
rotation = dt * self.rotationSpeed
|
assert self.debugPrint("isAirborne 1 due to jump")
|
||||||
|
|
||||||
# Take a step in the direction of our previous heading.
|
|
||||||
if distance or slideDistance or self.priorParent != Vec3.zero():
|
|
||||||
# rotMat is the rotation matrix corresponding to
|
|
||||||
# our previous heading.
|
|
||||||
rotMat=Mat3.rotateMatNormaxis(self.avatarNodePath.getH(), Vec3.up())
|
|
||||||
if self.isAirborne:
|
|
||||||
forward = Vec3.forward()
|
|
||||||
else:
|
|
||||||
contact = self.lifter.getContactNormal()
|
|
||||||
forward = contact.cross(Vec3.right())
|
|
||||||
# Consider commenting out this normalize. If you do so
|
|
||||||
# then going up and down slops is a touch slower and
|
|
||||||
# steeper terrain can cut the movement in half. Without
|
|
||||||
# the normalize the movement is slowed by the cosine of
|
|
||||||
# the slope (i.e. it is multiplied by the sign as a
|
|
||||||
# side effect of the cross product above).
|
|
||||||
forward.normalize()
|
|
||||||
self.vel=Vec3(forward * distance)
|
|
||||||
if slideDistance:
|
|
||||||
if self.isAirborne:
|
|
||||||
right = Vec3.right()
|
|
||||||
else:
|
|
||||||
right = forward.cross(contact)
|
|
||||||
# See note above for forward.normalize()
|
|
||||||
right.normalize()
|
|
||||||
self.vel=Vec3(self.vel + (right * slideDistance))
|
|
||||||
self.vel=Vec3(rotMat.xform(self.vel))
|
|
||||||
step=self.vel + (self.priorParent * dt)
|
|
||||||
self.avatarNodePath.setFluidPos(Point3(
|
|
||||||
self.avatarNodePath.getPos()+step))
|
|
||||||
self.avatarNodePath.setH(self.avatarNodePath.getH()+rotation)
|
|
||||||
else:
|
|
||||||
self.vel.set(0.0, 0.0, 0.0)
|
|
||||||
'''
|
|
||||||
if self.moving or jump:
|
|
||||||
messenger.send("avatarMoving")
|
|
||||||
return Task.cont
|
|
Loading…
x
Reference in New Issue
Block a user