I tried to strip most of the unnecessary code. This is my gun class with a bullet class underneath. My gun class puts bullet instances into a list and then removes them when the bullet collides or expires its range value.
My problem is that, although the bullets are removed from the list, the bullet graphic still won’t disappear off screen. I load my bullet geom in the parent Buster class, so maybe that’s why. However, I don’t think its good to reload new geom. every time a shot is fired. Is there another way?
from direct.showbase.DirectObject import DirectObject
class Buster(DirectObject):
def __init__(self):
self.properties=[1,3,1,1,1]
self.measureLength=0
self.Melody=[]
self.framesNotPlayed=0
self.index=0
self.busterShotModel = loader.loadModel('models/BusterShot')
self.shotList=[]
self.createMelody()
print self.Melody
taskMgr.add(self.update, 'update Buster')
def createMelody(self):
note=[True]
self.Melody=[True]
notelen=1
self.measureLength=1
invRPD=self.RPDrng[1]+1-self.properties[3]
# CREATE NOTE
if invRPD>0:
for i in range(1,invRPD*7):
note.append(False)
notelen=len(note)
self.measureLength=self.properties[1]*notelen-1
# CREATE MEASURE
j=0
for i in range(0,self.measureLength):
j+=1
if j>=notelen:j=0
self.Melody.append(note[j])
# ENERGY APPEND
if self.properties[1]<self.ENGrng[1]: #MAXENG=10
for i in range(0,int(self.properties[1]*15/self.properties[3])):
self.Melody.append(False)
self.Melody.append(False)
def shootBuster(self, Gun, Target):
self.framesNotPlayed=0
try:
if self.Melody[self.index]:
self.shotList.append(BusterShot(self.busterShotModel, Gun, Target, self.properties))
self.index+=1
except: self.index=0
def update(self, Task):
self.framesNotPlayed+=1
if self.framesNotPlayed>self.measureLength/self.properties[1]:
self.index=0
for b in self.shotList:
if b.hasCollided:
self.shotList.remove(b) #shot removed from list, but bullet is still there!
print 'shot removed!'
return Task.cont
class BusterShot(object):
def __init__(self, model, Gun, Target, properties):
self.node=model.copyTo(Gun)
self.node.lookAt(Target)
self.node.wrtReparentTo(render)
self.deltaVel = self.node.getQuat().getForward() *.36
self.hasCollided=False
self.SPC=properties[4]
self.RNG=properties[2]
self.degree=0
taskMgr.add(self.update, 'update BusterShot')
def update(self, Task):
self.node.setPos(self.node.getPos() + self.deltaVel)
self.node.setHpr(self.degree,0,0)
if self.degree>360:self.degree=0
self.degree+=2.5**self.SPC
if Task.time<self.RNG:
return Task.cont
else:
self.hasCollided=True
print 'shot collided!'