Add <tag> support to BulletDef and FireDef.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 20 Mar 2010 07:16:47 +0000 (00:16 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 20 Mar 2010 07:16:47 +0000 (00:16 -0700)
bulletml/impl.py
bulletml/parser.py

index 9a1aa56..6e8138a 100644 (file)
@@ -134,7 +134,7 @@ class Action(object):
                 break
 
             elif isinstance(action, (parser.FireDef, parser.FireRef)):
-                direction, speed, actions, offset = action(s_params, rank)
+                direction, speed, offset, tags, actions = action(s_params, rank)
                 if direction:
                     direction, type = direction
                     if type == "aim" or type is None:
@@ -175,7 +175,9 @@ class Action(object):
                         y += off_y
 
                 bullet = owner.__class__(
-                    x, y, direction, speed, owner.target, actions, rank)
+                    x=x, y=y, direction=direction, speed=speed,
+                    target=owner.target, actions=actions, rank=rank,
+                    tags=tags, Action=self.__class__)
                 created.append(bullet)
 
             elif isinstance(action, parser.ChangeSpeed):
@@ -290,7 +292,7 @@ class Bullet(object):
     """
 
     def __init__(self, x=0, y=0, direction=0, speed=0, target=None,
-                 actions=(), rank=0.5, Action=Action):
+                 actions=(), rank=0.5, tags=(), Action=Action):
         self.x = self.px = x
         self.y = self.py = y
         self.mx = 0
@@ -300,7 +302,7 @@ class Bullet(object):
         self.vanished = False
         self.target = target
         self.rank = rank
-        self.tags = set()
+        self.tags = set(tags)
         # New bullets reset the parent hierarchy.
         self._actions = [Action(self, None, action, params, rank)
                          for action, params in actions]
@@ -310,7 +312,8 @@ class Bullet(object):
                      params=(), rank=0.5, Action=Action):
         """Construct a new Bullet from a loaded BulletML document."""
         actions = [a(params, rank) for a in doc.actions]
-        return cls(x, y, direction, speed, target, actions, rank, Action)
+        return cls(x=x, y=y, direction=direction, speed=speed,
+                   target=target, actions=actions, rank=rank, Action=Action)
 
     def __repr__(self):
         return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
index e9b0488..8f253fe 100644 (file)
@@ -358,10 +358,11 @@ class BulletDef(object):
     direction = None
     speed = None
 
-    def __init__(self, actions=[], direction=None, speed=None):
+    def __init__(self, actions=[], direction=None, speed=None, tags=()):
         self.direction = direction
         self.speed = speed
         self.actions = list(actions)
+        self.tags = set(tags)
 
     def __getstate__(self):
         state = []
@@ -371,6 +372,8 @@ class BulletDef(object):
             state.append(("speed", self.speed))
         if self.actions:
             state.append(("actions", self.actions))
+        if self.tags:
+            state.append(("tags", list(self.tags)))
         return state
 
     def __setstate__(self, state):
@@ -393,6 +396,8 @@ class BulletDef(object):
                 actions.append(ActionDef.FromXML(doc, subelem))
             elif tag == "actionRef":
                 actions.append(ActionRef.FromXML(doc, subelem))
+            elif tag == "tag":
+                self.tags.add(subelem.text)
         dfn = cls(actions, direction, speed)
         doc._bullets[element.get("label")] = dfn
         return dfn
@@ -402,6 +407,7 @@ class BulletDef(object):
         return (
             self.direction and self.direction(params, rank),
             self.speed and self.speed(params, rank),
+            self.tags,
             actions)
 
     def __repr__(self):
@@ -567,11 +573,13 @@ class Offset(object):
 class FireDef(object):
     """Fire definition (creates a bullet)."""
 
-    def __init__(self, bullet, direction=None, speed=None, offset=None):
+    def __init__(
+        self, bullet, direction=None, speed=None, offset=None, tags=()):
         self.bullet = bullet
         self.direction = direction
         self.speed = speed
         self.offset = offset
+        self.tags = set(tags)
 
     def __getstate__(self):
         state = []
@@ -581,6 +589,8 @@ class FireDef(object):
             state.append(("speed", self.speed))
         if self.offset:
             state.append(("offset", self.offset))
+        if self.tags:
+            state.append(("tags", list(self.tags)))
         try:
             params = self.bullet.params
         except AttributeError:
@@ -616,6 +626,8 @@ class FireDef(object):
                 bullet = BulletRef.FromXML(doc, subelem)
             elif tag == "offset":
                 offset = Offset.FromXML(doc, subelem)
+            elif tag == "tag":
+                self.tags.add(subelem.text)
         try:
             fire = cls(bullet, direction, speed, offset)
         except UnboundLocalError as exc:
@@ -625,12 +637,13 @@ class FireDef(object):
             return fire
 
     def __call__(self, params, rank):
-        direction, speed, actions = self.bullet(params, rank)
+        direction, speed, tags, actions = self.bullet(params, rank)
         if self.direction:
             direction = self.direction(params, rank)
         if self.speed:
             speed = self.speed(params, rank)
-        return direction, speed, actions, self.offset
+        tags = tags.union(self.tags)
+        return direction, speed, self.offset, tags, actions
 
     def __repr__(self):
         return "%s(direction=%r, speed=%r, bullet=%r)" % (