Bullet tagging. (Fixes issue #7)
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 18 Mar 2010 07:17:40 +0000 (00:17 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 18 Mar 2010 07:17:40 +0000 (00:17 -0700)
bulletml/impl.py
bulletml/parser.py

index 358aae1a1c192a60d8c4567aa5ddf63b6821a321..8f94d5569e02e5d91b5b3e3f751cf881b96491a7 100644 (file)
@@ -224,6 +224,15 @@ class Action(object):
                     elif type == "relative":
                         self.my = my / frames
 
+            elif isinstance(action, parser.Tag):
+                owner.tags.add(action.tag)
+
+            elif isinstance(action, parser.Untag):
+                try:
+                    owner.tags.remove(action.tag)
+                except KeyError:
+                    pass
+
             elif isinstance(action, parser.Wait):
                 self.wait_frames = action(self.params, rank)
                 break
@@ -246,6 +255,7 @@ class Bullet(object):
     target - object with .x and .y fields for "aim" directions
     vanished - set to true by a <vanish> action
     rank - game difficulty, 0 to 1, default 0.5
+    tags - string tags set by the running actions
 
     Contructor Arguments:
     x, y, direction, speed, target, rank - same as the attributes
@@ -266,6 +276,7 @@ class Bullet(object):
         self.vanished = False
         self.target = target
         self.rank = rank
+        self.tags = set()
         # New bullets reset the parent hierarchy.
         self._actions = [Action(self, None, action, params, rank)
                          for action, params in actions]
index a07cc2dab61f7cb74ba3bbeec0f91d83df08f744..de3e8170bdd932932e54070f451c8c6fe6db552c 100644 (file)
@@ -170,6 +170,28 @@ class Wait(object):
     def __repr__(self):
         return "%s(%r)" % (type(self).__name__, self.frames)
 
+class Tag(object):
+    """Set a bullet tag."""
+
+    def __init__(self, tag):
+        self.tag = tag
+
+    @classmethod
+    def FromElement(cls, doc, element):
+        """Construct using an ElementTree-style element."""
+        return cls(element.text)
+
+class Untag(object):
+    """Unset a bullet tag."""
+
+    def __init__(self, tag):
+        self.tag = tag
+
+    @classmethod
+    def FromElement(cls, doc, element):
+        """Construct using an ElementTree-style element."""
+        return cls(element.text)
+
 class Vanish(object):
     """Make the owner disappear."""
 
@@ -551,13 +573,15 @@ class BulletML(object):
             self.fires)
 
 ActionDef.CONSTRUCTORS = dict(
-        repeat=Repeat,
-        fire=FireDef,
-        fireRef=FireRef,
-        changeSpeed=ChangeSpeed,
-        changeDirection=ChangeDirection,
-        accel=Accel,
-        wait=Wait,
-        vanish=Vanish,
-        action=ActionDef,
-        actionRef=ActionRef)
+    repeat=Repeat,
+    fire=FireDef,
+    fireRef=FireRef,
+    changeSpeed=ChangeSpeed,
+    changeDirection=ChangeDirection,
+    accel=Accel,
+    wait=Wait,
+    vanish=Vanish,
+    tag=Tag,
+    untag=Untag,
+    action=ActionDef,
+    actionRef=ActionRef)