Mercurial > touhou
comparison pytouhou/game/bullet.pyx @ 257:9b699e8de4a7
Switch Bullet to Cython to improve performances.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 22 Jan 2012 21:54:07 +0100 |
parents | pytouhou/game/bullet.py@507dfd6efe0c |
children | 620134bc51f4 |
comparison
equal
deleted
inserted
replaced
256:507dfd6efe0c | 257:9b699e8de4a7 |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 from math import cos, sin, atan2, pi | |
16 | |
17 from pytouhou.utils.interpolator import Interpolator | |
18 from pytouhou.vm.anmrunner import ANMRunner | |
19 from pytouhou.game.sprite import Sprite | |
20 | |
21 | |
22 LAUNCHING, LAUNCHED, CANCELLED = range(3) | |
23 | |
24 cdef class Bullet(object): | |
25 cdef public unsigned int _state, flags, frame, sprite_idx_offset | |
26 cdef public double dx, dy, angle, speed #TODO | |
27 cdef public object player_bullet, target | |
28 cdef public object _game, _sprite, _anmrunner, _removed, _bullet_type, _was_visible | |
29 cdef public object attributes, damage, hitbox_half_size, speed_interpolator, grazed | |
30 cdef public object x, y #TODO | |
31 | |
32 def __init__(self, pos, bullet_type, sprite_idx_offset, | |
33 angle, speed, attributes, flags, target, game, | |
34 player_bullet=False, damage=0, hitbox=None): | |
35 self._game = game | |
36 self._sprite = None | |
37 self._anmrunner = None | |
38 self._removed = False | |
39 self._bullet_type = bullet_type | |
40 self._was_visible = True | |
41 self._state = LAUNCHING | |
42 | |
43 if hitbox: | |
44 self.hitbox_half_size = (hitbox[0] / 2., hitbox[1] / 2.) | |
45 else: | |
46 self.hitbox_half_size = (bullet_type.hitbox_size / 2., bullet_type.hitbox_size / 2.) | |
47 | |
48 self.speed_interpolator = None | |
49 self.frame = 0 | |
50 self.grazed = False | |
51 | |
52 self.target = target | |
53 | |
54 self.sprite_idx_offset = sprite_idx_offset | |
55 | |
56 self.flags = flags | |
57 self.attributes = list(attributes) | |
58 | |
59 self.x, self.y = pos | |
60 self.angle = angle | |
61 self.speed = speed | |
62 self.dx, self.dy = cos(angle) * speed, sin(angle) * speed | |
63 | |
64 self.player_bullet = player_bullet | |
65 self.damage = damage | |
66 | |
67 #TODO | |
68 if flags & 14: | |
69 if flags & 2: | |
70 index = bullet_type.launch_anim2_index | |
71 launch_mult = bullet_type.launch_anim_penalties[0] | |
72 elif flags & 4: | |
73 index = bullet_type.launch_anim4_index | |
74 launch_mult = bullet_type.launch_anim_penalties[1] | |
75 else: | |
76 index = bullet_type.launch_anim8_index | |
77 launch_mult = bullet_type.launch_anim_penalties[2] | |
78 self.dx, self.dy = self.dx * launch_mult, self.dy * launch_mult | |
79 self._sprite = Sprite() | |
80 self._anmrunner = ANMRunner(bullet_type.anm_wrapper, | |
81 index, self._sprite, | |
82 bullet_type.launch_anim_offsets[sprite_idx_offset]) | |
83 self._anmrunner.run_frame() | |
84 else: | |
85 self.launch() | |
86 | |
87 if self.player_bullet: | |
88 self._sprite.angle = angle - pi | |
89 else: | |
90 self._sprite.angle = angle | |
91 | |
92 | |
93 def is_visible(self, screen_width, screen_height): | |
94 tx, ty, tw, th = self._sprite.texcoords | |
95 x, y = self.x, self.y | |
96 | |
97 max_x = tw / 2. | |
98 max_y = th / 2. | |
99 | |
100 if (max_x < x - screen_width | |
101 or max_x < -x | |
102 or max_y < y - screen_height | |
103 or max_y < -y): | |
104 return False | |
105 return True | |
106 | |
107 | |
108 def set_anim(self, sprite_idx_offset=None): | |
109 if sprite_idx_offset is not None: | |
110 self.sprite_idx_offset = sprite_idx_offset | |
111 | |
112 bt = self._bullet_type | |
113 self._sprite = Sprite() | |
114 if self.player_bullet: | |
115 self._sprite.angle = self.angle - pi | |
116 else: | |
117 self._sprite.angle = self.angle | |
118 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.anim_index, | |
119 self._sprite, self.sprite_idx_offset) | |
120 self._anmrunner.run_frame() | |
121 | |
122 | |
123 def launch(self): | |
124 self._state = LAUNCHED | |
125 self.frame = 0 | |
126 self.set_anim() | |
127 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed | |
128 if self.flags & 1: | |
129 self.speed_interpolator = Interpolator((self.speed + 5.,), 0, | |
130 (self.speed,), 16) | |
131 | |
132 | |
133 def collide(self): | |
134 self.cancel() | |
135 | |
136 | |
137 def cancel(self): | |
138 # Cancel animation | |
139 bt = self._bullet_type | |
140 self._sprite = Sprite() | |
141 if self.player_bullet: | |
142 self._sprite.angle = self.angle - pi | |
143 else: | |
144 self._sprite.angle = self.angle | |
145 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.cancel_anim_index, | |
146 self._sprite, bt.launch_anim_offsets[self.sprite_idx_offset]) | |
147 self._anmrunner.run_frame() | |
148 self.dx, self.dy = self.dx / 2., self.dy / 2. | |
149 | |
150 # Change update method | |
151 self._state = CANCELLED | |
152 | |
153 # Do not use this one for collisions anymore | |
154 if self.player_bullet: | |
155 self._game.players_bullets.remove(self) | |
156 else: | |
157 self._game.bullets.remove(self) | |
158 self._game.cancelled_bullets.append(self) | |
159 | |
160 | |
161 def update(self): | |
162 if self._anmrunner is not None and not self._anmrunner.run_frame(): | |
163 if self._state == LAUNCHING: | |
164 #TODO: check if it doesn't skip a frame | |
165 self.launch() | |
166 elif self._state == CANCELLED: | |
167 self._removed = True | |
168 else: | |
169 self._anmrunner = None | |
170 | |
171 if self._state == LAUNCHING: | |
172 pass | |
173 elif self._state == CANCELLED: | |
174 pass | |
175 elif self.flags & 1: | |
176 # Initial speed burst | |
177 #TODO: use frame instead of interpolator? | |
178 if not self.speed_interpolator: | |
179 self.flags &= ~1 | |
180 elif self.flags & 16: | |
181 # Each frame, add a vector to the speed vector | |
182 length, angle = self.attributes[4:6] | |
183 angle = self.angle if angle < -900.0 else angle #TODO: is that right? | |
184 self.dx += cos(angle) * length | |
185 self.dy += sin(angle) * length | |
186 self.speed = (self.dx ** 2 + self.dy ** 2) ** 0.5 | |
187 self.angle = self._sprite.angle = atan2(self.dy, self.dx) | |
188 if self._sprite.automatic_orientation: | |
189 self._sprite._changed = True | |
190 if self.frame == self.attributes[0]: #TODO: include last frame, or not? | |
191 self.flags &= ~16 | |
192 elif self.flags & 32: | |
193 # Each frame, accelerate and rotate | |
194 #TODO: check | |
195 acceleration, angular_speed = self.attributes[4:6] | |
196 self.speed += acceleration | |
197 self.angle += angular_speed | |
198 self.dx = cos(self.angle) * self.speed | |
199 self.dy = sin(self.angle) * self.speed | |
200 self._sprite.angle = self.angle | |
201 if self._sprite.automatic_orientation: | |
202 self._sprite._changed = True | |
203 if self.frame == self.attributes[0]: | |
204 self.flags &= ~32 | |
205 elif self.flags & 448: | |
206 #TODO: check | |
207 frame, count = self.attributes[0:2] | |
208 angle, speed = self.attributes[4:6] | |
209 if self.frame % frame == 0: | |
210 count = count - 1 | |
211 | |
212 if self.frame != 0: | |
213 self.speed = speed | |
214 | |
215 if self.flags & 64: | |
216 self.angle += angle | |
217 elif self.flags & 128: | |
218 self.angle = atan2(self.target.y - self.y, | |
219 self.target.x - self.x) + angle | |
220 elif self.flags & 256: | |
221 self.angle = angle | |
222 | |
223 self.dx = cos(self.angle) * self.speed | |
224 self.dy = sin(self.angle) * self.speed | |
225 self._sprite.angle = self.angle | |
226 if self._sprite.automatic_orientation: | |
227 self._sprite._changed = True | |
228 | |
229 if count >= 0: | |
230 self.speed_interpolator = Interpolator((self.speed,), self.frame, | |
231 (0.,), self.frame + frame - 1) | |
232 else: | |
233 self.flags &= ~448 | |
234 | |
235 self.attributes[1] = count | |
236 #TODO: other flags | |
237 | |
238 # Common updates | |
239 | |
240 if self.speed_interpolator: | |
241 self.speed_interpolator.update(self.frame) | |
242 self.speed, = self.speed_interpolator.values | |
243 self.dx = cos(self.angle) * self.speed | |
244 self.dy = sin(self.angle) * self.speed | |
245 | |
246 self.x += self.dx | |
247 self.y += self.dy | |
248 | |
249 self.frame += 1 | |
250 |