def test_collision_handler_pusher_pickle():
    from panda3d.core import CollisionHandlerPusher

    collider1 = NodePath(CollisionNode("collider1"))
    collider2 = NodePath(CollisionNode("collider2"))
    target1 = NodePath("target1")
    target2 = NodePath("target2")

    handler = CollisionHandlerPusher()
    handler.add_again_pattern("again pattern")
    handler.add_collider(collider1, target1)
    handler.add_collider(collider2, target2)
    handler.horizontal = True

    handler = loads(dumps(handler, -1))

    assert tuple(handler.in_patterns) == ()
    assert tuple(handler.again_patterns) == ("again pattern", )
    assert tuple(handler.out_patterns) == ()
    assert not handler.has_center()
    assert handler.horizontal
Exemplo n.º 2
0
class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        #globalClock.setMode(ClockObject.M_limited)
        #globalClock.setFrameRate(60)

        self.disableMouse()

        Common.initialise()
        Common.framework = self

        properties = WindowProperties()
        properties.setSize(1000, 750)
        self.win.requestProperties(properties)

        self.exitFunc = self.cleanup

        render.setShaderAuto()

        self.keyMap = {
            "up" : False,
            "down" : False,
            "left" : False,
            "right" : False,
            "shoot" : False,
            "activateItem" : False,
            "invLeft" : False,
            "invRight" : False
        }

        self.accept("w", self.updateKeyMap, ["up", True])
        self.accept("w-up", self.updateKeyMap, ["up", False])
        self.accept("s", self.updateKeyMap, ["down", True])
        self.accept("s-up", self.updateKeyMap, ["down", False])
        self.accept("a", self.updateKeyMap, ["left", True])
        self.accept("a-up", self.updateKeyMap, ["left", False])
        self.accept("d", self.updateKeyMap, ["right", True])
        self.accept("d-up", self.updateKeyMap, ["right", False])
        self.accept("mouse1", self.updateKeyMap, ["shoot", True])
        self.accept("mouse1-up", self.updateKeyMap, ["shoot", False])
        self.accept("wheel_up", self.onMouseWheel, [1])
        self.accept("wheel_down", self.onMouseWheel, [-1])
        self.accept("space-up", self.interact)
        self.accept("1", self.selectWeapon, [0])
        self.accept("2", self.selectWeapon, [1])

        self.accept("f", self.toggleFrameRateMeter)
        self.showFrameRateMeter = False

        self.pusher = CollisionHandlerPusher()
        self.traverser = CollisionTraverser()
        self.traverser.setRespectPrevTransform(True)

        self.pusher.setHorizontal(True)

        self.pusher.add_in_pattern("%fn-into-%in")
        self.pusher.add_in_pattern("%fn-into")
        self.pusher.add_again_pattern("%fn-again-into")
        #self.accept("trapEnemy-into-wall", self.stopTrap)
        self.accept("projectile-into", self.projectileImpact)
        self.accept("projectile-again-into", self.projectileImpact)
        self.accept("playerWallCollider-into-item", self.itemCollected)
        self.accept("playerWallCollider-into-trigger", self.triggerActivated)

        self.updateTask = taskMgr.add(self.update, "update")

        self.player = None
        self.currentLevel = None

        self.gameOverScreen = DirectDialog(frameSize = (-0.7, 0.7, -0.7, 0.7),
                                           fadeScreen = 0.4,
                                           relief = DGG.FLAT)
        self.gameOverScreen.hide()

        label = DirectLabel(text = "Game Over!",
                            parent = self.gameOverScreen,
                            scale = 0.1,
                            pos = (0, 0, 0.2),
                            #text_font = self.font,
                            relief = None)

        self.finalScoreLabel = DirectLabel(text = "",
                                           parent = self.gameOverScreen,
                                           scale = 0.07,
                                           pos = (0, 0, 0),
                                           #text_font = self.font,
                                           relief = None)

        btn = DirectButton(text = "Restart",
                           command = self.startGame,
                           pos = (-0.3, 0, -0.2),
                           parent = self.gameOverScreen,
                           scale = 0.07,
                           #text_font = self.font,
                           frameSize = (-4, 4, -1, 1),
                           text_scale = 0.75,
                           relief = DGG.FLAT,
                           text_pos = (0, -0.2))
        btn.setTransparency(True)

        btn = DirectButton(text = "Quit",
                           command = self.quit,
                           pos = (0.3, 0, -0.2),
                           parent = self.gameOverScreen,
                           scale = 0.07,
                           #text_font = self.font,
                           frameSize = (-4, 4, -1, 1),
                           text_scale = 0.75,
                           relief = DGG.FLAT,
                           text_pos = (0, -0.2))
        btn.setTransparency(True)

        render.setShaderAuto()

        self.startGame()

    def toggleFrameRateMeter(self):
        self.showFrameRateMeter = not self.showFrameRateMeter

        self.setFrameRateMeter(self.showFrameRateMeter)

    def startGame(self):
        self.gameOverScreen.hide()

        self.cleanup()

        self.player = Player()

        self.currentLevel = Level("testLevel")

    def selectWeapon(self, index):
        if self.player is not None:
            self.player.setCurrentWeapon(index)

    def interact(self):
        if self.player is not None:
            self.player.interact()

    def onMouseWheel(self, dir):
        if self.player is not None:
            self.player.scrollWeapons(dir)

    def updateKeyMap(self, controlName, controlState, callback = None):
        self.keyMap[controlName] = controlState

        if callback is not None:
            callback(controlName, controlState)

    def update(self, task):
        dt = globalClock.getDt()

        if self.currentLevel is not None:
            self.currentLevel.update(self.player, self.keyMap, dt)

            if self.player is not None and self.player.health <= 0:
                if self.gameOverScreen.isHidden():
                    self.gameOverScreen.show()
                    #self.finalScoreLabel["text"] = "Final score: " + str(self.player.score)
                    #self.finalScoreLabel.setText()

            self.traverser.traverse(render)

            if self.player is not None and self.player.health > 0:
                self.player.postTraversalUpdate(dt)

        return Task.cont

    def projectileImpact(self, entry):
        fromNP = entry.getFromNodePath()
        proj = fromNP.getPythonTag(TAG_OWNER)

        intoNP = entry.getIntoNodePath()
        if intoNP.hasPythonTag(TAG_OWNER):
            intoObj = intoNP.getPythonTag(TAG_OWNER)
            proj.impact(intoObj)
        else:
            proj.impact(None)

    def itemCollected(self, entry):
        fromNP = entry.getFromNodePath()
        player = fromNP.getPythonTag(TAG_OWNER)

        intoNP = entry.getIntoNodePath()
        item = intoNP.getPythonTag(TAG_OWNER)

        item.collected(player)

    def triggerActivated(self, entry):
        intoNP = entry.getIntoNodePath()
        trigger = intoNP.getPythonTag(TAG_OWNER)

        if self.currentLevel is not None:
            self.currentLevel.triggerActivated(trigger)

    def cleanup(self):
        if self.currentLevel is not None:
            self.currentLevel.cleanup()
            self.currentLevel = None

        if self.player is not None:
            self.player.cleanup()
            self.player = None

    def quit(self):
        self.cleanup()

        base.userExit()
