Mercurial > touhou
comparison pytouhou/game/game.py @ 202:d348892ef012
Handle enemy collisions and damages in a way closer to the original game.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 31 Oct 2011 19:01:09 +0100 |
parents | 13918723d1bc |
children | df8b2ab54639 |
comparison
equal
deleted
inserted
replaced
201:220c122f428c | 202:d348892ef012 |
---|---|
143 | 143 |
144 def update_enemies(self): | 144 def update_enemies(self): |
145 for enemy in self.enemies: | 145 for enemy in self.enemies: |
146 enemy.update() | 146 enemy.update() |
147 | 147 |
148 # Check for collisions | |
149 for enemy in self.enemies: | |
150 ex, ey = enemy.x, enemy.y | |
151 ehalf_size_x, ehalf_size_y = enemy.hitbox_half_size | |
152 ex1, ex2 = ex - ehalf_size_x, ex + ehalf_size_x | |
153 ey1, ey2 = ey - ehalf_size_y, ey + ehalf_size_y | |
154 | |
155 for bullet in self.players_bullets: | |
156 half_size = bullet.hitbox_half_size | |
157 bx, by = bullet.x, bullet.y | |
158 bx1, bx2 = bx - half_size, bx + half_size | |
159 by1, by2 = by - half_size, by + half_size | |
160 | |
161 if not (bx2 < ex1 or bx1 > ex2 | |
162 or by2 < ey1 or by1 > ey2): | |
163 bullet.collide() | |
164 enemy.on_attack(bullet) | |
165 #TODO: place that at the right place. | |
166 #player.state.score += 90 # found experimentally | |
167 | |
168 | 148 |
169 def update_players(self, keystate): | 149 def update_players(self, keystate): |
170 for player in self.players: | 150 for player in self.players: |
171 player.update(keystate) #TODO: differentiate keystates (multiplayer mode) | 151 player.update(keystate) #TODO: differentiate keystates (multiplayer mode) |
172 if player.state.x < 8.: | 152 if player.state.x < 8.: |
179 player.state.y = 448.-16 | 159 player.state.y = 448.-16 |
180 | 160 |
181 for bullet in self.players_bullets: | 161 for bullet in self.players_bullets: |
182 bullet.update() | 162 bullet.update() |
183 | 163 |
184 # Check for collisions | |
185 for player in self.players: | |
186 if not player.state.touchable: | |
187 continue | |
188 | |
189 px, py = player.x, player.y | |
190 phalf_size = player.hitbox_half_size | |
191 px1, px2 = px - phalf_size, px + phalf_size | |
192 py1, py2 = py - phalf_size, py + phalf_size | |
193 | |
194 ghalf_size = player.graze_hitbox_half_size | |
195 gx1, gx2 = px - ghalf_size, px + ghalf_size | |
196 gy1, gy2 = py - ghalf_size, py + ghalf_size | |
197 | |
198 #TODO: Should that be done here or in update_enemies? | |
199 for enemy in self.enemies: | |
200 half_size_x, half_size_y = enemy.hitbox_half_size | |
201 bx, by = enemy.x, enemy.y | |
202 bx1, bx2 = bx - half_size_x, bx + half_size_x | |
203 by1, by2 = by - half_size_y, by + half_size_y | |
204 | |
205 #TODO: box-box or point-in-box? | |
206 if enemy.touchable and not (bx2 < px1 or bx1 > px2 | |
207 or by2 < py1 or by1 > py2): | |
208 enemy.on_collide() | |
209 if player.state.invulnerable_time == 0: | |
210 player.collide() | |
211 | |
212 | 164 |
213 def update_effects(self): | 165 def update_effects(self): |
214 for effect in self.effects: | 166 for effect in self.effects: |
215 effect.update() | 167 effect.update() |
216 | 168 |