|
52 | 52 | from listeners import OnEntityDeleted |
53 | 53 | from listeners import on_entity_deleted_listener_manager |
54 | 54 | from listeners.tick import Delay |
| 55 | +from listeners.tick import Repeat |
| 56 | +from listeners.tick import RepeatStatus |
55 | 57 | # Mathlib |
56 | 58 | from mathlib import NULL_VECTOR |
57 | 59 | # Memory |
|
72 | 74 | # Get a dictionary to store the delays |
73 | 75 | _entity_delays = defaultdict(set) |
74 | 76 |
|
| 77 | +# Get a dictionary to store the repeats |
| 78 | +_entity_repeats = defaultdict(set) |
| 79 | + |
75 | 80 | # Get a set to store the registered entity classes |
76 | 81 | _entity_classes = WeakSet() |
77 | 82 |
|
@@ -800,7 +805,7 @@ def _set_property(self, name, prop_type, value): |
800 | 805 | def delay( |
801 | 806 | self, delay, callback, args=(), kwargs=None, |
802 | 807 | cancel_on_level_end=False): |
803 | | - """Execute a callback after the given delay. |
| 808 | + """Create the delay which will be stopped after removing the entity. |
804 | 809 |
|
805 | 810 | :param float delay: |
806 | 811 | The delay in seconds. |
@@ -843,6 +848,33 @@ def _callback(*args, **kwargs): |
843 | 848 | # Return the delay instance... |
844 | 849 | return delay |
845 | 850 |
|
| 851 | + def repeat(self, callback, args=(), kwargs=None, cancel_on_level_end=False): |
| 852 | + """Create the repeat which will be stopped after removing the entity. |
| 853 | +
|
| 854 | + :param callback: |
| 855 | + A callable object that should be called at the end of each loop. |
| 856 | + :param tuple args: |
| 857 | + Arguments that should be passed to the callback. |
| 858 | + :param dict kwargs: |
| 859 | + Keyword arguments that should be passed to the callback. |
| 860 | + :param bool cancel_on_level_end: |
| 861 | + Whether or not to cancel the delay at the end of the map. |
| 862 | + :raise ValueError: |
| 863 | + Raised if the given callback is not callable. |
| 864 | + :return: |
| 865 | + The repeat instance. |
| 866 | + :rtype: Repeat |
| 867 | + """ |
| 868 | + |
| 869 | + # Get the repeat instance... |
| 870 | + repeat = Repeat(callback, args, kwargs, cancel_on_level_end) |
| 871 | + |
| 872 | + # Add the repeat to the dictionary... |
| 873 | + _entity_repeats[self.index].add(repeat) |
| 874 | + |
| 875 | + # Return the repeat instance... |
| 876 | + return repeat |
| 877 | + |
846 | 878 | def get_input(self, name): |
847 | 879 | """Return the input function matching the given name. |
848 | 880 |
|
@@ -1129,19 +1161,30 @@ def _on_entity_deleted(base_entity): |
1129 | 1161 | for cls in _entity_classes: |
1130 | 1162 | cls.cache.pop(index, None) |
1131 | 1163 |
|
1132 | | - # Was no delay registered for this entity? |
1133 | | - if index not in _entity_delays: |
1134 | | - return |
| 1164 | + # Was delay registered for this entity? |
| 1165 | + if index in _entity_delays: |
| 1166 | + # Loop through all delays... |
| 1167 | + for delay in _entity_delays[index]: |
| 1168 | + |
| 1169 | + # Make sure the delay is still running... |
| 1170 | + if not delay.running: |
| 1171 | + continue |
| 1172 | + |
| 1173 | + # Cancel the delay... |
| 1174 | + delay.cancel() |
1135 | 1175 |
|
1136 | | - # Loop through all delays... |
1137 | | - for delay in _entity_delays[index]: |
| 1176 | + # Remove the entity from the dictionary... |
| 1177 | + del _entity_delays[index] |
1138 | 1178 |
|
1139 | | - # Make sure the delay is still running... |
1140 | | - if not delay.running: |
1141 | | - continue |
| 1179 | + # Was repeat registered for this entity? |
| 1180 | + if index in _entity_repeats: |
| 1181 | + # Loop through all repeats... |
| 1182 | + for repeat in _entity_repeats[index]: |
1142 | 1183 |
|
1143 | | - # Cancel the delay... |
1144 | | - delay.cancel() |
| 1184 | + # Stop the repeat if running |
| 1185 | + if repeat.status is RepeatStatus.RUNNING: |
| 1186 | + repeat.stop() |
1145 | 1187 |
|
1146 | | - # Remove the entity from the dictionary... |
1147 | | - del _entity_delays[index] |
| 1188 | + # Remove the entity from the dictionary... |
| 1189 | + del _entity_repeats[index] |
| 1190 | + |
0 commit comments