Ejemplo n.º 1
0
    def __emitShell(self):
        def __shellThink(shell, task):
            if task.time > 3.0:
                base.physicsWorld.remove(shell.node())
                shell.removeNode()
                return task.done

            if not hasattr(task, 'didHitNoise'):
                task.didHitNoise = False

            if not task.didHitNoise:
                contact = base.physicsWorld.contactTest(shell.node())
                if contact.getNumContacts() > 0:
                    task.didHitNoise = True
                    hitNoise = base.loadSfxOnNode(
                        self.ShellContactSoundPath.format(
                            random.randint(*self.ShellContactSoundRange)),
                        shell)
                    hitNoise.play()

            return task.cont

        from panda3d.bullet import BulletCylinderShape, BulletRigidBodyNode, ZUp
        scale = 0.75
        shape = BulletCylinderShape(0.07 * scale, 0.47 * scale, ZUp)
        rbnode = BulletRigidBodyNode('shellrbnode')
        rbnode.setMass(1.0)
        rbnode.addShape(shape)
        rbnode.setCcdMotionThreshold(1e-7)
        rbnode.setCcdSweptSphereRadius(0.1)
        rbnp = render.attachNewNode(rbnode)
        mdl = loader.loadModel(self.ShellPath)
        mdl.reparentTo(rbnp)
        mdl.setScale(0.3 * scale, 0.7 * scale, 0.3 * scale)
        mdl.setP(90)
        mdl.setTransparency(True, 1)

        rbnp.setPos(camera, (1, 2, -0.5))
        rbnp.setHpr(camera, (0, -90, 0))

        localEjectDir = Vec3(1, 0, 0.3)
        rbnode.applyCentralImpulse(
            camera.getQuat(render).xform(localEjectDir) * 7)
        base.physicsWorld.attach(rbnode)

        taskMgr.add(__shellThink,
                    'shellThink',
                    extraArgs=[rbnp],
                    appendTask=True)