From a3105f5ddabb9c6416158a5deadd00fe68ad87a3 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Sat, 10 Apr 2010 22:17:34 -0700 Subject: [PATCH] Action.CUSTOM: New action types without subclassing. --- bulletml/impl.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/bulletml/impl.py b/bulletml/impl.py index b2a0aae..262b7dc 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -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. -- 2.20.1