X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fimpl.py;h=6e8138a042bec6374d2a21de47a3c5efb9456e88;hp=938e9e23ad85567a7a38c5a55d385af6cf5ba70f;hb=6e7423671150b704dd2accbf4b8d544744271b81;hpb=5da55ac7eeae21ee0ca3149bed99c95383397f05 diff --git a/bulletml/impl.py b/bulletml/impl.py index 938e9e2..6e8138a 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -134,7 +134,7 @@ class Action(object): break elif isinstance(action, (parser.FireDef, parser.FireRef)): - direction, speed, actions, offset = action(s_params, rank) + direction, speed, offset, tags, actions = action(s_params, rank) if direction: direction, type = direction if type == "aim" or type is None: @@ -175,7 +175,9 @@ class Action(object): y += off_y bullet = owner.__class__( - x, y, direction, speed, owner.target, actions, rank) + x=x, y=y, direction=direction, speed=speed, + target=owner.target, actions=actions, rank=rank, + tags=tags, Action=self.__class__) created.append(bullet) elif isinstance(action, parser.ChangeSpeed): @@ -284,13 +286,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=(), rank=0.5, Action=Action): + actions=(), rank=0.5, tags=(), Action=Action): self.x = self.px = x self.y = self.py = y self.mx = 0 @@ -300,11 +302,19 @@ class Bullet(object): self.vanished = False self.target = target self.rank = rank - self.tags = set() + self.tags = set(tags) # New bullets reset the parent hierarchy. 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=x, y=y, direction=direction, speed=speed, + target=target, actions=actions, rank=rank, Action=Action) + def __repr__(self): return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, " "actions=%r, target=%r, vanished=%r)") % ( @@ -314,11 +324,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):