Oops, I nearly forgot, I must share my new found knowledge
. So here is the entire code that I have so far:
# MouseControls.py
# Move the mouse pointer to the edge of the screen to rotate the camera.
# The Left and Right arrows also rotate the camera. The Mouse Wheel zooms the
# camera in and out. The Up and Down arrows also zoom the camera in and out.
# Move the player with the W, A, S, D keys.
import direct.directbase.DirectStart # Start Panda
from pandac.PandaModules import* # Import the Panda Modules
from direct.showbase.DirectObject import DirectObject # To listen for Events
from direct.task import Task # To use Tasks
from direct.actor import Actor # To use animated Actors
from direct.interval.IntervalGlobal import * # To use Intervals
import math # To use math (sin, cos..etc)
import sys
class Controls(DirectObject):
#Constructor
def __init__(self):
base.disableMouse() # Disable default camera.
self.speed = .10 # Controls speed of camera rotation and zoom.
self.loadModels()
self.setupAnimations()
# Setup key controls
self.accept("escape", sys.exit)
self.accept("arrow_left", self.cameraTurn,[-1])
self.accept("arrow_right", self.cameraTurn,[1])
self.accept("arrow_up", self.cameraZoom,[-1])
self.accept("arrow_down", self.cameraZoom,[1])
self.accept("wheel_up", self.cameraZoom,[-1])
self.accept("wheel_down", self.cameraZoom,[1])
self.acceptOnce("w", self.forward)
self.acceptOnce("s", self.backward)
self.acceptOnce("a", self.turn,[-1])
self.acceptOnce("d", self.turn,[1])
self.accept("w-up",self.stopForward)
self.accept("s-up",self.stopBackward)
self.accept("a-up",self.stopTurn)
self.accept("d-up",self.stopTurn)
taskMgr.add(self.mousecamTask, "mousecamTask")
# end __init__
# Define a task to monitor the position of the mouse pointer & rotate
# the camera when the mouse pointer moves to the edges of the screen.
def mousecamTask(self,task):
# Check if the mouse is available
if not base.mouseWatcherNode.hasMouse():
return Task.cont
# Get the relative mouse position, its always between 1 and -1
mpos = base.mouseWatcherNode.getMouse()
if mpos.getX() > 0.99:
self.cameraTurn(1)
elif mpos.getX() < -0.99:
self.cameraTurn(-1)
return Task.cont
# end mousecamTask
def loadModels(self):
# Load the player and its animations
self.player = Actor.Actor("MODELS/ralph",{"walk":"MODELS/ralph-walk"})
self.player.reparentTo(render) # Make it display/render on the screen.
self.player.setScale(.005)
self.player.setPos(0, 0, 0) # Position it at the center of the world.
# Load an environment
self.environ = loader.loadModel("MODELS/env")
self.environ.reparentTo(render)
self.environ.setPos(0, 0, 0)
#Create a camera dummy node
self.camera_dummy_node = render.attachNewNode("camera_dummy_node")
#Position the camera dummy node.
self.camera_dummy_node.setPos( 0, 0, 0)
# Make it view from behind the player.
self.camera_dummy_node.setHpr(180, 0, 0)
# Attach the camera dummy node to the player.
self.camera_dummy_node.reparentTo(self.player)
# Attach the camera to the dummy node.
camera.reparentTo(self.camera_dummy_node)
# Position the camera
camera.setPos(0, -30, 7) # X = left & right, Y = zoom, Z = Up & down.
camera.setHpr(0, -15, 0) # Heading, pitch, roll.
camera.lookAt(self.player) # Make the camera follow the player.
# end loadModels
# Define the CameraTurn function.
def cameraTurn(self,dir):
self.camTurn = LerpHprInterval(self.camera_dummy_node, self.speed, Point3(self.camera_dummy_node.getH()-(10*dir), 0, 0))
self.camTurn.start()
# end cameraTurn
# Define the cameraZoom function.
def cameraZoom(self,dir):
self.camZoom = LerpPosInterval(camera, self.speed, Point3(camera.getX(), camera.getY()-(2*dir), camera.getZ()))
self.camZoom.start()
# end cameraZoom
# Define the setupAnimations function
def setupAnimations(self):
self.playerWalk = self.player.actorInterval("walk")
self.playerBack = self.player.actorInterval("walk")
self.playerTurn = self.player.actorInterval("walk")
# end setupIntervals
# Define the forward function
def forward(self):
taskMgr.add(self.forwardTask,"forwardTask")
self.playerWalk.loop() # Play the actor's animations
# end forward
# Define the forwardTask
def forwardTask(self,task):
speed = 0.05 # controls how far the actor moves
dt = globalClock.getDt()
dist = speed*dt
angle = self.player.getH()*math.pi/180
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
self.player.setPos(Vec3(self.player.getX()+dx,self.player.getY()+dy,0))
return Task.cont
# end forwardTask
# Define the stopForward function
def stopForward(self):
taskMgr.remove("forwardTask")
self.playerWalk.pause()
self.player.pose("walk",17)
self.acceptOnce("w",self.forward)
#end stopForward
# Define the backward function
def backward(self):
taskMgr.add(self.backwardTask,"backwardTask")
self.playerBack.loop()
# end backward
# Define the backwardTask
def backwardTask(self,task):
speed = 0.05 # controls how far the actor moves
dt = globalClock.getDt()
dist = speed*dt
angle = self.player.getH()*math.pi/180
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
self.player.setPos(Vec3(self.player.getX()-dx,self.player.getY()-dy,0))
return Task.cont
# end backwardTask
# Define the stopBackward function
def stopBackward(self):
taskMgr.remove("backwardTask")
self.playerBack.pause()
self.acceptOnce("s",self.backward)
#end stopForward
# Define the turn function
def turn(self,dir):
taskMgr.add(self.turnTask, "turnTask",extraArgs =[dir])
self.playerTurn.loop()
self.ignore("a")
self.ignore("d")
#end turn
# Define the turnTask
def turnTask(self,dir):
speed = 50.0
dt = globalClock.getDt()
angle = dir*speed*dt
self.player.setH(self.player.getH()-angle)
return Task.cont
# end turnTask
# Define the stopTurn function
def stopTurn(self):
taskMgr.remove("turnTask")
self.playerTurn.pause()
self.acceptOnce("a",self.turn,[-1])
self.acceptOnce("d",self.turn,[1])
#end stopTurn
# end class Controls
c = Controls()
run()
I hope it helps other newbies who are trying to write their own game controls code
.
Cheers