<offset>: Parse, evaluate, and example test case. (Fixes issue #3)
[python-bulletml.git] / bulletml / impl.py
index ad39e7b..26b9580 100644 (file)
@@ -125,7 +125,7 @@ class Action(object):
                 break
 
             elif isinstance(action, (parser.FireDef, parser.FireRef)):
-                direction, speed, actions = action(self.params, rank)
+                direction, speed, actions, offset = action(self.params, rank)
                 if direction:
                     direction, type = direction
                     if type == "aim" or type is None:
@@ -153,8 +153,20 @@ class Action(object):
                     speed = 1
                 self.previous_fire_speed = speed
 
-                bullet = Bullet(owner.x, owner.y, direction, speed,
-                                owner.target, actions, self, rank)
+                x, y = owner.x, owner.y
+                if offset:
+                    off_x, off_y = offset(self.params, rank)
+                    if offset.type == "relative":
+                        sin = math.sin(direction)
+                        cos = math.cos(direction)
+                        x += cos * off_x + sin * off_y
+                        y += sin * off_x - cos * off_y
+                    else:
+                        x += off_x
+                        y += off_y
+
+                bullet = Bullet(
+                    x, y, direction, speed, owner.target, actions, self, rank)
                 created.append(bullet)
 
             elif isinstance(action, parser.ChangeSpeed):