Пример #1
0
    def update(self, events, world):
        context = world.find_component("context")
        settings = world.find_component("settings")
        context["paused"] = True

        exiting = False

        for event in events:
            if event.type == PAUSE_QUIT_TO_MENU:
                context["paused"] = False
                exiting = True
            elif event.type == PAUSE_SAVE_AND_QUIT:
                self._save(settings["save_file"], world)
                context["paused"] = False
                world.inject_event({
                    "type": "sound",
                    "action": "stop",
                    "sound": "background_music",
                })

                self.teardown(world)
                return SceneManager.replace(scenes.equip.EquipScene())

        if exiting:
            world.inject_event({
                "type": "sound",
                "action": "stop",
                "sound": "background_music",
            })
            return SceneManager.new_root(scenes.title.TitleScene())

        world.process_all_systems(events)
Пример #2
0
    def update(self, events, world):

        for event in events:
            if event.type == VICTORY:
                return SceneManager.replace(VictoryScene())

            if event.type == SCENE_REFOCUS:
                self.teardown(world)
                self.setup(world)

        # Loading should happen only AFTER refocusing, if both events have fired this frame
        for event in events:
            if event.type == LOAD:
                load(world)

        context = world.find_component("context")
        screen = context["screen"]

        # There will only ever be one player entity, unless scope drastically changes
        player_entity = world.filter("player")[0]

        # No physics until the player has jumped
        if player_entity.player.has_jumped:

            # First, clear out per-frame physics values
            world.inject_event({"type": "physics_frame_reset"})

            # TODO: Simulate gravity as a force, instead of just doing it in the movement system
            # world.inject_event({"type": "physics_force", "magnitude": 0, "angle": 90})

            # Then gliding, which translates rotation into acceleration
            world.inject_event({"type": "glide"})

            # Finally, we add movement after any events that could affect acceleration
            world.inject_event({"type": "move"})

            if calculate_altitude(player_entity, screen) > 0:

                # allow the deafening silence to emphasize the player's deadly mistake
                world.inject_event(
                    {
                        "type": "sound",
                        "action": "stop",
                        "sound": "background_music",
                    }
                )

                # also make a funny sound effect
                world.inject_event(
                    {
                        "type": "sound",
                        "action": "play",
                        "sound": "crash",
                    }
                )

                for sys in self.systems:
                    world.unregister_system(sys)
                return SceneManager.push(CrashResultsScene())

        world.process_all_systems(events)

        keys = pygame.key.get_pressed()
        mods = pygame.key.get_mods()

        # # Win button for debugging
        # if keys[pygame.K_v]:
        #     pygame.event.post(pygame.event.Event(VICTORY))

        # Before doing anything else, the player must jump off the cliff
        if not player_entity.player.has_jumped:

            if keys[pygame.K_SPACE] and not player_entity.player.jumping:

                # Tell everyone we've jumped
                player_entity.player.has_jumped = True
                player_entity.player.jumping = True

                # The jump itself
                world.inject_event(
                    {"type": "physics_force", "magnitude": 20, "angle": -20}
                )

                # Start background music
                world.inject_event(
                    {
                        "type": "sound",
                        "action": "start",
                        "sound": "background_music",
                    }
                )

        # We don't want to rotate before jumping. TODO: or do we?
        else:

            if player_entity.player.jumping:
                player_entity.rotation.angle += 0.5
                if player_entity.rotation.angle > 0:
                    player_entity.player.jumping = False

            rotation_speed = 1
            # If you have the wings upgrade, you can use shift to go back to slower rotation
            if player_entity.player.hasWings and not mods & pygame.KMOD_SHIFT:
                rotation_speed = 2

            # The player only has direct control over their angle from the ground.
            # Our rudimentary physics takes care of the rest.
            # Also, clamp the angle from straight up to straight down.
            if keys[pygame.K_RIGHT]:
                player_entity.player.jumping = False
                angle = player_entity.rotation.angle + rotation_speed
                player_entity.rotation.angle = min(angle, 90)
            if keys[pygame.K_LEFT]:
                player_entity.player.jumping = False
                angle = player_entity.rotation.angle - rotation_speed
                player_entity.rotation.angle = max(angle, -90)

            for event in events:
                if (
                    event.type == pygame.KEYDOWN
                    and event.key == pygame.K_SPACE
                    and player_entity.player.has_jumped
                    and player_entity.player.hasJetBoots
                    and player_entity.player.numBoosts > 0
                ):
                    player_entity.player.jumping = False
                    player_entity.player.numBoosts -= 1
                    world.inject_event(
                        {
                            "type": "physics_force",
                            "magnitude": 15,
                            "angle": player_entity.rotation.angle,
                        }
                    )

        for event in events:
            # Use keyup here as a simple way to only trigger once and not repeatedly
            if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
                return SceneManager.push(PauseScene())