From: Joe Wreschnig Date: Wed, 17 Mar 2010 09:13:21 +0000 (-0700) Subject: BulletML: FromDocument for parity with FromElement. X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=commitdiff_plain;h=ba81e7d74da58dc8dfa47949502d2a2759c84309 BulletML: FromDocument for parity with FromElement. --- diff --git a/bulletml-parse b/bulletml-parse deleted file mode 100755 index 6f36ccc..0000000 --- a/bulletml-parse +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python - -import sys - -from bulletml import BulletML - -for filename in sys.argv[1:]: - print BulletML(open(filename, "rU")) diff --git a/bulletml-runner b/bulletml-runner index 21ba80e..de0fa5a 100755 --- a/bulletml-runner +++ b/bulletml-runner @@ -23,9 +23,10 @@ def main(argv): while True: filename = argv[file_idx % len(argv)] - doc = bulletml.BulletML(open(filename, "rU")) - source = bulletml.Bullet.FromDoc( - doc, x=150, y=150, target=target, rank=0.5) + doc = bulletml.BulletML.FromDocument(open(filename, "rU")) + actions = [act([], 0.5) for act in doc.top] + source = bulletml.Bullet( + x=150, y=150, target=target, actions=actions, rank=0.5) active = set([source]) source.vanished = True @@ -58,8 +59,10 @@ def main(argv): elif event.key == pygame.K_RETURN: newfile = True elif event.key == pygame.K_s: - source = bulletml.Bullet.FromDoc( - doc, x=150, y=150, target=target, rank=0.5) + actions = [act([], 0.5) for act in doc.top] + source = bulletml.Bullet( + x=150, y=150, target=target, + actions=actions, rank=0.5) source.vanished = True active.add(source) target.x, target.y = pygame.mouse.get_pos() diff --git a/bulletml/impl.py b/bulletml/impl.py index fcb3aab..98b0ef7 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -308,10 +308,3 @@ class Bullet(object): self.y += self.my - math.cos(self.direction) * self.speed return created - - @classmethod - def FromDoc(cls, doc, params=(), x=0, y=0, speed=0, direction=0, - target=None, rank=0.5): - """Construct a bullet from top-level actions in a document.""" - actions = [act(params, rank) for act in doc.top] - return cls(x, y, direction, speed, target, actions, rank=rank) diff --git a/bulletml/parser.py b/bulletml/parser.py index b39437a..edb9d29 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -443,22 +443,25 @@ class BulletML(object): fire=FireDef, ) - def __init__(self, source): + def __init__(self, type="none", bullets={}, fires={}, actions={}): + self.type = type self.bullets = {} self.actions = {} self.fires = {} - self._bullet_refs = [] - self._action_refs = [] - self._fire_refs = [] - + @classmethod + def FromDocument(cls, source): if isinstance(source, (str, unicode)): source = StringIO(source) tree = ElementTree() root = tree.parse(source) - self.type = root.get("type", "none") + self = cls(type=root.get("type", "none")) + + self._bullet_refs = [] + self._action_refs = [] + self._fire_refs = [] for element in root.getchildren(): tag = realtag(element) @@ -483,6 +486,8 @@ class BulletML(object): self.actions.pop(None, None) self.fires.pop(None, None) + return self + @property def top(self): """Get a list of all top-level actions."""