Added timeouts
This commit is contained in:
@ -3,4 +3,5 @@ from .utils import centre
|
|||||||
from .anim import AnimationHandler
|
from .anim import AnimationHandler
|
||||||
from .anim import AnimatedObject
|
from .anim import AnimatedObject
|
||||||
from .events import AnimStart
|
from .events import AnimStart
|
||||||
from .events import AnimFinish
|
from .events import AnimFinish
|
||||||
|
from .events import Timeout
|
||||||
@ -1,5 +1,7 @@
|
|||||||
|
from .events import Timeout
|
||||||
from .anim import AnimatedObject
|
from .anim import AnimatedObject
|
||||||
import tomllib
|
import tomllib
|
||||||
|
import time
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, surface):
|
def __init__(self, surface):
|
||||||
@ -13,12 +15,21 @@ class Game:
|
|||||||
self.surf = surface
|
self.surf = surface
|
||||||
self.size = self.surf.size
|
self.size = self.surf.size
|
||||||
self.pm = None
|
self.pm = None
|
||||||
|
# Holds all the timeouts that haven't been fired yet
|
||||||
|
self._timeouts = []
|
||||||
|
|
||||||
with open('./game.toml', 'r') as f:
|
with open('./game.toml', 'r') as f:
|
||||||
self.cfg = tomllib.loads(f.read())
|
self.cfg = tomllib.loads(f.read())
|
||||||
|
|
||||||
def update(self):
|
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):
|
def onEvent(self, event):
|
||||||
pass
|
pass
|
||||||
@ -28,3 +39,16 @@ class Game:
|
|||||||
|
|
||||||
def createAnimObj(self, *args, **kwargs):
|
def createAnimObj(self, *args, **kwargs):
|
||||||
return AnimatedObject(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))
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
class _event:
|
class _event:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.type = self.__class__
|
self.type = self.__class__
|
||||||
@ -18,4 +20,17 @@ class AnimFinish(_event):
|
|||||||
self.animationID = animationID
|
self.animationID = animationID
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<AnimFinish | {self.objectID=} | {self.animationID=}>'
|
return f'<AnimFinish | {self.objectID=} | {self.animationID=}>'
|
||||||
|
|
||||||
|
class Timeout(_event):
|
||||||
|
def __init__(self, timeoutID, fireOn):
|
||||||
|
super().__init__()
|
||||||
|
# The timeout ID specified by the user
|
||||||
|
self.timeoutID = timeoutID
|
||||||
|
# When the event should be fired
|
||||||
|
self.fireOn = fireOn
|
||||||
|
# When it was created
|
||||||
|
self.created = time.perf_counter()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<Timeout | {self.timeoutID=} | {self.fireOn=} | {self.created=}>'
|
||||||
Reference in New Issue
Block a user