1 """BulletML implementation.
3 http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html
6 from __future__
import division
10 from bulletml
import parser
12 # TODO(jfw): This is very non-Pythonic, it's pretty much just the
13 # BulletML reference ActionImpl translated to Python.
18 """Running action implementation."""
20 def __init__(self
, owner
, parent
, actions
, params
, rank
):
21 self
.actions
= actions
27 self
.aim_direction
= 0
29 self
.direction_frames
= 0
37 self
.previous_fire_direction
= 0
38 self
.previous_fire_speed
= 0
44 self
.copy_state(parent
)
47 return "%s(pc=%r, actions=%r)" % (
48 type(self
).__name
__, self
.pc
, self
.actions
)
51 """End this action and its parents."""
57 def copy_state(self
, other
):
58 """Copy fire/movement state from other to self."""
59 self
.direction_frames
= other
.direction_frames
60 self
.direction
= other
.direction
61 self
.aiming
= other
.aiming
62 self
.speed_frames
= other
.speed_frames
63 self
.speed
= other
.speed
64 self
.accel_frames
= other
.accel_frames
67 self
.previous_fire_direction
= other
.previous_fire_direction
68 self
.previous_fire_speed
= other
.previous_fire_speed
71 """Advance by one frame."""
74 if self
.speed_frames
> 0:
75 self
.speed_frames
-= 1
76 self
.owner
.speed
+= self
.speed
78 if self
.direction_frames
> 0:
79 self
.direction_frames
-= 1
80 if self
.direction_frames
<= 0:
82 self
.owner
.direction
+= self
.owner
.aim
84 self
.owner
.direction
+= self
.direction
86 if self
.accel_frames
> 0:
87 self
.accel_frames
-= 1
88 self
.owner
.mx
+= self
.mx
89 self
.owner
.my
+= self
.my
94 if self
.wait_frames
> 0:
101 if self
.pc
>= len(self
.actions
):
106 if self
.parent
is not None:
107 self
.parent
.copy_state(self
)
108 self
.owner
.replace(self
, self
.parent
)
113 action
= self
.actions
[self
.pc
]
115 if isinstance(action
, parser
.Repeat
):
116 repeat
, (actions
, params
) = action(self
.params
, self
.rank
)
117 child
= Action(self
.owner
, self
, actions
, params
, self
.rank
)
118 child
.repeat
= repeat
119 self
.owner
.replace(self
, child
)
120 created
.extend(child
.step())
123 elif isinstance(action
, (parser
.ActionDef
, parser
.ActionRef
)):
124 actions
, params
= action(self
.params
, self
.rank
)
125 child
= Action(self
.owner
, self
, actions
, params
, self
.rank
)
126 self
.owner
.replace(self
, child
)
127 created
.extend(child
.step())
130 elif isinstance(action
, (parser
.FireDef
, parser
.FireRef
)):
131 direction
, speed
, actions
= action(self
.params
, self
.rank
)
133 direction
, type = direction
134 if type == "aim" or type is None:
135 direction
+= self
.owner
.aim
136 elif type == "sequence":
137 direction
+= self
.previous_fire_direction
138 elif type == "relative":
139 direction
+= self
.owner
.direction
141 direction
= self
.owner
.aim
142 self
.previous_fire_direction
= direction
147 if type == "sequence":
148 speed
+= self
.previous_fire_speed
149 elif type == "relative":
150 # FIXME(jfw): Reference impl uses prvFireSpeed
151 # here? That doesn't seem right at all.
152 speed
+= self
.owner
.speed
155 self
.previous_fire_speed
= speed
157 bullet
= Bullet(self
.owner
.x
, self
.owner
.y
, direction
, speed
,
158 self
.owner
.target
, actions
, self
)
159 created
.append(bullet
)
161 elif isinstance(action
, parser
.ChangeSpeed
):
162 frames
, (speed
, type) = action(self
.params
, self
.rank
)
163 self
.speed_frames
= frames
164 if type == "sequence":
166 elif type == "relative":
167 self
.speed
= speed
/ frames
169 self
.speed
= (speed
- self
.owner
.speed
) / frames
171 elif isinstance(action
, parser
.ChangeDirection
):
172 frames
, (direction
, type) = action(self
.params
, self
.rank
)
173 self
.direction_frames
= frames
175 if type == "sequence":
177 self
.direction
= direction
179 if type == "absolute":
182 direction
- self
.owner
.direction
) % PI_2
183 elif type == "relative":
185 self
.direction
= direction
191 - self
.owner
.direction
) % PI_2
193 while self
.direction
> math
.pi
:
194 self
.direction
-= PI_2
195 while self
.direction
< -math
.pi
:
196 self
.direction
+= PI_2
197 self
.direction
/= self
.direction_frames
199 elif isinstance(action
, parser
.Accel
):
200 frames
, horizontal
, vertical
= action(self
.params
, self
.rank
)
201 self
.accel_frames
= frames
203 mx
, type = horizontal
204 if type == "sequence":
206 elif type == "absolute":
207 self
.mx
= (mx
- self
.owner
.mx
) / frames
208 elif type == "relative":
209 self
.mx
= mx
/ frames
212 if type == "sequence":
214 elif type == "absolute":
215 self
.my
= (my
- self
.owner
.my
) / frames
216 elif type == "relative":
217 self
.my
= my
/ frames
219 elif isinstance(action
, parser
.Wait
):
220 self
.wait_frames
= action(self
.params
, self
.rank
)
223 elif isinstance(action
, parser
.Vanish
):
229 class Bullet(object):
230 """Simple bullet implementation."""
232 def __init__(self
, x
=0, y
=0, direction
=0, speed
=0, target
=None,
233 actions
=(), parent
=None, rank
=None):
238 self
.direction
= direction
240 self
.vanished
= False
244 rank
= parent
.rank
if parent
else 0.5
245 # New bullets reset the parent hierarchy.
246 self
.actions
= [Action(self
, None, action
, params
, rank
)
247 for action
, params
in actions
]
250 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
251 "actions=%r, target=%r, vanished=%r)") % (
252 type(self
).__name
__, self
.x
, self
.y
, (self
.mx
, self
.my
),
253 self
.direction
, self
.speed
, self
.actions
, self
.target
,
258 """Angle to the target."""
259 if self
.target
is None:
260 return self
.direction
262 return math
.atan2(self
.target
.x
- self
.x
, self
.y
- self
.target
.y
)
266 """Check if this bullet is finished running."""
267 if not self
.vanished
:
269 for action
in self
.actions
:
270 if not action
.finished
:
275 """Vanish this bullet and stop all actions."""
277 for action
in self
.actions
:
281 def replace(self
, old
, new
):
282 """Replace an active action with another."""
284 idx
= self
.actions
.index(old
)
288 self
.actions
[idx
] = new
291 """Advance by one frame."""
294 for action
in self
.actions
:
295 created
.extend(action
.step())
299 self
.x
+= self
.mx
+ math
.sin(self
.direction
) * self
.speed
300 self
.y
+= self
.my
- math
.cos(self
.direction
) * self
.speed
305 def FromDoc(cls
, doc
, params
=(), x
=0, y
=0, speed
=0, direction
=0,
306 target
=None, rank
=0.5):
307 """Construct a bullet from top-level actions in a document."""
308 actions
= [act(params
, rank
) for act
in doc
.top
]
309 return cls(x
, y
, direction
, speed
, target
, actions
, rank
=rank
)