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 9136f21..faaf9e0 100755 (executable)
@@ -37,9 +37,8 @@ def main(argv):
     while True:
         filename = argv[file_idx % len(argv)]
         doc = bulletml.BulletML.FromDocument(open(filename, "rU"))
     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
                                          
         active = set([source])
         source.vanished = True
index 27faaf0..8ab4ec2 100644 (file)
@@ -17,15 +17,13 @@ Basic Usage:
 
     from bulletml import Bullet, BulletML
     doc = Bulletml.BulletML.FromDocument(open("test.xml", "rU"))
 
     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
     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())
     bullets = [bullet]
     ...
     for bullet in bullets:
         bullets.extend(bullet.step())
-
     ...
 
 For drawing, you're on your own, but Bullet instances have a number of
     ...
 
 For drawing, you're on your own, but Bullet instances have a number of
index 938e9e2..aa911fa 100644 (file)
@@ -305,6 +305,13 @@ class Bullet(object):
         self._actions = [Action(self, None, action, params, rank)
                          for action, params in actions]
 
         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)") % (
     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):
 
     @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:
         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):
 
     @property
     def finished(self):