From 16023be19df43f8b8acc4671e0fabdeffb0e720a Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Wed, 17 Mar 2010 23:22:48 -0700 Subject: [PATCH] : Parse, evaluate, and example test case. (Fixes issue #3) --- bulletml/impl.py | 18 +++++++-- bulletml/parser.py | 42 ++++++++++++++++++--- examples/normal/threefire-offset.xml | 55 ++++++++++++++++++++++++++++ examples/template.xml | 3 +- 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 examples/normal/threefire-offset.xml diff --git a/bulletml/impl.py b/bulletml/impl.py index ad39e7b..26b9580 100644 --- a/bulletml/impl.py +++ b/bulletml/impl.py @@ -125,7 +125,7 @@ class Action(object): break elif isinstance(action, (parser.FireDef, parser.FireRef)): - direction, speed, actions = action(self.params, rank) + direction, speed, actions, offset = action(self.params, rank) if direction: direction, type = direction if type == "aim" or type is None: @@ -153,8 +153,20 @@ class Action(object): speed = 1 self.previous_fire_speed = speed - bullet = Bullet(owner.x, owner.y, direction, speed, - owner.target, actions, self, rank) + x, y = owner.x, owner.y + if offset: + off_x, off_y = offset(self.params, rank) + if offset.type == "relative": + sin = math.sin(direction) + cos = math.cos(direction) + x += cos * off_x + sin * off_y + y += sin * off_x - cos * off_y + else: + x += off_x + y += off_y + + bullet = Bullet( + x, y, direction, speed, owner.target, actions, self, rank) created.append(bullet) elif isinstance(action, parser.ChangeSpeed): diff --git a/bulletml/parser.py b/bulletml/parser.py index 5e73069..a07cc2d 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -375,19 +375,51 @@ class ActionRef(object): return "%s(params=%r, action=%r)" % ( type(self).__name__, self.params, self.action) +class Offset(object): + """Provide an offset to a bullet's initial position.""" + + VALID_TYPES = ["relative", "absolute"] + + def __init__(self, type, x, y): + if type not in self.VALID_TYPES: + raise ValueError("invalid type %r" % type) + self.type = type + self.x = x + self.y = y + + @classmethod + def FromElement(cls, doc, element): + """Construct using an ElementTree-style element.""" + type = element.get("type", "relative") + x = None + y = None + for subelem in element: + tag = realtag(subelem) + if tag == "x": + x = NumberDef(subelem.text) + elif tag == "y": + y = NumberDef(subelem.text) + return cls(type, x, y) + + def __call__(self, params, rank): + return (self.x(params, rank) if self.x else 0, + self.y(params, rank) if self.y else 0) + class FireDef(object): """Fire definition (creates a bullet).""" - def __init__(self, bullet, direction=None, speed=None): + def __init__(self, bullet, direction=None, speed=None, offset=None): self.bullet = bullet self.direction = direction self.speed = speed + self.offset = offset @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" direction = None speed = None + offset = None for subelem in element.getchildren(): tag = realtag(subelem) @@ -399,9 +431,10 @@ class FireDef(object): bullet = BulletDef.FromElement(doc, subelem) elif tag == "bulletRef": bullet = BulletRef.FromElement(doc, subelem) - + elif tag == "offset": + offset = Offset.FromElement(doc, subelem) try: - fire = cls(bullet, direction, speed) + fire = cls(bullet, direction, speed, offset) except UnboundLocalError as exc: raise ParseError(str(exc)) else: @@ -414,7 +447,7 @@ class FireDef(object): direction = self.direction(params, rank) if self.speed: speed = self.speed(params, rank) - return direction, speed, actions + return direction, speed, actions, self.offset def __repr__(self): return "%s(direction=%r, speed=%r, bullet=%r)" % ( @@ -435,7 +468,6 @@ class FireRef(object): return fired def __call__(self, params, rank): - """Generate a Bullet from the FireDef and params.""" return self.fire(self.params(params, rank), rank) def __repr__(self): diff --git a/examples/normal/threefire-offset.xml b/examples/normal/threefire-offset.xml new file mode 100644 index 0000000..5ecb2b1 --- /dev/null +++ b/examples/normal/threefire-offset.xml @@ -0,0 +1,55 @@ + + + + + + + 10 + 20 * $rank * $rand + + + + + + + $1 + 0 + + 0 + + + + $1 + + 5-5 + + + + $1 + + -5-5 + + + + + 0 + + + $1 + + + + 0 + 1 + + + 4 + + + + 1 + 1 + + + + diff --git a/examples/template.xml b/examples/template.xml index 9ca98c3..4b93df8 100644 --- a/examples/template.xml +++ b/examples/template.xml @@ -1,7 +1,8 @@ - + -- 2.30.2