Beispiel #1
0
 def On_AutonomousAvatarReachedGoal(self, aAvatar):
     self.avatar = Avatar()
     self.avSprite.avatar = self.avatar
     self.avatar.strings = player.strings[:]
     self.avatar.feetPos = aAvatar.feetPos
     self.avatar.walkMask = self.walkMask
     self.avatar.triggerZones = self.triggerZones
Beispiel #2
0
    def __init__(self, levelNum, sound=True):
        global soundtrack
        events.AddListener(self)
        self.done = False
        self.deathDelay = 0
        self.levelNum = levelNum
        strLevelNum = '%02d' % levelNum
        triggers = data.levelTriggers['leveltriggers'+strLevelNum]
        if not levelNum % 2:
            # even levels are repeats of previous images
            strLevelNum = '%02d' % (levelNum-1)
        self.walkMask = data.levelMasks[strLevelNum]
        filePath= os.path.join(data.data_dir, 'levelbg%s-?.png' % strLevelNum)
        self.sound = sound

        bgPngs = glob.glob(filePath)
        bgPngs.sort()
        self.bgImages = [data.pngs[png] for png in bgPngs]

        self.avSprite = None
        self.avatar = AutonomousAvatar()
        self.visualEffects = visualeffects.EffectManager()

        self.miscSprites = []
        healthFont = font.load('Oh Crud BB', 28)
        #self.healthText = font.Text(healthFont, x=10, y=25, text='Health:')
        self.healthBar = HeartMeter()
        self.energyBar = EnergyMeter((240,5))

        self.fpsText = font.Text(healthFont, x=650, y=25)

        self.triggerZones = []
        for rect, clsName in triggers.items():
            cls = globals().get(clsName)
            if not cls:
                if len(rect) == 4:
                    print "ERROR: couldn't find", clsName
                continue
            zone = cls(rect, self)
            self.triggerZones.append(zone)
            if hasattr(zone, 'sprite'):
                self.miscSprites.append(zone.sprite)
            if DEBUG and hasattr(zone, 'debugSprite'):
                self.miscSprites.append(zone.debugSprite)

        self.enemySprites = {}

        self.startLoc = [key for key,val in triggers.items()
                        if val == 'start location'][0]

        if levelNum == 1 and self.sound:
            soundtrack = \
                queueSoundtrack('8bp077-01-nullsleep-her_lazer_light_eyes.mp3')
            soundtrack.play()