Exemplo n.º 3
0
class Section2():
    def __init__(self, showBase):
        self.showBase = showBase

        Common.initialise()
        Common.framework = self

        showBase.render.setShaderAuto()

        self.keyMap = {
            "up" : False,
            "down" : False,
            "left" : False,
            "right" : False,
            "shoot" : False,
            "shootSecondary" : False
        }

        showBase.accept("w", self.updateKeyMap, ["up", True])
        showBase.accept("w-up", self.updateKeyMap, ["up", False])
        showBase.accept("s", self.updateKeyMap, ["down", True])
        showBase.accept("s-up", self.updateKeyMap, ["down", False])
        showBase.accept("mouse1", self.updateKeyMap, ["shoot", True])
        showBase.accept("mouse1-up", self.updateKeyMap, ["shoot", False])
        showBase.accept("mouse3", self.updateKeyMap, ["shootSecondary", True])
        showBase.accept("mouse3-up", self.updateKeyMap, ["shootSecondary", False])

        showBase.accept("\\", self.toggleFriction)

        self.pusher = CollisionHandlerPusher()
        self.traverser = CollisionTraverser()
        self.traverser.setRespectPrevTransform(True)

        self.pusher.add_in_pattern("%fn-into-%in")
        self.pusher.add_in_pattern("%fn-into")
        self.pusher.add_again_pattern("%fn-again-into")
        showBase.accept("projectile-into", self.projectileImpact)
        showBase.accept("projectile-again-into", self.projectileImpact)
        showBase.accept("player-into", self.gameObjectPhysicalImpact)
        showBase.accept("enemy-into", self.gameObjectPhysicalImpact)

        self.updateTask = showBase.taskMgr.add(self.update, "update")

        self.player = None
        self.currentLevel = None

    def toggleFriction(self):
        Common.useFriction = not Common.useFriction

    def startGame(self, shipSpec):
        self.cleanupLevel()

        self.player = Player(shipSpec)

        self.currentLevel = Level("spaceLevel")

    def updateKeyMap(self, controlName, controlState, callback = None):
        self.keyMap[controlName] = controlState

        if callback is not None:
            callback(controlName, controlState)

    def update(self, task):
        dt = globalClock.getDt()

        if self.currentLevel is not None:
            self.currentLevel.update(self.player, self.keyMap, dt)

            if self.player is not None and self.player.health <= 0:
                self.showBase.gameOver()
                return Task.done

            self.traverser.traverse(self.showBase.render)

            if self.player is not None and self.player.health > 0:
                self.player.postTraversalUpdate(dt)

        return Task.cont

    def projectileImpact(self, entry):
        fromNP = entry.getFromNodePath()
        proj = fromNP.getPythonTag(TAG_OWNER)

        intoNP = entry.getIntoNodePath()
        if intoNP.hasPythonTag(TAG_OWNER):
            intoObj = intoNP.getPythonTag(TAG_OWNER)
            proj.impact(intoObj)
        else:
            proj.impact(None)

    def gameObjectPhysicalImpact(self, entry):
        fromNP = entry.getFromNodePath()
        if fromNP.hasPythonTag(TAG_OWNER):
            fromNP.getPythonTag(TAG_OWNER).physicalImpact(entry.getSurfaceNormal(self.showBase.render))

    def triggerActivated(self, entry):
        intoNP = entry.getIntoNodePath()
        trigger = intoNP.getPythonTag(TAG_OWNER)

        if self.currentLevel is not None:
            self.currentLevel.triggerActivated(trigger)

    def cleanupLevel(self):
        if self.currentLevel is not None:
            self.currentLevel.cleanup()
            self.currentLevel = None

        if self.player is not None:
            self.player.cleanup()
            self.player = None

    def cleanup(self):
        self.showBase.ignore("w")
        self.showBase.ignore("w-up")
        self.showBase.ignore("s")
        self.showBase.ignore("s-up")
        self.showBase.ignore("mouse1")
        self.showBase.ignore("mouse1-up")
        self.showBase.ignore("mouse3")
        self.showBase.ignore("mouse3-up")
        self.showBase.ignore("\\")
        self.showBase.ignore("projectile-into")
        self.showBase.ignore("projectile-again-into")
        self.showBase.ignore("player-into")
        self.showBase.ignore("enemy-into")

        self.cleanupLevel()
        self.showBase.taskMgr.remove(self.updateTask)
        self.showBase = None
        self.updateTask = None