Player Model – Pyro
load the player’s model
keyboard controls work-- just can’t bind animations to them.
PYRO CLASS - CHARACTER CLASS
from pandac.PandaModules import * # basic Panda modules
from direct.showbase.DirectObject import DirectObject # for Direct Objects
from direct.showbase import DirectObject
from direct.showbase.ShowBaseGlobal import *
from direct.actor import Actor # Actor class
from direct.interval.IntervalGlobal import * # Intervals (Parallel, Sequence, etc)
from direct.task import Task # for task contants
import other Platformer modules
import ClassGlobals
import level1
import miscallaneous classes
import math
import random
#########################################
Player Class
#########################################
load the main model
class PyroClass(DirectObject):
# constructor
def init(self, classNum):
#-----------------------------------------------------------------
#controls is a dictionary of what control keys are currently pressed
#The keyOn/keyOff events update this, and the task queries it for input
self.controls = {"walk":False,"run":False} # boardkeys are up
#A dictionary to map keyboard keys to their controls. To distinguish
#keyboard keys from dictionary keys in the comments "keyboard keys"
#will be called "'boardkeys" for short.
self.keybindings = {"walk":"arrow_up", "run":"a"} # author's control array
self.bindKeys() # keys dictionary
# load the model
self.Pyro()
# setup the Intervals
self.setupIntervals()
#-----------------------------------------------------------------
# load a model
def Pyro(self):
print ‘player has loaded’
self.pyro = Actor.Actor(“models/PyroMesh”,{ # array to contain the Actor animations
“idle”:“models/PyroIdle”,
“walk”:“models/PyroWalk”,
“run”:“models/PyroRun”,"})
self.pyro.reparentTo(render) # make the character renderable
self.pyro.setScale(0.75) # 1/4 of the original size
self.pyro.setPos(0,0,0) # at origin
self.pyro.setH(180) # set Heading to 180 degrees due to Blender translation
self.pyro.setP(-90) # pitch 90 degrees
print ‘player is idle’
self.pyro.loop(“idle”) # loop the Idle animation
# end loadPyro
#-----------------------------------------------------------------
def bindKeys(self):
#bindKeys maps the controls to the 'boardkeys that activate them
print ‘bindKeys activated’
for walk,arrow_up in self.keybindings.items(): #iterate through the dictionary
self.accept(“arrow_up”,self.Walk_On,[walk,arrow_up]) #event v activates method __keyOn
self.accept(“arrow_up-up”,self.Walk_Off,[walk,arrow_up]) #[k,v] are passed to method
#-----------------------------------------------------------------
# walk animation On(will implement soon)
def Walk_On(self,walk,arrow_up): #the c is the name of the control. As a string.
print ‘walks is true’
self.controls[walk] = True # arrow_up is True; Boolean statement
print ‘boardkeys ignored’
self.ignore(arrow_up) #ignore keyboard auto repeats
print ‘anim commences’
self.acceptOnce(arrow_up,self.walk())
# walk animation Off
def Walk_Off(self,walk,arrow_up): #c is the control name; b is the 'boardkey name.
print 'walk is false'
self.controls[walk] = False # arrow_up is False
print 'keypresses are not ignored'
self.accept(arrow_up,self.Walk_Off,[walk,arrow_up]) #stop ignoring that keypress.
print 'anim stops'
self.accept("arrow_up-up",self.stopWalk())
#-----------------------------------------------------------------
def setupIntervals(self):
print ‘walk loads’
self.pyroWalk = self.pyro.actorInterval(“walk”, loop = 1)
print ‘idle loads’
self.pyroIdle = self.pyro.actorInterval(“idle”)
print ‘run loads’
self.pyroRun = self.pyro.actorInterval(“run”)
# end setupIntervals
#-----------------------------------------------------------------
def walk(self):
print ‘player walks anim’
taskMgr.add(self.walkTask,‘walkTask’)
print ‘walk loop’
self.pyroWalk.loop()
# end walk
def stopWalk(self):
print 'player stops walking anim'
taskMgr.remove('walkTask')
print 'walk pauses'
self.pyroWalk.pause()
print 'player is idle'
self.pyro.loop("idle")
# end stopWalk
def walkTask(self,task):
speed = 5.0 # ft/s
dt = globalClock.getDt() # tell the Global Clock to get the Distance and the Time
dist = speed*dt # d = rt
angle = self.pyro.getH()*math.pi/180.0 # remember to add 'self' beforehand!!
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
self.pyro.setPos(Vec3(self.pyro.getX()+dx, self.pyro.getY()+dy,0))
return Task.cont
# end walkTask
#-----------------------------------------------------------------
def run():
print ‘player runs’
taskMgr.add(runTask,‘runTask’)
self.pyroRun.loop()
self.ignore(“a”)
# end run
def stopRun():
taskMgr.remove('runTask')
self.pyroRun.pause()
self.acceptOnce("a",self.run)
self.pyro.loop("idle") # player idles after having stopped running
# end stopRun
def runTask(task):
speed = 15.0 # ft/s
dt = globalClock.getDt()
dist = speed*dt
angle = pyro.getH()*math.pi/180.0
dx = dist*math.sin(angle)
dy = dist*-math.cos(angle)
self.pyro.setPos(Vec3(self.pyro.getX()+dx, self.pyro.getY()+dy,0))
return Task.cont
# end runTask
#-----------------------------------------------------------------