def loadPlanet(self): self.planet = loader.loadModel('planet.bam') self.planet.setPos(tupleToVec3(Game.PLANET_POSITION)) self.planet.setScale(20) self.planet.reparentTo(render) self.planetCollGeom = OdeSphereGeom(20)
def loadPlanet(self): self.planet = loader.loadModel('planet.bam') self.planet.setPos( tupleToVec3(Game.PLANET_POSITION) ) self.planet.setScale(20) self.planet.reparentTo(render) self.planetCollGeom = OdeSphereGeom(20)
def shoot(self): # TODO: add proper unit tests! angle = self.heading * math.pi / 180.0 headingX = math.cos(angle) headingY = math.sin(angle) offset = Vec3(headingX, headingY, 0) * Ship.BULLET_OFFSET shipPos = self.getPos() bulletPos = (offset[0] + shipPos[0], offset[1] + shipPos[1], offset[2]) bulletVisual = loader.loadModel("bullet.bam") bulletVisual.setPos(tupleToVec3(bulletPos)) bulletVisual.setHpr(tupleToVec3((self.heading + 90, 180))) bulletVisual.setScale(1.5) bulletVisual.reparentTo(self.bulletParent) # Create physics for bullet collisionSphere = OdeSphereGeom(1.5) collisionSphere.setCategoryBits(BitMask32(0xffffffff)) collisionSphere.setCollideBits(BitMask32(0xffffffff)) collisionSphere.setPosition(bulletPos[0], bulletPos[1], bulletPos[2]) shipVel = self.getVel() bullet = { 'vel': (headingX * Ship.BULLET_SPEED + shipVel[0] / Ship.BULLET_SHIP_SPEED_CORRELATION, headingY * Ship.BULLET_SPEED + shipVel[1] / Ship.BULLET_SHIP_SPEED_CORRELATION), 'visual': bulletVisual, 'physical': collisionSphere, 'isAlive': True, 'timeToLive': Ship.BULLET_MAX_LIFE_TIME } self.bullets.append(bullet) self.shootingSound.play()
def createVisualNode(self, pos=(0, 0)): # modelNode is the actualy ship model modelNode = loader.loadModel("indicator.bam") # visualNode is the node we operate on to move and rotate the ship visualNode = NodePath('Ship: ' + self.name) visualNode.setPos(tupleToVec3(pos)) visualNode.setHpr(Vec3(0, -90, 90)) # TODO: add scale parameter to this or some other aggregator class visualNode.setScale(1) # Reparent the actual modelNode to the visualNode modelNode.reparentTo(visualNode) # Offset the model node relative to the parent modelNode.setPos(tripleToVec3(Ship.MODEL_ROTATION_OFFSET)) visualNode.reparentTo(render) return visualNode
def update(self, deltaTime): """@param deltaTime float, how many seconds have passed since last tick""" # TODO: refactor the updating code into different methods # Update the bullets # TODO: Add test for this in testUpdate! for bullet in self.bullets: bullet['timeToLive'] -= 1 if bullet['timeToLive'] <= 0: bullet['isAlive'] = False if not bullet['isAlive']: self.destroyBullet(bullet) continue pos = bullet['visual'].getPos() bulletPos = Vec3(bullet['vel'][0] * deltaTime + pos[0], bullet['vel'][1] * deltaTime + pos[1], 0) bullet['visual'].setPos(bulletPos) bullet['physical'].setPosition(bulletPos) # If the ship is not alive anymore, we don't move it if not self.isAlive: return # update the heading. Must be done before position updating! if self.rotateLeft: self.heading = self.heading + Ship.ROTATION_SPEED * deltaTime elif self.rotateRight: self.heading = self.heading - Ship.ROTATION_SPEED * deltaTime for c in self.collisions: self.collisionHandler(self, c) # update position gainedSpeedScalar = self.acc * deltaTime * Ship.ACC_COEFFICIENT # convert degrees to radians angle = self.heading * math.pi / 180.0 #correction = math.pi / 2 deltaVelX = gainedSpeedScalar * math.cos(angle) deltaVelY = gainedSpeedScalar * math.sin(angle) for f in self.forces: deltaVelX += f[0] deltaVelY += f[1] self.forces = [] self.vel = (self.vel[0] + deltaVelX, self.vel[1] + deltaVelY) # Limit the ship's speed to Ship.SPEED_MAX self.limitVelocity() deltaPosX = deltaTime * self.vel[0] deltaPosY = deltaTime * self.vel[1] newPosX = self.pos[0] + deltaPosX newPosY = self.pos[1] + deltaPosY self.pos = (newPosX, newPosY) # Rotate the visual representation of the ship self.visualNode.setH(self.heading) # Move the actual visual representation self.visualNode.setPos(tupleToVec3(self.pos)) self.collisionSphere.setPosition(self.pos[0], self.pos[1], 0)
def testTupleToVec3(self): self.failUnlessEqual( tupleToVec3( (2, 4) ), Vec3(2, 4, 0) )
def testTupleToVec3(self): self.failUnlessEqual(tupleToVec3((2, 4)), Vec3(2, 4, 0))