X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fimpl.py;h=836fb410c4efbaaafec9a1c60c7d90b2b7bf50e9;hp=f0e8964d806351cd9a2e876db194730da44bc4f9;hb=67aa3f0b15d5f8122a8f99f5903ec2e2029937c4;hpb=bf5ff1d116840e1330c80894132919ec515596e4 diff --git a/bulletml/impl.py b/bulletml/impl.py index f0e8964..836fb41 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -4,7 +4,7 @@ from __future__ import division from math import atan2, sin, cos -from bulletml import parser +from bulletml.parser import ActionDef, ActionRef __all__ = ["Action", "Bullet"] @@ -48,7 +48,8 @@ class Action(object): return "%s(pc=%r, actions=%r)" % ( type(self).__name__, self.pc, self.actions) - def Child(self, actions, params, rank, repeat=1): + def Child(self, action, params, rank, repeat=1): + actions, params = action(params, rank) return type(self)(self, actions, params, rank, repeat) def vanish(self): @@ -73,8 +74,6 @@ class Action(object): def step(self, owner, created): """Advance by one frame.""" - s_params = self.params - rank = owner.rank if self.speed_frames > 0: self.speed_frames -= 1 @@ -100,6 +99,9 @@ class Action(object): self.wait_frames -= 1 return + s_params = self.params + rank = owner.rank + while True: self.pc += 1 @@ -118,9 +120,8 @@ class Action(object): self.pc = 0 action = self.actions[self.pc] - if isinstance(action, (parser.ActionDef, parser.ActionRef)): - actions, params = action(s_params, rank) - child = self.__class__(self, actions, params, rank) + if isinstance(action, (ActionDef, ActionRef)): + child = self.Child(action, s_params, rank) owner.replace(self, child) child.step(owner, created) break @@ -152,8 +153,7 @@ class Bullet(object): """ def __init__(self, x=0, y=0, direction=0, speed=0, target=None, - actions=(), rank=0.5, tags=(), appearance=None, - Action=Action): + actions=(), rank=0.5, tags=(), appearance=None): self.x = self.px = x self.y = self.py = y self.mx = 0 @@ -165,17 +165,18 @@ class Bullet(object): self.rank = rank self.tags = set(tags) self.appearance = appearance - # New bullets reset the parent hierarchy. - self.actions = [Action(None, action, params, rank) - for action, params in actions] + self.actions = list(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] + actions = [action(params, rank) for action in doc.actions] + # New bullets reset the parent hierarchy. + actions = [Action(None, action, params, rank) + for action, params in actions] return cls(x=x, y=y, direction=direction, speed=speed, - target=target, actions=actions, rank=rank, Action=Action) + target=target, actions=actions, rank=rank) def __repr__(self): return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, " @@ -248,9 +249,11 @@ class Bullet(object): for action in self.actions: action.step(self, created) + speed = self.speed + direction = self.direction self.px = self.x self.py = self.y - self.x += self.mx + sin(self.direction) * self.speed - self.y += -self.my + cos(self.direction) * self.speed + self.x += self.mx + sin(direction) * speed + self.y += -self.my + cos(direction) * speed return created