Last active
March 1, 2016 00:20
-
-
Save Brian61/2702669ba9f42802815d to your computer and use it in GitHub Desktop.
Actor action time scheduler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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