Ajout de coroutines pour les animations

This commit is contained in:
Tipragot 2023-11-01 22:16:02 +01:00
parent c396138bd5
commit 915b122a05
2 changed files with 20 additions and 2 deletions

View file

@ -3,18 +3,27 @@ Module d'exemple de l'utilisation du moteur de jeu.
""" """
from engine import Scene, start_game from engine import Scene, start_game
from engine.ecs import World
from plugins import assets, defaults from plugins import assets, defaults
from plugins.animation import Animation
from plugins.coroutine import Coroutine, condition, wait from plugins.coroutine import Coroutine, condition, wait
from plugins.inputs import MousePosition 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. Fonction du turfu qui s'execute sur plusieurs frames.
""" """
print("LOL") print("LOL")
yield wait(10.0) yield wait(10.0)
print("LOL 10 secondes après") 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) yield condition(lambda world: world.get(MousePosition).x > 500)
print("La souris est a droite !") print("La souris est a droite !")
@ -25,7 +34,7 @@ start_game(
Scene( Scene(
[ [
lambda world: world.new_entity().set( lambda world: world.new_entity().set(
Coroutine(super_fonction_du_turfu()), Coroutine(super_fonction_du_turfu(world)),
) )
], ],
[], [],

View file

@ -30,6 +30,13 @@ class Animation:
self.loop = loop self.loop = loop
self.callback = callback self.callback = callback
self.timer = 0.0 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): def __update_sprite(entity: Entity, texture: pygame.Surface, assets: Assets):
@ -76,12 +83,14 @@ def __update_animations(world: World) -> None:
else: else:
# Sinon on supprime le composant `Animation` # Sinon on supprime le composant `Animation`
del entity[Animation] del entity[Animation]
animation.ended = True
# Et on appelle la fonction de callback # Et on appelle la fonction de callback
animation.callback(world, entity) animation.callback(world, entity)
else: else:
# Si l'animation n'est pas finie, on met à jour la texture # Si l'animation n'est pas finie, on met à jour la texture
__update_sprite(entity, texture, assets) __update_sprite(entity, texture, assets)
animation.ended = False
PLUGIN = GlobalPlugin( PLUGIN = GlobalPlugin(