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
, errors
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
41 self
.copy_state(parent
)
49 def copy_state(self
, other
):
50 self
.direction_frames
= other
.direction_frames
51 self
.direction
= other
.direction
52 self
.aiming
= other
.aiming
53 self
.speed_frames
= other
.speed_frames
54 self
.speed
= other
.speed
55 self
.accel_frames
= other
.accel_frames
58 self
.previous_fire_direction
= other
.previous_fire_direction
59 self
.previous_fire_speed
= other
.previous_fire_speed
64 if self
.speed_frames
> 0:
65 self
.speed_frames
-= 1
66 self
.owner
.speed
+= self
.speed
67 if self
.direction_frames
> 0:
68 self
.direction_frames
-= 1
69 if self
.direction_frames
<= 0:
71 self
.owner
.direction
+= self
.owner
.aim
73 self
.owner
.direction
+= self
.direction
74 if self
.accel_frames
> 0:
75 self
.accel_frames
-= 1
76 self
.owner
.mx
+= self
.mx
77 self
.owner
.my
+= self
.my
82 if self
.wait_frames
> 0:
88 if self
.pc
>= len(self
.actions
):
92 if self
.parent
is not None:
93 self
.parent
.copy_state(self
)
94 self
.owner
.replace(self
, self
.parent
)
99 action
= self
.actions
[self
.pc
]
101 if isinstance(action
, parser
.Repeat
):
102 repeat
, (actions
, params
) = action(self
.params
, self
.rank
)
103 child
= Action(self
.owner
, self
, actions
, params
, self
.rank
)
104 child
.repeat
= repeat
105 self
.owner
.replace(self
, child
)
106 created
.extend(child
.step())
109 elif isinstance(action
, (parser
.ActionDef
, parser
.ActionRef
)):
110 actions
, params
= action(self
.params
, self
.rank
)
111 child
= Action(self
.owner
, self
, actions
, params
, self
.rank
)
112 self
.owner
.replace(self
, child
)
113 created
.extend(child
.step())
116 elif isinstance(action
, (parser
.FireDef
, parser
.FireRef
)):
117 direction
, speed
, actions
= action(self
.params
, self
.rank
)
119 direction
, type = direction
120 if type == "aim" or type is None:
121 direction
+= self
.owner
.aim
122 elif type == "sequence":
123 direction
+= self
.previous_fire_direction
124 elif type == "relative":
125 direction
+= self
.owner
.direction
127 direction
= self
.owner
.aim
128 self
.previous_fire_direction
= direction
133 if type == "sequence":
134 speed
+= self
.previous_fire_speed
135 elif type == "relative":
136 # FIXME(jfw): Reference impl uses prvFireSpeed
137 # here? That doesn't seem right at all.
138 speed
+= self
.owner
.speed
141 self
.previous_fire_speed
= speed
143 bullet
= Bullet(self
.owner
.x
, self
.owner
.y
, direction
, speed
,
144 self
.owner
.target
, actions
, self
)
145 created
.append(bullet
)
147 elif isinstance(action
, parser
.ChangeSpeed
):
148 frames
, (speed
, type) = action(self
.params
, self
.rank
)
149 self
.speed_frames
= frames
150 if type == "sequence":
152 elif type == "relative":
153 self
.speed
= speed
/ frames
155 self
.speed
= (speed
- self
.owner
.speed
) / frames
157 elif isinstance(action
, parser
.ChangeDirection
):
158 frames
, (direction
, type) = action(self
.params
, self
.rank
)
159 self
.direction_frames
= frames
161 if type == "sequence":
163 self
.direction
= direction
165 if type == "absolute":
168 direction
- self
.owner
.direction
) % 360
169 elif type == "relative":
171 self
.direction
= direction
177 - self
.owner
.direction
) % 360
179 while self
.direction
> 180:
180 self
.direction
-= 360
181 while self
.direction
< -180:
182 self
.direction
+= 360
183 self
.direction
/= self
.direction_frames
185 elif isinstance(action
, parser
.Accel
):
186 frames
, horizontal
, vertical
= action(self
.params
, self
.rank
)
187 self
.accel_frames
= frames
189 mx
, type = horizontal
190 if type == "sequence":
192 elif type == "absolute":
193 self
.mx
= (mx
- self
.owner
.mx
) / frames
194 elif type == "relative":
195 self
.mx
= mx
/ frames
198 if type == "sequence":
200 elif type == "absolute":
201 self
.my
= (my
- self
.owner
.my
) / frames
202 elif type == "relative":
203 self
.my
= my
/ frames
205 elif isinstance(action
, parser
.Wait
):
206 self
.wait_frames
= action(self
.params
, self
.rank
)
209 elif isinstance(action
, parser
.Vanish
):
215 class Bullet(object):
216 """Simple bullet implementation."""
218 def __init__(self
, x
=0, y
=0, direction
=0, speed
=0, target
=None,
219 actions
=(), parent
=None, rank
=None):
224 self
.direction
= direction
226 self
.vanished
= False
230 rank
= parent
.rank
if parent
else 0.5
231 for action
, params
in actions
:
233 Action(self
, parent
, action
, params
, rank
))
236 return ("%s(%r, %r, accel=%r, direction=%r, speed=%r, "
237 "actions=%r, target=%r, vanished=%r)") % (
238 type(self
).__name
__, self
.x
, self
.y
, (self
.mx
, self
.my
),
239 self
.direction
, self
.speed
, self
.actions
, self
.target
,
244 """Angle to the target."""
245 if self
.target
is None:
246 return self
.direction
249 math
.atan2(self
.target
.x
- self
.x
, self
.target
.y
- self
.y
))
253 return self
.vanished
and not self
.actions
256 """Vanish this bullet and stop all actions."""
258 for action
in self
.actions
:
262 def replace(self
, old
, new
):
264 idx
= self
.actions
.index(old
)
268 self
.actions
[idx
] = new
273 self
.actions
= filter(None, self
.actions
)
275 for action
in self
.actions
:
276 created
.extend(action
.step())
278 direction
= math
.radians(self
.direction
)
279 self
.x
+= self
.mx
+ math
.sin(direction
) * self
.speed
280 self
.y
+= self
.my
+ math
.cos(direction
) * self
.speed
285 def FromDoc(cls
, doc
, params
=(), x
=0, y
=0, speed
=0, direction
=0,
286 target
=None, rank
=0.5):
287 actions
= [act(params
, rank
) for act
in doc
.top
]
288 return cls(x
, y
, direction
, speed
, target
, actions
, rank
=rank
)