Paste Code
Paste Blends
Paste Images
from collections import defaultdict

class EventError(Exception):
def __init__(self, message=None, errors=None):
super().__init__(message, errors)
self.message = message
self.errors = errors

def __str__(self):
return repr(self.message, self.errors)

class EventNotFound(EventError):

def __str__(self):
return repr("Event '{}' not found".format(self.message) if self.message is not None else "Event not found")

class Event:

def __init__(self):
self.handlers = []

def add(self, handler):
self.handlers.append(handler)
return self

def remove(self, handler):
self.handlers.remove(handler)
return self

def trigger(self, event_args):
for handler in self.handlers:
handler(*event_args)

__iadd__ = add
__isub__ = remove
__call__ = trigger

class EventManager:
def __init__(self):
self.events = {}

def add(self, name, *responses):
event = self.events[name] = Event()

for response in responses:
event.add(response)

return event

def remove(self, name):
try:
self.events.pop(name)
except KeyError:
raise EventNotFound(name)

def trigger(self, name, *invoke_args):
try:
event = self.events[name]
except KeyError:
raise EventNotFound(name)

event.trigger(invoke_args)

manager = EventManager()

class Entity:
def __init__(self, entity_id):
self.entity_id = entity_id

def attack(self, other):
manager.trigger("remove_entity", other)

def die(self, other=None):
print("killed")

class EntityManager:
def __init__(self):
self.entities = []
manager.add("remove_entity", self.remove_entity)

def add_entity(self, id):
entity = Entity(id)
self.entities.append(entity)
return entity

def remove_entity(self, entity):
self.entities.remove(entity)
entity.die()

entities = EntityManager()

a = entities.add_entity(12)
b = entities.add_entity(13)

a.attack(b)
  1. from collections import defaultdict
  2.  
  3. class EventError(Exception):
  4.     def __init__(self, message=None, errors=None):
  5.         super().__init__(message, errors)
  6.         self.message = message
  7.         self.errors = errors
  8.        
  9.     def __str__(self):
  10.         return repr(self.message, self.errors)
  11.  
  12. class EventNotFound(EventError):
  13.    
  14.     def __str__(self):
  15.         return repr("Event '{}' not found".format(self.message) if self.message is not None else "Event not found")
  16.  
  17. class Event:
  18.    
  19.     def __init__(self):
  20.         self.handlers = []
  21.    
  22.     def add(self, handler):
  23.         self.handlers.append(handler)
  24.         return self
  25.    
  26.     def remove(self, handler):
  27.         self.handlers.remove(handler)
  28.         return self
  29.    
  30.     def trigger(self, event_args):
  31.         for handler in self.handlers:
  32.             handler(*event_args)
  33.    
  34.     __iadd__ = add
  35.     __isub__ = remove
  36.     __call__ = trigger
  37.    
  38. class EventManager:
  39.     def __init__(self):
  40.         self.events = {}
  41.        
  42.     def add(self, name, *responses):
  43.         event = self.events[name] = Event()
  44.        
  45.         for response in responses:
  46.             event.add(response)
  47.            
  48.         return event
  49.    
  50.     def remove(self, name):
  51.         try:
  52.             self.events.pop(name)
  53.         except KeyError:
  54.             raise EventNotFound(name)
  55.  
  56.     def trigger(self, name, *invoke_args):
  57.         try:
  58.             event = self.events[name]
  59.         except KeyError:
  60.             raise EventNotFound(name)
  61.        
  62.         event.trigger(invoke_args)
  63.                
  64. manager = EventManager()
  65.  
  66. class Entity:
  67.     def __init__(self, entity_id):
  68.         self.entity_id = entity_id
  69.    
  70.     def attack(self, other):
  71.         manager.trigger("remove_entity", other)
  72.    
  73.     def die(self, other=None):
  74.         print("killed")
  75.  
  76. class EntityManager:
  77.     def __init__(self):
  78.         self.entities = []
  79.         manager.add("remove_entity", self.remove_entity)
  80.        
  81.     def add_entity(self, id):
  82.         entity = Entity(id)
  83.         self.entities.append(entity)
  84.         return entity
  85.    
  86.     def remove_entity(self, entity):
  87.         self.entities.remove(entity)
  88.         entity.die()
  89.    
  90. entities = EntityManager()
  91.  
  92. a = entities.add_entity(12)
  93. b = entities.add_entity(13)
  94.  
  95. a.attack(b)
go to heaven