Skip to content

Instantly share code, notes, and snippets.

@Brian61
Last active March 1, 2016 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Brian61/2702669ba9f42802815d to your computer and use it in GitHub Desktop.
Save Brian61/2702669ba9f42802815d to your computer and use it in GitHub Desktop.
Actor action time scheduler
# scheduler.py - scheduling entity actions
import collections
import numbers
class Scheduler(object):
"""
Schedule entity actions
"""
def __init__(self, actors, start_time=0):
if not issubclass(actors, collections.Mapping):
raise TypeError("actors must be subclass of Mapping")
if start_time and not issubclass(start_time, numbers.Number):
raise TypeError("current_time must be a subclass of Number")
self.actors = actors
self.start_time = start_time
super(Scheduler, self).__init__()
def current_actors(self, delta_time, blocking_id =0):
time_next = self.start_time + delta_time
if blocking_id:
block_time = self.actors[blocking_id].action_time
if block_time < time_next:
if block_time < (self.start_time + 0.001):
return [] # first actor would block
time_next = block_time
rval = [(v.action_time, k, v) for k, v in self.actors.items()
if v.action_time < time_next ]
rval.sort()
self.start_time = time_next
return rval
def modify_time(self, actor_id, modifier, start_time=0):
start_time = start_time or self.start_time
actor = self.actors[actor_id]
delta_time = actor.action_time - start_time
actor.action_time = start_time + (delta_time * modifier)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment