Mercurial > skaapsteker
changeset 187:92ab784ecf5a
Repel fx from longstanding collisions
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 06 Apr 2011 20:50:00 +0200 |
parents | 72e92893ccb8 |
children | 30a5f7cf670a |
files | skaapsteker/sprites/player.py |
diffstat | 1 files changed, 34 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/skaapsteker/sprites/player.py Wed Apr 06 20:49:47 2011 +0200 +++ b/skaapsteker/sprites/player.py Wed Apr 06 20:50:00 2011 +0200 @@ -33,6 +33,8 @@ self.set_facing('left') self.set_image() self.set_pos((0, 0)) + self._collisions_seen = 0 + self._last_collide = [] self._layer = Layers.PLAYER def set_image(self): @@ -86,12 +88,43 @@ self.velocity = (0, v_y) # Standard platformer physics self.running = True self.set_image() + if self._collisions_seen > 2 * len(self._last_collide): + print 'Trying to move' + # Can we find a position "nearby" that reduces the collision + # surface + best_move = (0, 0) + clip_area = 0 + for rect in self._last_collide: + clip = rect.collide_rect.clip(self.collide_rect) + clip_area += clip.width * clip.height + min_area = clip_area + for attempt in [(0, 2), (2, 0), (-2, 0), (2, 2), (-2, 2)]: + clip_area = 0 + for rect in self._last_collide: + cand_rect = self.collide_rect.move(attempt) + clip = rect.collide_rect.clip(cand_rect) + clip_area += clip.width * clip.height + if clip_area < min_area: + min_area = clip_area + best_move = attempt + print best_move, min_area, self.velocity + self.collide_rect.move_ip(best_move) + self.rect.move_ip(best_move) + self.init_pos() + self._last_collide = [] + self._collisions_seen = 0 def set_facing(self, new_facing): self.facing = new_facing def collided(self, other): - pass + if other not in self._last_collide: + self._last_collide.append(other) + self._collide_pos = self.collide_rect.midbottom + self._collisions_seen = 0 + else: + self._collisions_seen += 1 + def set_pos(self, pos): self.starting_tile_pos = pos