Beispiel #3
0
class Level(Scene):
    def On_AutonomousAvatarReachedGoal(self, aAvatar):
        self.avatar = Avatar()
        self.avSprite.avatar = self.avatar
        self.avatar.strings = player.strings[:]
        self.avatar.feetPos = aAvatar.feetPos
        self.avatar.walkMask = self.walkMask
        self.avatar.triggerZones = self.triggerZones

    def __init__(self, levelNum, sound=True):
        global soundtrack
        events.AddListener(self)
        self.done = False
        self.deathDelay = 0
        self.levelNum = levelNum
        strLevelNum = '%02d' % levelNum
        triggers = data.levelTriggers['leveltriggers'+strLevelNum]
        if not levelNum % 2:
            # even levels are repeats of previous images
            strLevelNum = '%02d' % (levelNum-1)
        self.walkMask = data.levelMasks[strLevelNum]
        filePath= os.path.join(data.data_dir, 'levelbg%s-?.png' % strLevelNum)
        self.sound = sound

        bgPngs = glob.glob(filePath)
        bgPngs.sort()
        self.bgImages = [data.pngs[png] for png in bgPngs]

        self.avSprite = None
        self.avatar = AutonomousAvatar()
        self.visualEffects = visualeffects.EffectManager()

        self.miscSprites = []
        healthFont = font.load('Oh Crud BB', 28)
        #self.healthText = font.Text(healthFont, x=10, y=25, text='Health:')
        self.healthBar = HeartMeter()
        self.energyBar = EnergyMeter((240,5))

        self.fpsText = font.Text(healthFont, x=650, y=25)

        self.triggerZones = []
        for rect, clsName in triggers.items():
            cls = globals().get(clsName)
            if not cls:
                if len(rect) == 4:
                    print "ERROR: couldn't find", clsName
                continue
            zone = cls(rect, self)
            self.triggerZones.append(zone)
            if hasattr(zone, 'sprite'):
                self.miscSprites.append(zone.sprite)
            if DEBUG and hasattr(zone, 'debugSprite'):
                self.miscSprites.append(zone.debugSprite)

        self.enemySprites = {}

        self.startLoc = [key for key,val in triggers.items()
                        if val == 'start location'][0]

        if levelNum == 1 and self.sound:
            soundtrack = \
                queueSoundtrack('8bp077-01-nullsleep-her_lazer_light_eyes.mp3')
            soundtrack.play()

    def getNextScene(self):
        #print 'getting next scene for ', self.levelNum
        nextScene = getLevel(self.levelNum+1, self.sound)
        return nextScene

    def end(self):
        events.RemoveListener(self)
        events.CleanWeakrefs()
        self.done = True
        self.avSprite = None
        self.avatar.triggerZones = []
        self.avatar.newLevel()
        del self.enemySprites
        del self.miscSprites
        del self.walkMask
        del self.triggerZones
        del self.visualEffects
        del self.bgImages
        del self.healthBar
        del self.energyBar

    def getAttackables(self):
        # TODO: might wanna be more sophisticated, including barrels and such.
        return self.enemySprites.keys()

    def calcBGOffset(self, cameraFocusX, cameraFocusY,
                     windowWidth, windowHeight,
                     backgroundWidth, backgroundHeight):
        '''Return the amount to offset the background.
        (cameraFocusX, cameraFocusY) is the spot where the camera is focused
        to, usually the center of the Avatar.
        '''
        return (-clamp(cameraFocusX-(windowWidth/2),
                       0, (backgroundWidth-windowWidth)),
                -clamp(cameraFocusY-(windowHeight/2),
                       0, (backgroundHeight-windowHeight))
               )

    def run(self):
        global _activeLevel
        global soundtrack
        _activeLevel = self
        self.done = False
        clock.set_fps_limit(60)
        win = window.window

        self.avSprite = AvatarSprite(self.avatar)
        self.avatar.feetPos = (0, 300)
        print 'setting goal to', self.startLoc
        self.avatar.goal = self.startLoc
        self.avatar.walkMask = self.walkMask
        self.avatar.triggerZones = self.triggerZones

        events.Fire('AvatarBirth', self.avatar)
        events.Fire('LevelStartedEvent', self)
        
        while not self.done:
            timeChange = clock.tick()

            if soundtrack and self.sound:
                soundtrack.dispatch_events()

            events.ConsumeEventQueue()
            win.dispatch_events()

            if self.deathDelay:
                self.deathDelay -= timeChange
                if self.deathDelay <= 0:
                    self.done = True

            if self.done or win.has_exit:
                player.strings = self.avatar.strings[:]
                events.Reset()
                break

            self.avSprite.update( timeChange )
            for miscSprite in self.miscSprites:
                miscSprite.update(timeChange)
            for enemySprite in self.enemySprites.values():
                enemySprite.update(timeChange)
            for sprite in self.visualEffects.sprites:
                sprite.update(timeChange)

            win.clear()

            # find the combined height of the background images

            bgWidth = 0
            bgHeight = 0

            for bg in self.bgImages:
                bgWidth += bg.width
                bgHeight += bg.height

            offset = self.calcBGOffset(self.avatar.x, self.avatar.y,
                                       win.width, win.height,
                                       bgWidth, bgHeight)

            window.bgOffset[0] = offset[0]
            window.bgOffset[1] = offset[1]

            #self.bg.blit(*window.bgOffset)
            #[bg.blit(*window.bgOffset) for bg in self.bgImages]

            for count, bg in enumerate(self.bgImages):
                bg.blit(count * 1024 + window.bgOffset[0], window.bgOffset[1])

            for miscSprite in self.miscSprites:
                miscSprite.draw()
            self.avSprite.draw()

            for enemySprite in self.enemySprites.values():
                enemySprite.draw()
            for sprite in self.visualEffects.sprites:
                sprite.draw()

            #self.healthText.draw()
            self.healthBar.draw(self.avatar)
            self.energyBar.draw(self.avatar)

            if DEBUG:
                self.fpsText.text = "fps: %d" % clock.get_fps()
                self.fpsText.draw()

            if clock.get_fps() < 30:
                events.Fire('LowFPS30')

            win.flip()

        return self.getNextScene()

    def On_LevelCompletedEvent(self, level):
        # just assume it's me.
        self.end()

    def On_AvatarDeath(self, avatar):
        # wait 5 seconds then cutscene
        self.deathDelay = 5.0
        def getNextScene():
            scene = DeathCutscene()
            scene.avatar = None
            return scene
        self.getNextScene = getNextScene

    def On_EnemyBirth(self, enemy):
        #print 'handling enemy birth'
        cls = getattr(enemysprite, enemy.spriteClass)
        enemySprite = cls(enemy)
        self.enemySprites[enemy] = enemySprite

    def On_EnemyDeath(self, enemy):
        del self.enemySprites[enemy]

    def On_TriggerZoneRemove(self, zone):
        if zone in self.triggerZones:
            self.triggerZones.remove(zone)

    def On_SpriteRemove(self, sprite):
        if sprite in self.miscSprites:
            self.miscSprites.remove(sprite)