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):
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
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)") % (
@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):