ecs #58
|
@ -5,8 +5,9 @@ lancement du jeu et le temps depuis la dernière frame.
|
||||||
|
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
|
from typing import Callable
|
||||||
from engine import GlobalPlugin, KeepAlive
|
from engine import GlobalPlugin, KeepAlive
|
||||||
from engine.ecs import World
|
from engine.ecs import Entity, World
|
||||||
|
|
||||||
|
|
||||||
class GlobalTime(KeepAlive, float):
|
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):
|
def __initialize(world: World):
|
||||||
"""
|
"""
|
||||||
Initialise les ressources pour la gestion du temps.
|
Initialise les ressources pour la gestion du temps.
|
||||||
|
@ -36,13 +47,22 @@ def __initialize(world: World):
|
||||||
|
|
||||||
def __update(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()
|
now = time()
|
||||||
world[Delta] = delta = now - world[GlobalTime]
|
world[Delta] = delta = now - world[GlobalTime]
|
||||||
world[GlobalTime] = now
|
world[GlobalTime] = now
|
||||||
world[Time] += delta
|
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(
|
PLUGIN = GlobalPlugin(
|
||||||
[__initialize],
|
[__initialize],
|
||||||
|
|
Loading…
Reference in a new issue