Action: Don't need the owner in the constructor. Factory function to create a child.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 21 Apr 2010 07:41:51 +0000 (00:41 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 21 Apr 2010 07:41:51 +0000 (00:41 -0700)
bulletml-runner
bulletml/impl.py
bulletml/parser.py

index 3dfdfa382a9b4f74535a06cabc7e6fbec2852861..b98e9010c3ed4f9c8811871342e1fd191b900552 100755 (executable)
@@ -50,7 +50,7 @@ def main(argv):
         active = set([source])
         source.vanished = True
         print filename
-        print "  Loaded %d top-level actions." % len(source._actions)
+        print "  Loaded %d top-level actions." % len(source.actions)
         frames = 0
         total = 0
         paused = False
index ca800be7848a15a4799a98e3a4f6835d3a984784..f0e8964d806351cd9a2e876db194730da44bc4f9 100644 (file)
@@ -23,7 +23,7 @@ class Action(object):
 
     """
 
-    def __init__(self, owner, parent, actions, params, rank, repeat=1):
+    def __init__(self, parent, actions, params, rank, repeat=1):
         self.actions = actions
         self.parent = parent
         self.repeat = repeat
@@ -48,6 +48,9 @@ class Action(object):
         return "%s(pc=%r, actions=%r)" % (
             type(self).__name__, self.pc, self.actions)
 
+    def Child(self, actions, params, rank, repeat=1):
+        return type(self)(self, actions, params, rank, repeat)
+
     def vanish(self):
         """End this action and its parents."""
         if self.parent:
@@ -117,7 +120,7 @@ class Action(object):
 
             if isinstance(action, (parser.ActionDef, parser.ActionRef)):
                 actions, params = action(s_params, rank)
-                child = self.__class__(owner, self, actions, params, rank)
+                child = self.__class__(self, actions, params, rank)
                 owner.replace(self, child)
                 child.step(owner, created)
                 break
@@ -163,7 +166,7 @@ class Bullet(object):
         self.tags = set(tags)
         self.appearance = appearance
         # New bullets reset the parent hierarchy.
-        self._actions = [Action(self, None, action, params, rank)
+        self.actions = [Action(None, action, params, rank)
                          for action, params in actions]
 
     @classmethod
@@ -178,7 +181,7 @@ class Bullet(object):
         return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
                 "actions=%r, target=%r, appearance=vanished=%r)") % (
             type(self).__name__, self.x, self.y, (self.mx, self.my),
-            self.direction, self.speed, self._actions, self.target,
+            self.direction, self.speed, self.actions, self.target,
             self.appearance, self.vanished)
 
     @property
@@ -208,7 +211,7 @@ class Bullet(object):
         """
         if not self.vanished:
             return False
-        for action in self._actions:
+        for action in self.actions:
             if not action.finished:
                 return False
         return True
@@ -216,9 +219,9 @@ class Bullet(object):
     def vanish(self):
         """Vanish this bullet and stop all actions."""
         self.vanished = True
-        for action in self._actions:
+        for action in self.actions:
             action.vanish()
-        self._actions = []
+        self.actions = []
 
     def replace(self, old, new):
         """Replace an active action with another.
@@ -226,11 +229,11 @@ class Bullet(object):
         This is mostly used by actions internally to queue children.
         """
         try:
-            idx = self._actions.index(old)
+            idx = self.actions.index(old)
         except ValueError:
             pass
         else:
-            self._actions[idx] = new
+            self.actions[idx] = new
 
     def step(self):
         """Advance by one frame.
@@ -242,7 +245,7 @@ class Bullet(object):
         """
         created = []
 
-        for action in self._actions:
+        for action in self.actions:
             action.step(self, created)
 
         self.px = self.x
index 2075ac961ae06715a4ffe899541667053fa8e563..c8bc990b105bfab652326be334a7db4b0be6a820 100644 (file)
@@ -368,8 +368,7 @@ class Repeat(object):
     def __call__(self, owner, action, params, rank, created):
         repeat = self.times(params, rank)
         actions, params = self.action(params, rank)
-        child = action.__class__(
-            owner, action, actions, params, rank, repeat)
+        child = action.Child(actions, params, rank, repeat)
         owner.replace(action, child)
         child.step(owner, created)
         return True
@@ -424,7 +423,7 @@ class If(object):
 
         if branch:
             actions, params = branch(params, rank)
-            child = action.__class__(owner, action, actions, params, rank)
+            child = action.Child(actions, params, rank)
             owner.replace(action, child)
             child.step(owner, created)
             return True