FireDef.__call__: Minor optimizations.
[python-bulletml.git] / bulletml / parser.py
index 2075ac9..1470ed3 100644 (file)
@@ -367,12 +367,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)
-        owner.replace(action, child)
-        child.step(owner, created)
-        return True
+        return self.action(owner, action, params, rank, created, repeat)
 
     def __repr__(self):
         return "%s(%r, %r)" % (type(self).__name__, self.times, self.action)
@@ -423,11 +418,15 @@ class If(object):
             branch = self.else_
 
         if branch:
-            actions, params = branch(params, rank)
-            child = action.__class__(owner, action, actions, params, rank)
-            owner.replace(action, child)
-            child.step(owner, created)
-            return True
+            return branch(owner, action, params, rank, created)
+
+    def __repr__(self):
+        if self.else_:
+            return "%s(%r, then=%r, else_=%r)" % (
+                type(self).__name__, self.cond, self.then, self.else_)
+        else:
+            return "%s(%r, then=%r)" % (
+                type(self).__name__, self.cond, self.then)
         
 class Accel(object):
     """Accelerate over some time."""
@@ -561,8 +560,9 @@ class BulletDef(object):
         doc._bullets[element.get("label")] = dfn
         return dfn
 
-    def __call__(self, params, rank):
-        actions = [action(params, rank) for action in self.actions]
+    def __call__(self, owner, action, params, rank, created):
+        actions = [a(None, action, params, rank, created)
+                   for a in self.actions]
         return (
             self.direction and self.direction(params, rank),
             self.speed and self.speed(params, rank),
@@ -602,8 +602,9 @@ class BulletRef(object):
         doc._bullet_refs.append(bullet)
         return bullet
 
-    def __call__(self, params, rank):
-        return self.bullet(self.params(params, rank), rank)
+    def __call__(self, owner, action, params, rank, created):
+        params = self.params(params, rank)
+        return self.bullet(owner, action, params, rank, created)
 
     def __repr__(self):
         return "%s(params=%r, bullet=%r)" % (
@@ -647,8 +648,14 @@ class ActionDef(object):
         doc._actions[element.get("label")] = dfn
         return dfn
 
-    def __call__(self, params, rank):
-        return self.actions, params
+    def __call__(self, owner, action, params, rank, created=(), repeat=1):
+        Action = action if isinstance(action, type) else type(action) 
+        parent = None if owner is None else action
+        child = Action(parent, self.actions, params, rank, repeat)
+        if owner is not None:
+            owner.replace(parent, child)
+            child.step(owner, created)
+        return child
 
     def __repr__(self):
         return "%s(%r)" % (type(self).__name__, self.actions)
@@ -681,8 +688,9 @@ class ActionRef(object):
         doc._action_refs.append(action)
         return action
 
-    def __call__(self, params, rank):
-        return self.action(self.params(params, rank), rank)
+    def __call__(self, owner, action, params, rank, created=(), repeat=1):
+        params = self.params(params, rank)
+        return self.action(owner, action, params, rank, created, repeat)
 
     def __repr__(self):
         return "%s(params=%r, action=%r)" % (
@@ -806,16 +814,17 @@ class FireDef(object):
             return fire
 
     def __call__(self, owner, action, params, rank, created):
-        direction, speed, tags, appearance, actions = self.bullet(params, rank)
-        if self.direction:
+        direction, speed, tags, appearance, actions = self.bullet(
+            owner, action, params, rank, created)
+        if self.direction is not None:
             direction = self.direction(params, rank)
-        if self.speed:
+        if self.speed is not None:
             speed = self.speed(params, rank)
         tags = tags.union(self.tags)
-        if self.appearance:
+        if self.appearance is not None:
             appearance = self.appearance
 
-        if direction:
+        if direction is not None:
             direction, type = direction
             if type == "aim" or type is None:
                 direction += owner.aim
@@ -827,7 +836,7 @@ class FireDef(object):
             direction = owner.aim
         action.previous_fire_direction = direction
 
-        if speed:
+        if speed is not None:
             speed, type = speed
             if type == "sequence":
                 speed += action.previous_fire_speed
@@ -842,8 +851,9 @@ class FireDef(object):
             speed = 1
         action.previous_fire_speed = speed
 
-        x, y = owner.x, owner.y
-        if self.offset:
+        x = owner.x
+        y = owner.y
+        if self.offset is not None:
             off_x, off_y = self.offset(params, rank)
             if self.offset.type == "relative":
                 s = sin(direction)
@@ -859,7 +869,7 @@ class FireDef(object):
         bullet = owner.__class__(
             x=x, y=y, direction=direction, speed=speed,
             target=owner.target, actions=actions, rank=rank,
-            appearance=appearance, tags=tags, Action=action.__class__)
+            appearance=appearance, tags=tags)
         created.append(bullet)
 
     def __repr__(self):