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.
16 """Running action implementation."""
18 def __init__(self
, owner
, parent
, actions
, params
, rank
):
19 self
.actions
= actions
25 self
.aim_direction
= 0
27 self
.direction_frames
= 0
35 self
.previous_fire_direction
= 0
36 self
.previous_fire_speed
= 0
42 self
.copy_state(parent
)
45 return "%s(pc=%r, actions=%r)" % (
46 type(self
).__name
__, self
.pc
, self
.actions
)
49 """End this action and its parents."""
55 def copy_state(self
, other
):
56 """Copy fire/movement state from other to self."""
57 self
.direction_frames
= other
.direction_frames
58 self
.direction
= other
.direction
59 self
.aiming
= other
.aiming
60 self
.speed_frames
= other
.speed_frames
61 self
.speed
= other
.speed
62 self
.accel_frames
= other
.accel_frames
65 self
.previous_fire_direction
= other
.previous_fire_direction
66 self
.previous_fire_speed
= other
.previous_fire_speed
69 """Advance by one frame."""
72 if self
.speed_frames
> 0:
73 self
.speed_frames
-= 1
74 self
.owner
.speed
+= self
.speed
76 if self
.direction_frames
> 0:
77 self
.direction_frames
-= 1
78 if self
.direction_frames
<= 0:
80 self
.owner
.direction
+= self
.owner
.aim
82 self
.owner
.direction
+= self
.direction
84 if self
.accel_frames
> 0:
85 self
.accel_frames
-= 1
86 self
.owner
.mx
+= self
.mx
87 self
.owner
.my
+= self
.my
92 if self
.wait_frames
> 0:
99 if self
.pc
>= len(self
.actions
):
104 if self
.parent
is not None:
105 self
.parent
.copy_state(self
)
106 self
.owner
.replace(self
, self
.parent
)
111 action
= self
.actions
[self
.pc
]
113 if isinstance(action
, parser
.Repeat
):
114 repeat
, (actions
, params
) = action(self
.params
, self
.rank
)
115 child
= Action(self
.owner
, self
, actions
, params
, self
.rank
)
116 child
.repeat
= repeat
117 self
.owner
.replace(self
, child
)
118 created
.extend(child
.step())
121 elif isinstance(action
, (parser
.ActionDef
, parser
.ActionRef
)):
122 actions
, params
= action(self
.params
, self
.rank
)
123 child
= Action(self
.owner
, self
, actions
, params
, self
.rank
)
124 self
.owner
.replace(self
, child
)
125 created
.extend(child
.step())
128 elif isinstance(action
, (parser
.FireDef
, parser
.FireRef
)):
129 direction
, speed
, actions
= action(self
.params
, self
.rank
)
131 direction
, type = direction
132 if type == "aim" or type is None:
133 direction
+= self
.owner
.aim
134 elif type == "sequence":
135 direction
+= self
.previous_fire_direction
136 elif type == "relative":
137 direction
+= self
.owner
.direction
139 direction
= self
.owner
.aim
140 self
.previous_fire_direction
= direction
145 if type == "sequence":
146 speed
+= self
.previous_fire_speed
147 elif type == "relative":
148 # FIXME(jfw): Reference impl uses prvFireSpeed
149 # here? That doesn't seem right at all.
150 speed
+= self
.owner
.speed
153 self
.previous_fire_speed
= speed
155 bullet
= Bullet(self
.owner
.x
, self
.owner
.y
, direction
, speed
,
156 self
.owner
.target
, actions
, self
)
157 created
.append(bullet
)
159 elif isinstance(action
, parser
.ChangeSpeed
):
160 frames
, (speed
, type) = action(self
.params
, self
.rank
)
161 self
.speed_frames
= frames
162 if type == "sequence":
164 elif type == "relative":
165 self
.speed
= speed
/ frames
167 self
.speed
= (speed
- self
.owner
.speed
) / frames
169 elif isinstance(action
, parser
.ChangeDirection
):
170 frames
, (direction
, type) = action(self
.params
, self
.rank
)
171 self
.direction_frames
= frames
173 if type == "sequence":
175 self
.direction
= direction
177 if type == "absolute":
180 direction
- self
.owner
.direction
) % 360
181 elif type == "relative":
183 self
.direction
= direction
189 - self
.owner
.direction
) % 360
191 while self
.direction
> 180:
192 self
.direction
-= 360
193 while self
.direction
< -180:
194 self
.direction
+= 360
195 self
.direction
/= self
.direction_frames
197 elif isinstance(action
, parser
.Accel
):
198 frames
, horizontal
, vertical
= action(self
.params
, self
.rank
)
199 self
.accel_frames
= frames
201 mx
, type = horizontal
202 if type == "sequence":
204 elif type == "absolute":
205 self
.mx
= (mx
- self
.owner
.mx
) / frames
206 elif type == "relative":
207 self
.mx
= mx
/ frames
210 if type == "sequence":
212 elif type == "absolute":
213 self
.my
= (my
- self
.owner
.my
) / frames
214 elif type == "relative":
215 self
.my
= my
/ frames
217 elif isinstance(action
, parser
.Wait
):
218 self
.wait_frames
= action(self
.params
, self
.rank
)
221 elif isinstance(action
, parser
.Vanish
):
227 class Bullet(object):
228 """Simple bullet implementation."""
230 def __init__(self
, x
=0, y
=0, direction
=0, speed
=0, target
=None,
231 actions
=(), parent
=None, rank
=None):
236 self
.direction
= direction
238 self
.vanished
= False
242 rank
= parent
.rank
if parent
else 0.5
243 # New bullets reset the parent hierarchy.
244 self
.actions
= [Action(self
, None, action
, params
, rank
)
245 for action
, params
in actions
]
248 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
249 "actions=%r, target=%r, vanished=%r)") % (
250 type(self
).__name
__, self
.x
, self
.y
, (self
.mx
, self
.my
),
251 self
.direction
, self
.speed
, self
.actions
, self
.target
,
256 """Angle to the target."""
257 if self
.target
is None:
258 return self
.direction
261 math
.atan2(self
.target
.x
- self
.x
, self
.y
- self
.target
.y
))
265 """Check if this bullet is finished running."""
266 for action
in self
.actions
:
267 if not action
.finished
:
272 """Vanish this bullet and stop all actions."""
274 for action
in self
.actions
:
278 def replace(self
, old
, new
):
279 """Replace an active action with another."""
281 idx
= self
.actions
.index(old
)
285 self
.actions
[idx
] = new
288 """Advance by one frame."""
291 for action
in self
.actions
:
292 created
.extend(action
.step())
294 direction
= math
.radians(self
.direction
)
297 self
.x
+= self
.mx
+ math
.sin(direction
) * self
.speed
298 self
.y
+= self
.my
- math
.cos(direction
) * self
.speed
303 def FromDoc(cls
, doc
, params
=(), x
=0, y
=0, speed
=0, direction
=0,
304 target
=None, rank
=0.5):
305 """Construct a bullet from top-level actions in a document."""
306 actions
= [act(params
, rank
) for act
in doc
.top
]
307 return cls(x
, y
, direction
, speed
, target
, actions
, rank
=rank
)