Create Bullets from documents.
[python-bulletml.git] / bulletml / impl.py
1 """BulletML implementation.
2
3 http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html
4 """
5
6 from __future__ import division
7
8 import math
9
10 from bulletml import parser, errors
11
12 # TODO(jfw): This is very non-Pythonic, it's pretty much just the
13 # BulletML reference ActionImpl translated to Python.
14
15 class Action(object):
16 """Running action implementation."""
17
18 def __init__(self, owner, parent, actions, params, rank):
19 self.actions = actions
20 self.parent = parent
21 self.repeat = 0
22 self.wait_frames = 0
23 self.speed = 0
24 self.speed_frames = 0
25 self.aim_direction = 0
26 self.direction = 0
27 self.direction_frames = 0
28 self.aiming = False
29 self.aim_mx = 0
30 self.aim_my = 0
31 self.mx = 0
32 self.my = 0
33 self.owner = owner
34 self.accel_frames = 0
35 self.previous_fire_direction = 0
36 self.previous_fire_speed = 0
37 self.params = params
38 self.rank = rank
39 self.pc = -1
40 if parent:
41 self.copy_state(parent)
42
43 def vanish(self):
44 if self.parent:
45 self.parent.vanish()
46 self.repeat = 0
47 self.pc = None
48
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
56 self.mx = other.mx
57 self.my = other.my
58 self.previous_fire_direction = other.previous_fire_direction
59 self.previous_fire_speed = other.previous_fire_speed
60
61 def step(self):
62 created = []
63
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:
70 if self.aiming:
71 self.owner.direction += self.owner.aim
72 else:
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
78
79 if self.pc is None:
80 return created
81
82 if self.wait_frames > 0:
83 self.wait_frames -= 1
84 return created
85
86 while True:
87 self.pc += 1
88 if self.pc >= len(self.actions):
89 self.repeat -= 1
90 if self.repeat <= 0:
91 self.pc = None
92 if self.parent is not None:
93 self.parent.copy_state(self)
94 self.owner.replace(self, self.parent)
95 break
96 else:
97 self.pc = 0
98
99 action = self.actions[self.pc]
100
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())
107 break
108
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())
114 break
115
116 elif isinstance(action, (parser.FireDef, parser.FireRef)):
117 direction, speed, actions = action(self.params, self.rank)
118 if direction:
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
126 else:
127 direction = self.owner.aim
128 self.previous_fire_direction = direction
129
130
131 if speed:
132 speed, type = speed
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
139 else:
140 speed = 1
141 self.previous_fire_speed = speed
142
143 bullet = Bullet(self.owner.x, self.owner.y, direction, speed,
144 self.owner.target, actions, self)
145 created.append(bullet)
146
147 elif isinstance(action, parser.ChangeSpeed):
148 frames, (speed, type) = action(self.params, self.rank)
149 self.speed_frames = frames
150 if type == "sequence":
151 self.speed = speed
152 elif type == "relative":
153 self.speed = speed / frames
154 else:
155 self.speed = (speed - self.owner.speed) / frames
156
157 elif isinstance(action, parser.ChangeDirection):
158 frames, (direction, type) = action(self.params, self.rank)
159 self.direction_frames = frames
160 self.aiming = False
161 if type == "sequence":
162 self.aiming = False
163 self.direction = direction
164 else:
165 if type == "absolute":
166 self.aiming = False
167 self.direction = (
168 direction - self.owner.direction) % 360
169 elif type == "relative":
170 self.aiming = False
171 self.direction = direction
172 else:
173 self.aiming = True
174 self.direction = (
175 direction
176 + self.owner.aim
177 - self.owner.direction) % 360
178
179 while self.direction > 180:
180 self.direction -= 360
181 while self.direction < -180:
182 self.direction += 360
183 self.direction /= self.direction_frames
184
185 elif isinstance(action, parser.Accel):
186 frames, horizontal, vertical = action(self.params, self.rank)
187 self.accel_frames = frames
188 if horizontal:
189 mx, type = horizontal
190 if type == "sequence":
191 self.mx = mx
192 elif type == "absolute":
193 self.mx = (mx - self.owner.mx) / frames
194 elif type == "relative":
195 self.mx = mx / frames
196 if vertical:
197 my, type = vertical
198 if type == "sequence":
199 self.my = my
200 elif type == "absolute":
201 self.my = (my - self.owner.my) / frames
202 elif type == "relative":
203 self.my = my / frames
204
205 elif isinstance(action, parser.Wait):
206 self.wait_frames = action(self.params, self.rank)
207 break
208
209 elif isinstance(action, parser.Vanish):
210 self.owner.vanish()
211 break
212
213 return created
214
215 class Bullet(object):
216 """Simple bullet implementation."""
217
218 def __init__(self, x=0, y=0, direction=0, speed=0, target=None,
219 actions=(), parent=None, rank=None):
220 self.x = self.px = x
221 self.y = self.py = y
222 self.mx = 0
223 self.my = 0
224 self.direction = direction
225 self.speed = speed
226 self.vanished = False
227 self.target = target
228 self.actions = []
229 if rank is None:
230 rank = parent.rank if parent else 0.5
231 for action, params in actions:
232 self.actions.append(
233 Action(self, parent, action, params, rank))
234
235 def __repr__(self):
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,
240 self.vanished)
241
242 @property
243 def aim(self):
244 """Angle to the target."""
245 if self.target is None:
246 return self.direction
247 else:
248 return math.degrees(
249 math.atan2(self.target.x - self.x, self.target.y - self.y))
250
251 @property
252 def finished(self):
253 return self.vanished and not self.actions
254
255 def vanish(self):
256 """Vanish this bullet and stop all actions."""
257 self.vanished = True
258 for action in self.actions:
259 action.vanish()
260 self.actions = []
261
262 def replace(self, old, new):
263 try:
264 idx = self.actions.index(old)
265 except ValueError:
266 pass
267 else:
268 self.actions[idx] = new
269
270 def step(self):
271 created = []
272
273 self.actions = filter(None, self.actions)
274
275 for action in self.actions:
276 created.extend(action.step())
277
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
281
282 return created
283
284 @classmethod
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)
289