Beispiel #1
0
    def onActivate(self):

        print "onActivate level"

        self.inputToggleInfo = Input.find("toggleInfo")
        self.inputTest = Input.find("runTests")

        # Record and playback the player's moves using the MacroRecorder and MacroPlayback
        self.inputSave = Input.find("save")
        self.inputLoad = Input.find("load")

        self.inputNextPlayer = Input.find("nextPlayer")
        print "Found all inputs"

        for player in game.findRolesByTag("player"):
            if self.player is None or player.awake:
                self.player = player

        if self.player:
            game.layout.findView("grid").centerOn(self.player.actor)
            game.layout.findView("test").centerOn(self.player.actor)
            game.layout.findView("plain").centerOn(self.player.actor)

        self.droppedFramesRole = game.findActorById("droppedFrames").role

        self.toggleInfo()

        # Now that the scene has loaded, let the player find the position it should start in
        # This is used on the "play" scene, to allow the player to start near to the gate he
        # has just completed.
        # If there are more than one player, then the others will go to sleep.
        for player in game.findRolesByTag("player"):
            player.getReady(player == self.player)

        for portcullis in game.findRolesByTag("portcullis"):
            portcullis.getReady(self.player)

        self.macroRecorder = MacroRecorder()
        self.macroRecorder.startRecording()

        global replayInput
        global recordedInput
        if replayInput:
            replayInput = False
            self.macroRecorder.recorded = recordedInput
            self.macroPlayback = MacroPlayback(recordedInput)
Beispiel #2
0
class Level(PlainSceneDirector):
    def __init__(self):

        self.collectablesRemaining = 0
        self.showInfo = True
        self.player = None

        self.macroRecorder = None
        self.macroPlayback = None

    def loading(self, scene):
        # Load the glass stage on top of the current scene.
        print "Loading the glass"
        game.mergeScene("glass")

    def onActivate(self):

        print "onActivate level"

        self.inputToggleInfo = Input.find("toggleInfo")
        self.inputTest = Input.find("runTests")

        # Record and playback the player's moves using the MacroRecorder and MacroPlayback
        self.inputSave = Input.find("save")
        self.inputLoad = Input.find("load")

        self.inputNextPlayer = Input.find("nextPlayer")
        print "Found all inputs"

        for player in game.findRolesByTag("player"):
            if self.player is None or player.awake:
                self.player = player

        if self.player:
            game.layout.findView("grid").centerOn(self.player.actor)
            game.layout.findView("test").centerOn(self.player.actor)
            game.layout.findView("plain").centerOn(self.player.actor)

        self.droppedFramesRole = game.findActorById("droppedFrames").role

        self.toggleInfo()

        # Now that the scene has loaded, let the player find the position it should start in
        # This is used on the "play" scene, to allow the player to start near to the gate he
        # has just completed.
        # If there are more than one player, then the others will go to sleep.
        for player in game.findRolesByTag("player"):
            player.getReady(player == self.player)

        for portcullis in game.findRolesByTag("portcullis"):
            portcullis.getReady(self.player)

        self.macroRecorder = MacroRecorder()
        self.macroRecorder.startRecording()

        global replayInput
        global recordedInput
        if replayInput:
            replayInput = False
            self.macroRecorder.recorded = recordedInput
            self.macroPlayback = MacroPlayback(recordedInput)

    def onLoaded(self):

        print "onLoaded"

        # Calculate the size of the grid needed to fit all of the actors
        stage = game.layout.findStage("grid")

        minX = 1000000
        minY = 1000000
        maxX = -1000000
        maxY = -1000000

        i = stage.iterator()
        if not i.hasNext():
            minX = 0
            minY = 0
            maxX = 0
            maxY = 0

        while i.hasNext():
            actor = i.next()
            x = actor.getX()
            y = actor.getY()

            if x < minX:
                minX = x

            if x > maxX:
                maxX = x

            if y < minY:
                minY = y

            if y > maxY:
                maxY = y

        squareSize = game.director.squareSize
        across = math.floor((maxX - minX) / squareSize) + 1
        down = math.floor((maxY - minY) / squareSize) + 1

        self.grid = Grid(squareSize, across, down, minX, minY)
        game.layout.findStage("grid").grid = self.grid

        # Add all of the GridRoles to the grid
        i = stage.iterator()
        while i.hasNext():
            actor = i.next()
            role = actor.role
            if isinstance(role, GridRole):
                if not role.actor.isDead():
                    role.placeOnGrid(self.grid)

    def tick(self):

        if self.player:
            self.player.playerTick()

        PlainSceneDirector.tick(self)

        if self.macroPlayback:
            self.macroPlayback.tick()

        if self.macroRecorder:
            self.macroRecorder.tick()

    def onKeyDown(self, kevent):

        if self.inputToggleInfo.matches(kevent):
            self.toggleInfo()

        if self.inputTest.matches(kevent):
            self.runTests()

        if self.inputNextPlayer.matches(kevent):
            self.wakeNextPlayer()

        if self.inputSave.matches(kevent):
            self.saveGame()

        if self.inputLoad.matches(kevent):
            self.loadGame()

    def playerDied(self, player):
        if self.player is player:
            self.wakeNextPlayer()

    def wakeNextPlayer(self):

        previous = None
        for player in game.findRolesByTag("player"):

            if player is self.player and previous is not None:
                self.wakePlayer(previous)
                return

            if player.actor.isDying() or player.actor.isDead():
                pass
            else:
                previous = player

        self.wakePlayer(previous)

    def wakePlayer(self, player):
        if player is None:
            # All players are dead!
            return

        if player is not self.player:
            self.player.sleep()
            self.player = player
            self.player.wake()

    def saveGame(self):
        global recordedInput
        recordedInput = self.macroRecorder.getRecording()
        print "Recorded: ", recordedInput

    def loadGame(self):
        global replayInput
        replayInput = True
        game.startScene(game.sceneName)

    def runTests(self):

        for autoPilot in Itchy.getGame().findRolesByTag("autoPilot"):
            autoPilot.run()
            return

        game.startScene("test")

    def toggleInfo(self):

        self.showInfo = not self.showInfo
        alpha = 255 if self.showInfo else 0

        if self.droppedFramesRole:
            self.droppedFramesRole.actor.appearance.alpha = alpha

    # When the number of collectables remaining is zero, tell all gates to open.
    def collected(self, amount):
        self.collectablesRemaining -= amount
        if self.collectablesRemaining <= 0:

            for gate in game.findRolesByTag("gate"):
                gate.onMessage("open")

    # Boiler plate code - no need to change this
    def getProperties(self):
        return properties

    # Boiler plate code - no need to change this
    def getClassName(self):
        return ClassName(SceneDirector, self.__module__ + ".py")