Skip to content

Entity.repeat #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from Jan 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 56 additions & 13 deletions addons/source-python/packages/source-python/entities/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
from listeners import OnEntityDeleted
from listeners import on_entity_deleted_listener_manager
from listeners.tick import Delay
from listeners.tick import Repeat
from listeners.tick import RepeatStatus
# Mathlib
from mathlib import NULL_VECTOR
# Memory
Expand All @@ -70,6 +72,9 @@
# Get a dictionary to store the delays
_entity_delays = defaultdict(set)

# Get a dictionary to store the repeats
_entity_repeats = defaultdict(set)

# Get a set to store the registered entity classes
_entity_classes = WeakSet()

Expand Down Expand Up @@ -774,7 +779,7 @@ def _set_property(self, name, prop_type, value):
def delay(
self, delay, callback, args=(), kwargs=None,
cancel_on_level_end=False):
"""Execute a callback after the given delay.
"""Create the delay which will be stopped after removing the entity.

:param float delay:
The delay in seconds.
Expand Down Expand Up @@ -817,6 +822,33 @@ def _callback(*args, **kwargs):
# Return the delay instance...
return delay

def repeat(self, callback, args=(), kwargs=None, cancel_on_level_end=False):
"""Create the repeat which will be stopped after removing the entity.

:param callback:
A callable object that should be called at the end of each loop.
:param tuple args:
Arguments that should be passed to the callback.
:param dict kwargs:
Keyword arguments that should be passed to the callback.
:param bool cancel_on_level_end:
Whether or not to cancel the delay at the end of the map.
:raise ValueError:
Raised if the given callback is not callable.
:return:
The repeat instance.
:rtype: Repeat
"""

# Get the repeat instance...
repeat = Repeat(callback, args, kwargs, cancel_on_level_end)

# Add the repeat to the dictionary...
_entity_repeats[self.index].add(repeat)

# Return the repeat instance...
return repeat

def get_input(self, name):
"""Return the input function matching the given name.

Expand Down Expand Up @@ -1103,19 +1135,30 @@ def _on_entity_deleted(base_entity):
for cls in _entity_classes:
cls.cache.pop(index, None)

# Was no delay registered for this entity?
if index not in _entity_delays:
return
# Was delay registered for this entity?
if index in _entity_delays:
# Loop through all delays...
for delay in _entity_delays[index]:

# Make sure the delay is still running...
if not delay.running:
continue

# Cancel the delay...
delay.cancel()

# Loop through all delays...
for delay in _entity_delays[index]:
# Remove the entity from the dictionary...
del _entity_delays[index]

# Make sure the delay is still running...
if not delay.running:
continue
# Was repeat registered for this entity?
if index in _entity_repeats:
# Loop through all repeats...
for repeat in _entity_repeats[index]:

# Cancel the delay...
delay.cancel()
# Stop the repeat if running
if repeat.status is RepeatStatus.RUNNING:
repeat.stop()

# Remove the entity from the dictionary...
del _entity_delays[index]
# Remove the entity from the dictionary...
del _entity_repeats[index]