X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fimpl.py;h=9a1aa56adda6b9fc69727a34bdd72668f63e16eb;hp=e800810c798de5401b813c926a015d8e851d8919;hb=978a7d7372a25f5d2e5d4401151e20fd913bdae0;hpb=673e8986b11645ac6c8419cc49b39c06075c4f15 diff --git a/bulletml/impl.py b/bulletml/impl.py index e800810..9a1aa56 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -175,7 +175,7 @@ class Action(object): y += off_y bullet = owner.__class__( - x, y, direction, speed, owner.target, actions, self, rank) + x, y, direction, speed, owner.target, actions, rank) created.append(bullet) elif isinstance(action, parser.ChangeSpeed): @@ -284,13 +284,13 @@ class Bullet(object): Contructor Arguments: x, y, direction, speed, target, rank - same as the attributes actions - internal action list - parent - parent of actions, None for manually-created bullets + Action - custom Action constructor """ def __init__(self, x=0, y=0, direction=0, speed=0, target=None, - actions=(), parent=None, rank=0.5, Action=Action): + actions=(), rank=0.5, Action=Action): self.x = self.px = x self.y = self.py = y self.mx = 0 @@ -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):