def __call__(self, owner, action, params, rank, created):
repeat = self.times(params, rank)
- actions, params = self.action(params, rank)
- child = action.__class__(
- owner, action, actions, params, rank, repeat)
- owner.replace(action, child)
- child.step(owner, created)
- return True
+ return self.action(owner, action, params, rank, created, repeat)
def __repr__(self):
return "%s(%r, %r)" % (type(self).__name__, self.times, self.action)
+class If(object):
+ """Conditional actions."""
+
+ def __init__(self, cond, then, else_=None):
+ self.cond = cond
+ self.then = then
+ self.else_ = else_
+
+ def __getstate__(self):
+ if self.else_:
+ return [('cond', self.cond.expr),
+ ('then', self.then),
+ ('else', self.else_)]
+ else:
+ return [('cond', self.cond.expr), ('then', self.then)]
+
+ def __setstate__(self, state):
+ state = dict(state)
+ state["else_"] = state.pop("else", None)
+ state["cond"] = INumberDef(state["cond"])
+ self.__init__(**state)
+
+ @classmethod
+ def FromXML(cls, doc, element):
+ """Construct using an ElementTree-style element."""
+ else_ = None
+ for subelem in element.getchildren():
+ tag = realtag(subelem)
+ if tag == "cond":
+ cond = INumberDef(subelem.text)
+ elif tag == "then":
+ then = ActionDef.FromXML(doc, subelem)
+ elif tag == "else":
+ else_ = ActionDef.FromXML(doc, subelem)
+ try:
+ return cls(cond, then, else_)
+ except UnboundLocalError as exc:
+ raise ParseError(str(exc))
+
+ def __call__(self, owner, action, params, rank, created):
+ if self.cond(params, rank):
+ branch = self.then
+ else:
+ branch = self.else_
+
+ if branch:
+ return branch(owner, action, params, rank, created)
+
+ def __repr__(self):
+ if self.else_:
+ return "%s(%r, then=%r, else_=%r)" % (
+ type(self).__name__, self.cond, self.then, self.else_)
+ else:
+ return "%s(%r, then=%r)" % (
+ type(self).__name__, self.cond, self.then)
+
class Accel(object):
"""Accelerate over some time."""
doc._bullets[element.get("label")] = dfn
return dfn
- def __call__(self, params, rank):
- actions = [action(params, rank) for action in self.actions]
+ def __call__(self, owner, action, params, rank, created):
+ actions = [a(None, action, params, rank, created)
+ for a in self.actions]
return (
self.direction and self.direction(params, rank),
self.speed and self.speed(params, rank),
doc._bullet_refs.append(bullet)
return bullet
- def __call__(self, params, rank):
- return self.bullet(self.params(params, rank), rank)
+ def __call__(self, owner, action, params, rank, created):
+ params = self.params(params, rank)
+ return self.bullet(owner, action, params, rank, created)
def __repr__(self):
return "%s(params=%r, bullet=%r)" % (
doc._actions[element.get("label")] = dfn
return dfn
- def __call__(self, params, rank):
- return self.actions, params
+ def __call__(self, owner, action, params, rank, created=(), repeat=1):
+ Action = action if isinstance(action, type) else type(action)
+ parent = None if owner is None else action
+ child = Action(parent, self.actions, params, rank, repeat)
+ if owner is not None:
+ owner.replace(parent, child)
+ child.step(owner, created)
+ return child
def __repr__(self):
return "%s(%r)" % (type(self).__name__, self.actions)
doc._action_refs.append(action)
return action
- def __call__(self, params, rank):
- return self.action(self.params(params, rank), rank)
+ def __call__(self, owner, action, params, rank, created=(), repeat=1):
+ params = self.params(params, rank)
+ return self.action(owner, action, params, rank, created, repeat)
def __repr__(self):
return "%s(params=%r, action=%r)" % (
return fire
def __call__(self, owner, action, params, rank, created):
- direction, speed, tags, appearance, actions = self.bullet(params, rank)
+ direction, speed, tags, appearance, actions = self.bullet(
+ owner, action, params, rank, created)
if self.direction:
direction = self.direction(params, rank)
if self.speed:
bullet = owner.__class__(
x=x, y=y, direction=direction, speed=speed,
target=owner.target, actions=actions, rank=rank,
- appearance=appearance, tags=tags, Action=action.__class__)
+ appearance=appearance, tags=tags)
created.append(bullet)
def __repr__(self):
untag=Untag,
action=ActionDef,
actionRef=ActionRef)
+ActionDef.CONSTRUCTORS["if"] = If