Changeset 187:92ab784ecf5a


Ignore:
Timestamp:
04/06/11 18:50:00 (12 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Message:

Repel fx from longstanding collisions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/sprites/player.py

    r177 r187  
    3434        self.set_image()
    3535        self.set_pos((0, 0))
     36        self._collisions_seen = 0
     37        self._last_collide = []
    3638        self._layer = Layers.PLAYER
    3739
     
    8789            self.running = True
    8890        self.set_image()
     91        if self._collisions_seen > 2 * len(self._last_collide):
     92            print 'Trying to move'
     93            # Can we find a position "nearby" that reduces the collision
     94            # surface
     95            best_move = (0, 0)
     96            clip_area = 0
     97            for rect in self._last_collide:
     98                clip = rect.collide_rect.clip(self.collide_rect)
     99                clip_area += clip.width * clip.height
     100            min_area = clip_area
     101            for attempt in [(0, 2), (2, 0), (-2, 0), (2, 2), (-2, 2)]:
     102                clip_area = 0
     103                for rect in self._last_collide:
     104                    cand_rect = self.collide_rect.move(attempt)
     105                    clip = rect.collide_rect.clip(cand_rect)
     106                    clip_area += clip.width * clip.height
     107                if clip_area < min_area:
     108                    min_area = clip_area
     109                    best_move = attempt
     110            print best_move, min_area, self.velocity
     111            self.collide_rect.move_ip(best_move)
     112            self.rect.move_ip(best_move)
     113            self.init_pos()
     114            self._last_collide = []
     115            self._collisions_seen = 0
    89116
    90117    def set_facing(self, new_facing):
     
    92119
    93120    def collided(self, other):
    94         pass
     121        if other not in self._last_collide:
     122            self._last_collide.append(other)
     123            self._collide_pos = self.collide_rect.midbottom
     124            self._collisions_seen = 0
     125        else:
     126            self._collisions_seen += 1
     127
    95128
    96129    def set_pos(self, pos):
Note: See TracChangeset for help on using the changeset viewer.