From: Joe Wreschnig Date: Sat, 20 Mar 2010 06:54:21 +0000 (-0700) Subject: Bullet.FromDocument: Abstract weird constructor handling. X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=commitdiff_plain;h=a14b854db961815ff29962285bd4f632b98b98d9 Bullet.FromDocument: Abstract weird constructor handling. Bullet.aim: Return 0, not current direction, for aiming to no target. --- diff --git a/bulletml-runner b/bulletml-runner index 9136f21..faaf9e0 100755 --- a/bulletml-runner +++ b/bulletml-runner @@ -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 diff --git a/bulletml/__init__.py b/bulletml/__init__.py index 27faaf0..8ab4ec2 100644 --- a/bulletml/__init__.py +++ b/bulletml/__init__.py @@ -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 diff --git a/bulletml/impl.py b/bulletml/impl.py index 938e9e2..aa911fa 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -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):