1 """BulletML implementation."""
3 from __future__
import division
7 from bulletml
import parser
9 # TODO(jfw): This is very non-Pythonic, it's pretty much just the
10 # BulletML reference ActionImpl translated to Python.
14 __all__
= ["Action", "Bullet"]
17 """Running action implementation."""
19 def __init__(self
, owner
, parent
, actions
, params
, rank
, repeat
=1):
20 self
.actions
= actions
27 self
.direction_frames
= 0
32 self
.previous_fire_direction
= 0
33 self
.previous_fire_speed
= 0
38 self
.copy_state(parent
)
41 return "%s(pc=%r, actions=%r)" % (
42 type(self
).__name
__, self
.pc
, self
.actions
)
45 """End this action and its parents."""
51 def copy_state(self
, other
):
52 """Copy fire/movement state from other to self."""
53 self
.direction_frames
= other
.direction_frames
54 self
.direction
= other
.direction
55 self
.aiming
= other
.aiming
56 self
.speed_frames
= other
.speed_frames
57 self
.speed
= other
.speed
58 self
.accel_frames
= other
.accel_frames
61 self
.previous_fire_direction
= other
.previous_fire_direction
62 self
.previous_fire_speed
= other
.previous_fire_speed
64 def step(self
, owner
, rank
, created
, sin
=math
.sin
, cos
=math
.cos
):
65 """Advance by one frame."""
66 s_params
= self
.params
68 if self
.speed_frames
> 0:
69 self
.speed_frames
-= 1
70 owner
.speed
+= self
.speed
72 if self
.direction_frames
> 0:
73 # The Noiz implementation was a little weird here, I think
74 # there was a bug in it that prevented it from working if
75 # the frame count was 1. I'm still not sure what the aim
76 # check is supposed to do, exactly.
77 self
.direction_frames
-= 1
78 if self
.aiming
and self
.direction_frames
<= 0:
79 owner
.direction
+= owner
.aim
81 owner
.direction
+= self
.direction
83 if self
.accel_frames
> 0:
84 self
.accel_frames
-= 1
91 if self
.wait_frames
> 0:
99 action
= self
.actions
[self
.pc
]
105 if self
.parent
is not None:
106 self
.parent
.copy_state(self
)
107 owner
.replace(self
, self
.parent
)
111 action
= self
.actions
[self
.pc
]
113 if isinstance(action
, parser
.Repeat
):
114 repeat
, (actions
, params
) = action(s_params
, rank
)
115 child
= Action(owner
, self
, actions
, params
, rank
, repeat
)
116 owner
.replace(self
, child
)
117 child
.step(owner
, rank
, created
, sin
, cos
)
120 elif isinstance(action
, (parser
.ActionDef
, parser
.ActionRef
)):
121 actions
, params
= action(s_params
, rank
)
122 child
= Action(owner
, self
, actions
, params
, rank
)
123 owner
.replace(self
, child
)
124 child
.step(owner
, rank
, created
, sin
, cos
)
127 elif isinstance(action
, (parser
.FireDef
, parser
.FireRef
)):
128 direction
, speed
, actions
, offset
= action(s_params
, rank
)
130 direction
, type = direction
131 if type == "aim" or type is None:
132 direction
+= owner
.aim
133 elif type == "sequence":
134 direction
+= self
.previous_fire_direction
135 elif type == "relative":
136 direction
+= owner
.direction
138 direction
= owner
.aim
139 self
.previous_fire_direction
= direction
143 if type == "sequence":
144 speed
+= self
.previous_fire_speed
145 elif type == "relative":
146 # The reference Noiz implementation uses
147 # prvFireSpeed here, but the standard is
148 # pretty clear -- "In case of the type is
149 # "relative", ... the speed is relative to the
150 # speed of this bullet."
154 self
.previous_fire_speed
= speed
156 x
, y
= owner
.x
, owner
.y
158 off_x
, off_y
= offset(s_params
, rank
)
159 if offset
.type == "relative":
162 x
+= c
* off_x
+ s
* off_y
163 y
+= s
* off_x
- c
* off_y
169 x
, y
, direction
, speed
, owner
.target
, actions
, self
, rank
)
170 created
.append(bullet
)
172 elif isinstance(action
, parser
.ChangeSpeed
):
173 frames
, (speed
, type) = action(s_params
, rank
)
174 self
.speed_frames
= frames
175 if type == "sequence":
177 elif type == "relative":
178 self
.speed
= speed
/ frames
180 self
.speed
= (speed
- owner
.speed
) / frames
182 elif isinstance(action
, parser
.ChangeDirection
):
183 frames
, (direction
, type) = action(s_params
, rank
)
184 self
.direction_frames
= frames
186 if type == "sequence":
187 self
.direction
= direction
189 if type == "absolute":
190 direction
-= owner
.direction
191 elif type == "relative":
192 direction
= direction
195 direction
+= owner
.aim
- owner
.direction
198 (direction
+ math
.pi
) % PI_2
- math
.pi
) / frames
200 elif isinstance(action
, parser
.Accel
):
201 frames
, horizontal
, vertical
= action(s_params
, rank
)
202 self
.accel_frames
= frames
204 mx
, type = horizontal
205 if type == "sequence":
207 elif type == "absolute":
208 self
.mx
= (mx
- owner
.mx
) / frames
209 elif type == "relative":
210 self
.mx
= mx
/ frames
213 if type == "sequence":
215 elif type == "absolute":
216 self
.my
= (my
- owner
.my
) / frames
217 elif type == "relative":
218 self
.my
= my
/ frames
220 elif isinstance(action
, parser
.Tag
):
221 owner
.tags
.add(action
.tag
)
223 elif isinstance(action
, parser
.Untag
):
225 owner
.tags
.remove(action
.tag
)
229 elif isinstance(action
, parser
.Wait
):
230 self
.wait_frames
= action(s_params
, rank
)
233 elif isinstance(action
, parser
.Vanish
):
237 class Bullet(object):
238 """Simple bullet implementation.
241 x, y - current X/Y position
242 px, py - X/Y position prior to the last step
243 mx, my - X/Y axis-oriented speed modifier ("acceleration")
244 direction - direction of movement, in radians
245 speed - speed of movement, in units per frame
246 target - object with .x and .y fields for "aim" directions
247 vanished - set to true by a <vanish> action
248 rank - game difficulty, 0 to 1, default 0.5
249 tags - string tags set by the running actions
251 Contructor Arguments:
252 x, y, direction, speed, target, rank - same as the attributes
253 actions - internal action list
254 parent - parent of actions, None for manually-created bullets
259 def __init__(self
, x
=0, y
=0, direction
=0, speed
=0, target
=None,
260 actions
=(), parent
=None, rank
=0.5):
265 self
.direction
= direction
267 self
.vanished
= False
271 # New bullets reset the parent hierarchy.
272 self
._actions
= [Action(self
, None, action
, params
, rank
)
273 for action
, params
in actions
]
276 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
277 "actions=%r, target=%r, vanished=%r)") % (
278 type(self
).__name
__, self
.x
, self
.y
, (self
.mx
, self
.my
),
279 self
.direction
, self
.speed
, self
._actions
, self
.target
,
284 """Angle to the target, in radians."""
285 if self
.target
is None:
286 return self
.direction
288 return math
.atan2(self
.target
.x
- self
.x
, self
.y
- self
.target
.y
)
292 """Check if this bullet is finished running.
294 A bullet is finished when it has vanished, and all its
295 actions have finished.
297 If this is true, the bullet should be removed from the screen.
298 (You will probably want to cull it under other circumstances
301 if not self
.vanished
:
303 for action
in self
._actions
:
304 if not action
.finished
:
309 """Vanish this bullet and stop all actions."""
311 for action
in self
._actions
:
315 def replace(self
, old
, new
):
316 """Replace an active action with another.
318 This is mostly used by actions internally to queue children.
321 idx
= self
._actions
.index(old
)
325 self
._actions
[idx
] = new
327 def step(self
, sin
=math
.sin
, cos
=math
.cos
):
328 """Advance by one frame.
330 This updates the position and velocity, and may also set the
333 It returns any new bullets this bullet spawned during this step.
337 for action
in self
._actions
:
338 action
.step(self
, self
.rank
, created
, sin
, cos
)
342 self
.x
+= self
.mx
+ sin(self
.direction
) * self
.speed
343 self
.y
+= self
.my
- cos(self
.direction
) * self
.speed