comparison pytouhou/game/bullet.pyx @ 258:620134bc51f4

Move bullet visibility check to Bullet to improve performance and prepare handling of flags 1024 and 2048.
author Thibaut Girka <thib@sitedethib.com>
date Sun, 22 Jan 2012 22:03:57 +0100
parents 9b699e8de4a7
children e22e0d73f614
comparison
equal deleted inserted replaced
257:9b699e8de4a7 258:620134bc51f4
88 self._sprite.angle = angle - pi 88 self._sprite.angle = angle - pi
89 else: 89 else:
90 self._sprite.angle = angle 90 self._sprite.angle = angle
91 91
92 92
93 def is_visible(self, screen_width, screen_height): 93 cpdef is_visible(Bullet self, screen_width, screen_height):
94 tx, ty, tw, th = self._sprite.texcoords 94 tx, ty, tw, th = self._sprite.texcoords
95 x, y = self.x, self.y 95 x, y = self.x, self.y
96 96
97 max_x = tw / 2. 97 max_x = tw / 2.
98 max_y = th / 2. 98 max_y = th / 2.
103 or max_y < -y): 103 or max_y < -y):
104 return False 104 return False
105 return True 105 return True
106 106
107 107
108 def set_anim(self, sprite_idx_offset=None): 108 def set_anim(Bullet self, sprite_idx_offset=None):
109 if sprite_idx_offset is not None: 109 if sprite_idx_offset is not None:
110 self.sprite_idx_offset = sprite_idx_offset 110 self.sprite_idx_offset = sprite_idx_offset
111 111
112 bt = self._bullet_type 112 bt = self._bullet_type
113 self._sprite = Sprite() 113 self._sprite = Sprite()
118 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.anim_index, 118 self._anmrunner = ANMRunner(bt.anm_wrapper, bt.anim_index,
119 self._sprite, self.sprite_idx_offset) 119 self._sprite, self.sprite_idx_offset)
120 self._anmrunner.run_frame() 120 self._anmrunner.run_frame()
121 121
122 122
123 def launch(self): 123 def launch(Bullet self):
124 self._state = LAUNCHED 124 self._state = LAUNCHED
125 self.frame = 0 125 self.frame = 0
126 self.set_anim() 126 self.set_anim()
127 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed 127 self.dx, self.dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
128 if self.flags & 1: 128 if self.flags & 1:
129 self.speed_interpolator = Interpolator((self.speed + 5.,), 0, 129 self.speed_interpolator = Interpolator((self.speed + 5.,), 0,
130 (self.speed,), 16) 130 (self.speed,), 16)
131 131
132 132
133 def collide(self): 133 def collide(Bullet self):
134 self.cancel() 134 self.cancel()
135 135
136 136
137 def cancel(self): 137 def cancel(Bullet self):
138 # Cancel animation 138 # Cancel animation
139 bt = self._bullet_type 139 bt = self._bullet_type
140 self._sprite = Sprite() 140 self._sprite = Sprite()
141 if self.player_bullet: 141 if self.player_bullet:
142 self._sprite.angle = self.angle - pi 142 self._sprite.angle = self.angle - pi
156 else: 156 else:
157 self._game.bullets.remove(self) 157 self._game.bullets.remove(self)
158 self._game.cancelled_bullets.append(self) 158 self._game.cancelled_bullets.append(self)
159 159
160 160
161 def update(self): 161 def update(Bullet self):
162 if self._anmrunner is not None and not self._anmrunner.run_frame(): 162 if self._anmrunner is not None and not self._anmrunner.run_frame():
163 if self._state == LAUNCHING: 163 if self._state == LAUNCHING:
164 #TODO: check if it doesn't skip a frame 164 #TODO: check if it doesn't skip a frame
165 self.launch() 165 self.launch()
166 elif self._state == CANCELLED: 166 elif self._state == CANCELLED:
246 self.x += self.dx 246 self.x += self.dx
247 self.y += self.dy 247 self.y += self.dy
248 248
249 self.frame += 1 249 self.frame += 1
250 250
251 # Filter out-of-screen bullets and handle special flags
252 #TODO: flags 1024 and 2048
253 if self.flags & 448:
254 self._was_visible = False
255 elif self.is_visible(self._game.width, self._game.height):
256 self._was_visible = True
257 elif self._was_visible:
258 # Filter out-of-screen bullets
259 self._removed = True
260