From ce232071c1cd106d002dc5659b4cb539de3c35ec Mon Sep 17 00:00:00 2001 From: Tipragot Date: Wed, 1 Nov 2023 21:03:30 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'=C3=A9venements=20retard=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/timing.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/plugins/timing.py b/src/plugins/timing.py index cfdbdcf..4d5d3d0 100644 --- a/src/plugins/timing.py +++ b/src/plugins/timing.py @@ -5,8 +5,9 @@ lancement du jeu et le temps depuis la dernière frame. from time import time +from typing import Callable from engine import GlobalPlugin, KeepAlive -from engine.ecs import World +from engine.ecs import Entity, World class GlobalTime(KeepAlive, float): @@ -27,6 +28,16 @@ class Delta(KeepAlive, float): """ +class TimedEvent: + """ + Composant permettant d'executer un callback après un certain temps. + """ + + def __init__(self, timer: float, callback: Callable[[World, Entity], object]): + self.timer = timer + self.callback = callback + + def __initialize(world: World): """ Initialise les ressources pour la gestion du temps. @@ -36,13 +47,22 @@ def __initialize(world: World): def __update(world: World): """ - Met à jour les ressources de temps. + Met à jour les ressources de temps et execute les `TimedEvent`. """ + # On met à jour le temps now = time() world[Delta] = delta = now - world[GlobalTime] world[GlobalTime] = now world[Time] += delta + # On met à jour les `TimedEvent` + for entity in world.query(TimedEvent): + event = entity[TimedEvent] + event.timer -= delta + if event.timer <= 0: + del entity[TimedEvent] + event.callback(world, entity) + PLUGIN = GlobalPlugin( [__initialize],