From 915b122a05a1873d00e61cc21d433abd471d2709 Mon Sep 17 00:00:00 2001 From: Tipragot Date: Wed, 1 Nov 2023 22:16:02 +0100 Subject: [PATCH] Ajout de coroutines pour les animations --- src/main.py | 13 +++++++++++-- src/plugins/animation.py | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index 1638489..ad2e0b7 100644 --- a/src/main.py +++ b/src/main.py @@ -3,18 +3,27 @@ Module d'exemple de l'utilisation du moteur de jeu. """ from engine import Scene, start_game +from engine.ecs import World from plugins import assets, defaults +from plugins.animation import Animation from plugins.coroutine import Coroutine, condition, wait from plugins.inputs import MousePosition -def super_fonction_du_turfu(): +def super_fonction_du_turfu(world: World): """ Fonction du turfu qui s'execute sur plusieurs frames. """ print("LOL") yield wait(10.0) print("LOL 10 secondes après") + + animation = Animation("animations/intro") + world.new_entity().set(animation) + print("Started animation") + yield animation.wait() + print("Animation ended") + yield condition(lambda world: world.get(MousePosition).x > 500) print("La souris est a droite !") @@ -25,7 +34,7 @@ start_game( Scene( [ lambda world: world.new_entity().set( - Coroutine(super_fonction_du_turfu()), + Coroutine(super_fonction_du_turfu(world)), ) ], [], diff --git a/src/plugins/animation.py b/src/plugins/animation.py index 5ea69b3..fc810b6 100644 --- a/src/plugins/animation.py +++ b/src/plugins/animation.py @@ -30,6 +30,13 @@ class Animation: self.loop = loop self.callback = callback self.timer = 0.0 + self.ended = False + + def wait(self) -> Callable[[World], bool]: + """ + Utilitaire de `Coroutine` permettant d'attendre que l'animation soit finie. + """ + return lambda world: self.ended def __update_sprite(entity: Entity, texture: pygame.Surface, assets: Assets): @@ -76,12 +83,14 @@ def __update_animations(world: World) -> None: else: # Sinon on supprime le composant `Animation` del entity[Animation] + animation.ended = True # Et on appelle la fonction de callback animation.callback(world, entity) else: # Si l'animation n'est pas finie, on met à jour la texture __update_sprite(entity, texture, assets) + animation.ended = False PLUGIN = GlobalPlugin(