Add <tag> support to BulletDef and FireDef.
[python-bulletml.git] / bulletml / parser.py
index 9b2fbf8..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)" % (
@@ -710,41 +723,41 @@ class BulletML(object):
         tree = ElementTree()
         root = tree.parse(source)
 
-        self = cls(type=root.get("type", "none"))
+        doc = cls(type=root.get("type", "none"))
 
-        self._bullets = {}
-        self._actions = {}
-        self._fires = {}
-        self._bullet_refs = []
-        self._action_refs = []
-        self._fire_refs = []
+        doc._bullets = {}
+        doc._actions = {}
+        doc._fires = {}
+        doc._bullet_refs = []
+        doc._action_refs = []
+        doc._fire_refs = []
 
         for element in root.getchildren():
             tag = realtag(element)
-            if tag in self.CONSTRUCTORS:
-                self.CONSTRUCTORS[tag].FromXML(self, element)
+            if tag in doc.CONSTRUCTORS:
+                doc.CONSTRUCTORS[tag].FromXML(doc, element)
 
         try:
-            for ref in self._bullet_refs:
-                ref.bullet = self._bullets[ref.bullet]
-            for ref in self._fire_refs:
-                ref.fire = self._fires[ref.fire]
-            for ref in self._action_refs:
-                ref.action = self._actions[ref.action]
+            for ref in doc._bullet_refs:
+                ref.bullet = doc._bullets[ref.bullet]
+            for ref in doc._fire_refs:
+                ref.fire = doc._fires[ref.fire]
+            for ref in doc._action_refs:
+                ref.action = doc._actions[ref.action]
         except KeyError as exc:
             raise ParseError("unknown reference %s" % exc)
 
-        self.actions = [act for name, act in self._actions.items()
+        doc.actions = [act for name, act in doc._actions.items()
                         if name and name.startswith("top")]
 
-        del(self._bullet_refs)
-        del(self._action_refs)
-        del(self._fire_refs)
-        del(self._bullets)
-        del(self._actions)
-        del(self._fires)
+        del(doc._bullet_refs)
+        del(doc._action_refs)
+        del(doc._fire_refs)
+        del(doc._bullets)
+        del(doc._actions)
+        del(doc._fires)
         
-        return self
+        return doc
 
     @classmethod
     def FromYAML(cls, source):