X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fparser.py;h=c4b3e04d1b880b638616168089cd65f922863672;hp=5435925f753c2c324fe935abf89eb23fdaa69e27;hb=76079fba9b913a620bb4fee71504d427e0f9df10;hpb=81982ba30dbf5fca59d8e90816ded81cb20da279 diff --git a/bulletml/parser.py b/bulletml/parser.py index 5435925..c4b3e04 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -254,6 +254,23 @@ class Untag(object): """Construct using an ElementTree-style element.""" return cls(element.text) +class Appearance(object): + """Set a bullet appearance.""" + + def __init__(self, appearance): + self.appearance = appearance + + def __getstate__(self): + return dict(appearance=self.appearance) + + def __setstate__(self, state): + self.__init__(state["appearance"]) + + @classmethod + def FromXML(cls, doc, element): + """Construct using an ElementTree-style element.""" + return cls(element.text) + class Vanish(object): """Make the owner disappear.""" @@ -361,11 +378,13 @@ class Accel(object): class BulletDef(object): """Bullet definition.""" - def __init__(self, actions=(), direction=None, speed=None, tags=()): + def __init__(self, actions=(), direction=None, speed=None, tags=(), + appearance=None): self.direction = direction self.speed = speed self.actions = list(actions) self.tags = set(tags) + self.appearance = appearance def __getstate__(self): state = [] @@ -377,6 +396,8 @@ class BulletDef(object): state.append(("actions", self.actions)) if self.tags: state.append(("tags", list(self.tags))) + if self.appearance: + state.append(("appearance", self.appearance)) return state def __setstate__(self, state): @@ -412,6 +433,7 @@ class BulletDef(object): self.direction and self.direction(params, rank), self.speed and self.speed(params, rank), self.tags, + self.appearance, actions) def __repr__(self): @@ -579,13 +601,14 @@ class Offset(object): class FireDef(object): """Fire definition (creates a bullet).""" - def __init__( - self, bullet, direction=None, speed=None, offset=None, tags=()): + def __init__(self, bullet, direction=None, speed=None, offset=None, + tags=(), appearance=None): self.bullet = bullet self.direction = direction self.speed = speed self.offset = offset self.tags = set(tags) + self.appearance = appearance def __getstate__(self): state = [] @@ -597,6 +620,8 @@ class FireDef(object): state.append(("offset", self.offset)) if self.tags: state.append(("tags", list(self.tags))) + if self.appearance: + state.append(("appearance", self.appearance)) try: params = self.bullet.params except AttributeError: @@ -620,6 +645,7 @@ class FireDef(object): speed = None offset = None tags = set() + appearance = None for subelem in element.getchildren(): tag = realtag(subelem) @@ -635,8 +661,10 @@ class FireDef(object): offset = Offset.FromXML(doc, subelem) elif tag == "tag": tags.add(subelem.text) + elif tag == "appearance": + appearance = subelem.text try: - fire = cls(bullet, direction, speed, offset, tags) + fire = cls(bullet, direction, speed, offset, tags, appearance) except UnboundLocalError as exc: raise ParseError(str(exc)) else: @@ -644,13 +672,15 @@ class FireDef(object): return fire def __call__(self, params, rank): - direction, speed, tags, actions = self.bullet(params, rank) + direction, speed, tags, appearance, actions = self.bullet(params, rank) if self.direction: direction = self.direction(params, rank) if self.speed: speed = self.speed(params, rank) tags = tags.union(self.tags) - return direction, speed, self.offset, tags, actions + if self.appearance: + appearance = self.appearance + return direction, speed, self.offset, tags, appearance, actions def __repr__(self): return "%s(direction=%r, speed=%r, bullet=%r)" % ( @@ -813,6 +843,7 @@ ActionDef.CONSTRUCTORS = dict( wait=Wait, vanish=Vanish, tag=Tag, + appearance=Appearance, untag=Untag, action=ActionDef, actionRef=ActionRef)