From 5cd161fd356e93abf7be68b114b7111c0ca92fd5 Mon Sep 17 00:00:00 2001 From: Tipragot Date: Sun, 29 Oct 2023 15:43:48 +0100 Subject: [PATCH] =?UTF-8?q?Meilleur=20gestion=20de=20l'audio=20lors=20du?= =?UTF-8?q?=20changement=20de=20sc=C3=A8nes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine.py | 69 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/src/engine.py b/src/engine.py index 1eb5fc4..0c61496 100644 --- a/src/engine.py +++ b/src/engine.py @@ -589,13 +589,13 @@ class Sound: volume: float = 0.5, loop: bool = False, callback: Callable[[World, Entity], object] = lambda _w, _e: None, + stop_on_remove: bool = False, ) -> None: - self._name = name + self.name = name self.volume = volume - self._loop = loop - self._is_paying = False - + self.loop = loop self.callback = callback + self.stop_on_remove = stop_on_remove class Scene: @@ -644,7 +644,7 @@ def start_game( assets = Assets(surface) # creation des channels pour les sons - channels: dict[Entity, pygame.mixer.Channel] = {} + channels: dict[Entity, tuple[bool, pygame.mixer.Channel]] = {} # On récupère la première scène scene = scenes.get(start_scene) @@ -741,6 +741,35 @@ def start_game( for system in scene.update_systems: system(world) + # Gestion des sons en cours + sound_entities = world.query(Sound) + entities_to_delete: list[Entity] = [] + for entity, (stop_on_remove, channel) in channels.items(): + if Sound in entity: + entity_sound = entity[Sound] + channel.set_volume(entity_sound.volume) + if not channel.get_busy(): + entities_to_delete.append(entity) + del entity[Sound] + entity_sound.callback(world, entity) + continue + if stop_on_remove and entity not in sound_entities: + entities_to_delete.append(entity) + channel.stop() + for entity in entities_to_delete: + del channels[entity] + + # Ajout des sons non gérés + for entity in sound_entities: + if entity not in channels: + entity_sound = entity[Sound] + sound = assets.get_sound(entity_sound.name) + channel = sound.play(loops=-1 if entity_sound.loop else 0) + if channel is None: # type: ignore + continue + channel.set_volume(entity_sound.volume) + channels[entity] = entity_sound.stop_on_remove, channel + # Mise à jour des animations for entity in world.query(Animation): animation = entity[Animation] @@ -804,36 +833,6 @@ def start_game( ), ) - # On update assets._sound en fonction des nouvelles entité. - for entity in world.query(Sound): - channel = channels.get(entity) - if channel is None: - sound = assets.get_sound(entity[Sound]._name) - channel: Optional[pygame.mixer.Channel] = sound.play( - loops=-1 if entity[Sound]._loop else 0 - ) - if channel is None: # type: ignore - continue - channels[entity] = channel - - channel.set_volume(entity[Sound].volume) - - # On verifie si le son est terminé - if not channel.get_busy(): - entity[Sound]._is_paying = False - callback = entity[Sound].callback - del entity[Sound] - del channels[entity] - callback(world, entity) - - entities_to_delete: list[Entity] = [] - for entity, channel in channels.items(): - if Sound not in entity: - channel.stop() - entities_to_delete.append(entity) - for entity in entities_to_delete: - del channels[entity] - # Mise a jour de la fenêtre rect = Display._calculate_surface_rect() pygame.transform.set_smoothscale_backend("MMX")