Added timeouts

This commit is contained in:
2025-06-21 23:03:11 +01:00
parent a421069a63
commit 4161ec9c5b
3 changed files with 43 additions and 3 deletions

View File

@ -1,5 +1,7 @@
from .events import Timeout
from .anim import AnimatedObject
import tomllib
import time
class Game:
def __init__(self, surface):
@ -13,12 +15,21 @@ class Game:
self.surf = surface
self.size = self.surf.size
self.pm = None
# Holds all the timeouts that haven't been fired yet
self._timeouts = []
with open('./game.toml', 'r') as f:
self.cfg = tomllib.loads(f.read())
def update(self):
pass
"""
Updates some core things in the background.
"""
for timeout in self._timeouts.copy():
if timeout.fireOn <= time.perf_counter():
self.onEvent(timeout)
self._timeouts.remove(timeout)
def onEvent(self, event):
pass
@ -28,3 +39,16 @@ class Game:
def createAnimObj(self, *args, **kwargs):
return AnimatedObject(self, *args, **kwargs)
def timeout(self, id: str, delay: float):
"""
Fires a Timeout event with the specified ID
after the specified delay.
Args:
id (str): The timeout ID.
delay (float): How long (in seconds)
to wait before firing.
"""
self._timeouts.append(Timeout(id, time.perf_counter()+delay))