Last active
December 11, 2015 09:38
-
-
Save cesarkawakami/4581139 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Event(object): | |
def __init__(self): | |
self.subscribers = [] | |
def subscribe(self, callback): | |
self.subscribers.append(callback) | |
def notify(self, *args, **kwargs): | |
for subscriber in self.subscribers: | |
subscriber(*args, **kwargs) | |
class Entity(object): | |
def __init__(self): | |
self.died_event = Event() | |
def kill(self, other): | |
print "killing!!! hahaha" | |
other.die() | |
def die(self): | |
print "omg I'm dying!!" | |
self.died_event.notify(self) | |
class Game(object): | |
def __init__(self): | |
self.entities = [] | |
def create_entity(self): | |
entity = Entity() | |
entity.died_event.subscribe(self.on_entity_death) | |
self.entities.append(entity) | |
return entity | |
def on_entity_death(self, entity): | |
print "I'm being notified that my poor entity died... :(" | |
self.entities.remove(entity) | |
if __name__ == "__main__": | |
game = Game() | |
entity1 = game.create_entity() | |
entity2 = game.create_entity() | |
entity1.kill(entity2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment