|
@@ -0,0 +1,75 @@
|
|
|
|
|
+"""
|
|
|
|
|
+GhostWalker.py is for avatars.
|
|
|
|
|
+
|
|
|
|
|
+A walker control such as this one provides:
|
|
|
|
|
+ - creation of the collision nodes
|
|
|
|
|
+ - handling the keyboard and mouse input for avatar movement
|
|
|
|
|
+ - moving the avatar
|
|
|
|
|
+
|
|
|
|
|
+it does not:
|
|
|
|
|
+ - play sounds
|
|
|
|
|
+ - play animations
|
|
|
|
|
+
|
|
|
|
|
+although it does send messeges that allow a listener to play sounds or
|
|
|
|
|
+animations based on walker events.
|
|
|
|
|
+"""
|
|
|
|
|
+
|
|
|
|
|
+from ShowBaseGlobal import *
|
|
|
|
|
+
|
|
|
|
|
+import DirectNotifyGlobal
|
|
|
|
|
+import NonPhysicsWalker
|
|
|
|
|
+
|
|
|
|
|
+class GhostWalker(NonPhysicsWalker.NonPhysicsWalker):
|
|
|
|
|
+
|
|
|
|
|
+ notify = DirectNotifyGlobal.directNotify.newCategory("GhostWalker")
|
|
|
|
|
+
|
|
|
|
|
+ def handleAvatarControls(self, task):
|
|
|
|
|
+ """
|
|
|
|
|
+ Check on the arrow keys and update the avatar.
|
|
|
|
|
+ """
|
|
|
|
|
+ # get the button states:
|
|
|
|
|
+ forward = inputState.isSet("forward")
|
|
|
|
|
+ reverse = inputState.isSet("reverse")
|
|
|
|
|
+ turnLeft = inputState.isSet("turnLeft")
|
|
|
|
|
+ turnRight = inputState.isSet("turnRight")
|
|
|
|
|
+ # Ghosts slide instead of jump:
|
|
|
|
|
+ slide = inputState.isSet("jump")
|
|
|
|
|
+ # Determine what the speeds are based on the buttons:
|
|
|
|
|
+ self.speed=(forward and self.avatarControlForwardSpeed or
|
|
|
|
|
+ reverse and -self.avatarControlReverseSpeed)
|
|
|
|
|
+ # Should fSlide be renamed slideButton?
|
|
|
|
|
+ self.slideSpeed=slide and (
|
|
|
|
|
+ (turnLeft and -self.avatarControlForwardSpeed) or
|
|
|
|
|
+ (turnRight and self.avatarControlForwardSpeed))
|
|
|
|
|
+ self.rotationSpeed=not slide and (
|
|
|
|
|
+ (turnLeft and self.avatarControlRotateSpeed) or
|
|
|
|
|
+ (turnRight and -self.avatarControlRotateSpeed))
|
|
|
|
|
+
|
|
|
|
|
+ # How far did we move based on the amount of time elapsed?
|
|
|
|
|
+ dt=min(ClockObject.getGlobalClock().getDt(), 0.1)
|
|
|
|
|
+ # Check to see if we're moving at all:
|
|
|
|
|
+ if self.speed or self.slideSpeed or self.rotationSpeed:
|
|
|
|
|
+ if self.stopThisFrame:
|
|
|
|
|
+ distance = 0.0
|
|
|
|
|
+ slideDistance = 0.0
|
|
|
|
|
+ rotation = 0.0
|
|
|
|
|
+ self.stopThisFrame = 0
|
|
|
|
|
+ else:
|
|
|
|
|
+ distance = dt * self.speed
|
|
|
|
|
+ slideDistance = dt * self.slideSpeed
|
|
|
|
|
+ rotation = dt * self.rotationSpeed
|
|
|
|
|
+
|
|
|
|
|
+ # Take a step in the direction of our previous heading.
|
|
|
|
|
+ self.vel=Vec3(Vec3.forward() * distance +
|
|
|
|
|
+ Vec3.right() * slideDistance)
|
|
|
|
|
+ if self.vel != Vec3.zero():
|
|
|
|
|
+ # rotMat is the rotation matrix corresponding to
|
|
|
|
|
+ # our previous heading.
|
|
|
|
|
+ rotMat=Mat3.rotateMatNormaxis(self.avatarNodePath.getH(), Vec3.up())
|
|
|
|
|
+ step=rotMat.xform(self.vel)
|
|
|
|
|
+ self.avatarNodePath.setFluidPos(Point3(self.avatarNodePath.getPos()+step))
|
|
|
|
|
+ self.avatarNodePath.setH(self.avatarNodePath.getH()+rotation)
|
|
|
|
|
+ messenger.send("avatarMoving")
|
|
|
|
|
+ else:
|
|
|
|
|
+ self.vel.set(0.0, 0.0, 0.0)
|
|
|
|
|
+ return Task.cont
|