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:
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):
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)
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:
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)" % (
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):
--- /dev/null
+<?xml version="1.0" ?>\r
+<!DOCTYPE bulletml SYSTEM "http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/bulletml.dtd">\r
+\r
+<bulletml xmlns="http://www.asahi-net.or.jp/~cs8k-cyu/bulletml"\r
+ xmlns:py="http://code.google.com/p/python-bulletml/">\r
+ <action label="top">\r
+ <actionRef label="threefire">\r
+ <param>10 + 20 * $rank * $rand</param>\r
+ </actionRef>\r
+ </action>\r
+\r
+ <action label="threefire">\r
+ <fire>\r
+ <bulletRef label="dropper">\r
+ <param>$1</param>\r
+ <param>0</param>\r
+ </bulletRef>\r
+ <direction>0</direction>\r
+ </fire>\r
+ <fire>\r
+ <bulletRef label="dropper">\r
+ <param>$1</param>\r
+ </bulletRef>\r
+ <py:offset><py:x>5</py:x><py:y>-5</py:y></py:offset>\r
+ </fire>\r
+ <fire>\r
+ <bulletRef label="dropper">\r
+ <param>$1</param>\r
+ </bulletRef>\r
+ <py:offset><py:x>-5</py:x><py:y>-5</py:y></py:offset>\r
+ </fire>\r
+ </action>\r
+\r
+ <bullet label="dropper">\r
+ <speed>0</speed>\r
+ <action>\r
+ <repeat>\r
+ <times>$1</times>\r
+ <action>\r
+ <fire>\r
+ <bullet>\r
+ <direction type="relative">0</direction>\r
+ <speed type="absolute">1</speed>\r
+ </bullet>\r
+ </fire>\r
+ <wait>4</wait>\r
+ </action>\r
+ </repeat>\r
+ <changeSpeed>\r
+ <term>1</term>\r
+ <speed>1</speed>\r
+ </changeSpeed>\r
+ </action>\r
+ </bullet>\r
+</bulletml>\r