Action.CUSTOM: New action types without subclassing.
[python-bulletml.git] / bulletml / impl.py
index b2a0aae..262b7dc 100644 (file)
@@ -21,8 +21,15 @@ class Action(object):
       your custom action type.
     - Pass the impl.Bullet constructor your Action subclass when
       creating your root Bullet.
+
+    Or, for very simple actions, add it to the Action.CUSTOM
+    dictionary.  The key should be the type of the action and the
+    value a 3-ary callable that recieves the Action instance, the
+    owner, and a list to append created bullets to.
     """
 
+    CUSTOM = {}
+
     def __init__(self, owner, parent, actions, params, rank, repeat=1):
         self.actions = actions
         self.parent = parent
@@ -78,10 +85,7 @@ class Action(object):
             owner.speed += self.speed
 
         if self.direction_frames > 0:
-            # The Noiz implementation was a little weird here, I think
-            # there was a bug in it that prevented it from working if
-            # the frame count was 1. I'm still not sure what the aim
-            # check is supposed to do, exactly.
+            # I'm still not sure what the aim check is supposed to do.
             self.direction_frames -= 1
             if self.aiming and self.direction_frames <= 0:
                 owner.direction += owner.aim
@@ -273,7 +277,12 @@ class Action(object):
 
     def handle(self, action, owner, created):
         """Override in subclasses for new action types."""
-        raise NotImplementedError(action.__class__.__name__)
+        try:
+            handler = self.CUSTOM[type(action)]
+        except KeyError:
+            raise NotImplementedError(action.__class__.__name__)
+        else:
+            handler(self, owner, created)
 
 class Bullet(object):
     """Simple bullet implementation.