25
25
# Listeners
26
26
from listeners import listeners_logger
27
27
from listeners import on_tick_listener_manager
28
+ from listeners import OnLevelEnd
28
29
29
30
30
31
# =============================================================================
@@ -108,12 +109,16 @@ def add(self, delay):
108
109
class Delay (WeakAutoUnload ):
109
110
"""Execute a callback after a given delay."""
110
111
111
- def __init__ (self , delay , callback , * args , ** kwargs ):
112
+ def __init__ (
113
+ self , delay , callback , cancel_on_map_end = False , * args , ** kwargs
114
+ ):
112
115
"""Initialize the delay.
113
116
114
117
:param float delay: The delay in seconds.
115
118
:param callback: A callable object that should be called after the
116
119
delay expired.
120
+ :param bool cancel_on_map_end: Whether or not to cancel the delay at
121
+ the end of the map.
117
122
:param args: Arguments that should be passed to the callback.
118
123
:param kwargs: Keyword arguments that should be passed to the
119
124
callback.
@@ -126,6 +131,7 @@ def __init__(self, delay, callback, *args, **kwargs):
126
131
self ._start_time = time .time ()
127
132
self .exec_time = self ._start_time + delay
128
133
self .callback = callback
134
+ self .cancel_on_map_end = cancel_on_map_end
129
135
self .args = args
130
136
self .kwargs = kwargs
131
137
_delay_manager .add (self )
@@ -199,17 +205,20 @@ class RepeatStatus(IntEnum):
199
205
class Repeat (AutoUnload ):
200
206
"""Class used to create and call repeats."""
201
207
202
- def __init__ (self , callback , * args , ** kwargs ):
208
+ def __init__ (self , callback , cancel_on_map_end = False , * args , ** kwargs ):
203
209
"""Store all instance attributes.
204
210
205
211
:param callback: A callable object that should be called at the
206
212
end of each loop.
213
+ :param bool cancel_on_map_end: Whether or not to cancel the repeat at
214
+ the end of the map.
207
215
:param args: Arguments that should be passed to the callback.
208
216
:param kwargs: Keyword arguments that should be passed to the
209
217
callback.
210
218
"""
211
219
# Store the base attributes
212
220
self .callback = callback
221
+ self .cancel_on_map_end = cancel_on_map_end
213
222
self .args = args
214
223
self .kwargs = kwargs
215
224
@@ -268,7 +277,9 @@ def start(self, interval, limit, execute_on_start=False):
268
277
self ._adjusted = 0
269
278
270
279
# Start the delay
271
- self ._delay = Delay (self ._interval , self ._execute )
280
+ self ._delay = Delay (
281
+ self ._interval , self ._execute , self .cancel_on_map_end
282
+ )
272
283
273
284
# Call the callback if set to execute on start
274
285
if execute_on_start :
@@ -368,7 +379,9 @@ def resume(self):
368
379
self ._status = RepeatStatus .RUNNING
369
380
370
381
# Start the delay
371
- self ._delay = Delay (self ._loop_time , self ._execute )
382
+ self ._delay = Delay (
383
+ self ._loop_time , self ._execute , self .cancel_on_map_end
384
+ )
372
385
373
386
def extend (self , adjustment ):
374
387
"""Add to the number of loops to be made.
@@ -465,7 +478,9 @@ def _execute(self):
465
478
self .remaining_loops ))
466
479
467
480
# Call the delay again
468
- self ._delay = Delay (self ._interval , self ._execute )
481
+ self ._delay = Delay (
482
+ self ._interval , self ._execute , self .cancel_on_map_end
483
+ )
469
484
470
485
# Are no more loops to be made?
471
486
else :
@@ -572,3 +587,13 @@ def delay_time_elapsed(self):
572
587
def _unload_instance (self ):
573
588
"""Stop the repeat with being unloaded."""
574
589
self .stop ()
590
+
591
+
592
+ # =============================================================================
593
+ # >> HELPER FUNCTIONS
594
+ # =============================================================================
595
+ @OnLevelEnd
596
+ def _cancel_delays_on_level_end ():
597
+ for delay in _delay_manager :
598
+ if delay .cancel_on_map_end :
599
+ delay .cancel ()
0 commit comments