comparison pytouhou/game/bullet.pyx @ 335:2350147cf043

Fix bullet cancellation and removal
author Thibaut Girka <thib@sitedethib.com>
date Sun, 01 Jul 2012 11:43:38 +0200
parents 56523a16db1d
children b5ab2ad06de8
comparison
equal deleted inserted replaced
334:4eca6130f118 335:2350147cf043
20 20
21 21
22 LAUNCHING, LAUNCHED, CANCELLED = range(3) 22 LAUNCHING, LAUNCHED, CANCELLED = range(3)
23 23
24 cdef class Bullet(object): 24 cdef class Bullet(object):
25 cdef public unsigned int _state, flags, frame, sprite_idx_offset 25 cdef public unsigned int state, flags, frame, sprite_idx_offset
26 cdef public double dx, dy, angle, speed #TODO 26 cdef public double dx, dy, angle, speed #TODO
27 cdef public object player_bullet, target 27 cdef public object player_bullet, target
28 cdef public object _game, _bullet_type 28 cdef public object _game, _bullet_type
29 cdef public object sprite, anmrunner, removed, was_visible 29 cdef public object sprite, anmrunner, removed, was_visible
30 cdef public object attributes, damage, hitbox_half_size, speed_interpolator, grazed 30 cdef public object attributes, damage, hitbox_half_size, speed_interpolator, grazed
33 def __init__(self, pos, bullet_type, sprite_idx_offset, 33 def __init__(self, pos, bullet_type, sprite_idx_offset,
34 angle, speed, attributes, flags, target, game, 34 angle, speed, attributes, flags, target, game,
35 player_bullet=False, damage=0, hitbox=None): 35 player_bullet=False, damage=0, hitbox=None):
36 self._game = game 36 self._game = game
37 self._bullet_type = bullet_type 37 self._bullet_type = bullet_type
38 self._state = LAUNCHING 38 self.state = LAUNCHING
39 self.sprite = None 39 self.sprite = None
40 self.anmrunner = None 40 self.anmrunner = None
41 self.removed = False 41 self.removed = False
42 self.was_visible = True 42 self.was_visible = True
43 43
120 self.sprite, self.sprite_idx_offset) 120 self.sprite, self.sprite_idx_offset)
121 self.anmrunner.run_frame() 121 self.anmrunner.run_frame()
122 122
123 123
124 def launch(Bullet self): 124 def launch(Bullet self):
125 self._state = LAUNCHED 125 self.state = LAUNCHED
126 self.frame = 0 126 self.frame = 0
127 self.set_anim() 127 self.set_anim()
128 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed 128 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
129 129
130 if self.flags & 1: 130 if self.flags & 1:
149 self.sprite, bt.launch_anim_offsets[self.sprite_idx_offset]) 149 self.sprite, bt.launch_anim_offsets[self.sprite_idx_offset])
150 self.anmrunner.run_frame() 150 self.anmrunner.run_frame()
151 self.dx, self.dy = self.dx / 2., self.dy / 2. 151 self.dx, self.dy = self.dx / 2., self.dy / 2.
152 152
153 # Change update method 153 # Change update method
154 self._state = CANCELLED 154 self.state = CANCELLED
155 155
156 # Do not use this one for collisions anymore 156 # Do not use this one for collisions anymore
157 if self.player_bullet: 157 if self.player_bullet:
158 self._game.players_bullets.remove(self) 158 self._game.players_bullets.remove(self)
159 else: 159 else:
161 self._game.cancelled_bullets.append(self) 161 self._game.cancelled_bullets.append(self)
162 162
163 163
164 def update(Bullet self): 164 def update(Bullet self):
165 if self.anmrunner is not None and not self.anmrunner.run_frame(): 165 if self.anmrunner is not None and not self.anmrunner.run_frame():
166 if self._state == LAUNCHING: 166 if self.state == LAUNCHING:
167 #TODO: check if it doesn't skip a frame 167 #TODO: check if it doesn't skip a frame
168 self.launch() 168 self.launch()
169 elif self._state == CANCELLED: 169 elif self.state == CANCELLED:
170 self.removed = True 170 self.removed = True
171 else: 171 else:
172 self.anmrunner = None 172 self.anmrunner = None
173 173
174 if self._state == LAUNCHING: 174 if self.state == LAUNCHING:
175 pass 175 pass
176 elif self._state == CANCELLED: 176 elif self.state == CANCELLED:
177 pass 177 pass
178 elif self.flags & 1: 178 elif self.flags & 1:
179 # Initial speed burst 179 # Initial speed burst
180 #TODO: use frame instead of interpolator? 180 #TODO: use frame instead of interpolator?
181 if not self.speed_interpolator: 181 if not self.speed_interpolator: