Bullet.FromDocument: Abstract weird constructor handling.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 20 Mar 2010 06:54:21 +0000 (23:54 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 20 Mar 2010 06:54:21 +0000 (23:54 -0700)
Bullet.aim: Return 0, not current direction, for aiming to no target.

bulletml-runner
bulletml/__init__.py
bulletml/impl.py

index 9136f2133ff7090cf09800970df47374ae4c7e13..faaf9e01006fbbf47de14c2f549af7c4d5e628db 100755 (executable)
@@ -37,9 +37,8 @@ def main(argv):
     while True:
         filename = argv[file_idx % len(argv)]
         doc = bulletml.BulletML.FromDocument(open(filename, "rU"))
-        actions = [act([], 0.5) for act in doc.actions]
-        source = bulletml.Bullet(
-            x=150, y=150, target=target, actions=actions, rank=0.5)
+        source = bulletml.Bullet.FromDocument(
+            doc, x=150, y=150, target=target, rank=0.5)
                                          
         active = set([source])
         source.vanished = True
index 27faaf03364a52efd3f89b8eb8c3108fe4a1e85e..8ab4ec265f87601ea47515238039c76fdb8a214b 100644 (file)
@@ -17,15 +17,13 @@ Basic Usage:
 
     from bulletml import Bullet, BulletML
     doc = Bulletml.BulletML.FromDocument(open("test.xml", "rU"))
+    player = ...  # On your own here, but it needs x and y fields.
     rank = 0.5    # Player difficulty, 0 to 1
-    params = []   # Initial variable settings, usually empty
-    actions = [a(params, rank) for a in doc.actions]
-    bullet = Bullet(x, y, target=player, actions=actions, rank=rank)
+    bullet = Bullet.FromDocument(doc, x, y, target=player, rank=rank)
     bullets = [bullet]
     ...
     for bullet in bullets:
         bullets.extend(bullet.step())
-
     ...
 
 For drawing, you're on your own, but Bullet instances have a number of
index 938e9e23ad85567a7a38c5a55d385af6cf5ba70f..aa911fa2b6f64fc02eb779102b1f4e58b662715c 100644 (file)
@@ -305,6 +305,13 @@ class Bullet(object):
         self._actions = [Action(self, None, action, params, rank)
                          for action, params in actions]
 
+    @classmethod
+    def FromDocument(cls, doc, x=0, y=0, direction=0, speed=0, target=None,
+                     params=(), rank=0.5, Action=Action):
+        """Construct a new Bullet from a loaded BulletML document."""
+        actions = [a(params, rank) for a in doc.actions]
+        return cls(x, y, direction, speed, target, actions, rank, Action)
+
     def __repr__(self):
         return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
                 "actions=%r, target=%r, vanished=%r)") % (
@@ -314,11 +321,17 @@ class Bullet(object):
 
     @property
     def aim(self):
-        """Angle to the target, in radians."""
-        if self.target is None:
-            return self.direction
+        """Angle to the target, in radians.
+
+        If the target does not exist or cannot be found, return 0.
+        """
+        try:
+            target_x = self.target.x
+            target_y = self.target.y
+        except AttributeError:
+            return 0
         else:
-            return math.atan2(self.target.x - self.x, self.y - self.target.y)
+            return math.atan2(target_x - self.x, self.y - target_y)
 
     @property
     def finished(self):