Changeset 172:bf144d817113


Ignore:
Timestamp:
Apr 5, 2011, 11:35:20 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/physics.py

    r161 r172  
    3131    collides_with = set() # nothing collides with this
    3232
    33     # set to True to have .update() called once per tick
     33    # set to True to have .update() called once per tick (and have .collision_group set)
    3434    wants_updates = False
    3535
     
    4747        self.image = pygame.Surface((10, 10))
    4848        self.image.fill((0, 0, 200))
     49        self.collision_group = None
    4950        self._mask_cache = {} # image id -> collision bit mask
    5051
     
    133134    def update(self):
    134135        pass # only called in wants_update = True
     136
     137    def check_collide_rect(self, new_collide_rect, new_rect, new_image):
     138        if self.collision_group is None:
     139            return True
     140
     141        # TODO: decide whether to throw out checking of existing
     142        # collisions. Doesn't seem needed at the moment and takes
     143        # time.
     144        old_image = self.image
     145        old_rect = self.rect
     146        #rect_collides = self.collide_rect.colliderect
     147        old_collisions = set()
     148        #for other in self.collision_group:
     149        #    if rect_collides(other.collide_rect) \
     150        #       and self.check_collides(other):
     151        #        old_collisions.add(other)
     152
     153        self.image = new_image
     154        self.rect = new_rect
     155        new_rect_collides = new_collide_rect.colliderect
     156        new_collisions = set()
     157        for other in self.collision_group:
     158            if new_rect_collides(other.collide_rect) \
     159               and self.check_collides(other):
     160                new_collisions.add(other)
     161
     162        self.image = old_image
     163        self.rect = old_rect
     164        return not bool(new_collisions - old_collisions)
    135165
    136166
     
    166196            self._add_collision_group(layer)
    167197            self._collision_groups[layer].add(sprite)
     198        if sprite.wants_updates:
     199            self._updaters.add(sprite)
     200            sprite.collision_group = self._collision_groups[sprite.collision_layer]
    168201
    169202    def _add_collision_group(self, layer):
  • skaapsteker/sprites/player.py

    r167 r172  
    2323        self._image_dict = {}
    2424        self._animation_frame = 0.0
    25         self._recent_collisions = []
    2625        self._last_time = time.time()
    2726        # State flags and such
     
    4645        else:
    4746            cur_pos = (0, 0)
     47
     48        # TODO: can save a lot of calculation here by caching collision rects
    4849        cand_image = images[int(self._animation_frame)]
    49         cand_collide_rect = cand_image.get_bounding_rect(1)
     50        cand_collide_rect = cand_image.get_bounding_rect(1).inflate(-2,-2)
     51        cand_rect = cand_image.get_rect()
     52        cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom
     53        cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1]
    5054        cand_collide_rect.midbottom = cur_pos
    51         if cand_collide_rect.collidelist(self._recent_collisions) != -1 \
    52                 and self.collide_rect.collidelist(self._recent_collisions) == -1:
    53             # We introduce a new collision, so don't update the image
     55        if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image):
    5456            return
    55         self.image = images[int(self._animation_frame)]
    56         self.collide_rect = self.image.get_bounding_rect(1)
    57         self.rect = self.image.get_rect()
    58         self.rect_offset = self.rect.centerx - self.collide_rect.centerx, self.rect.bottom - self.collide_rect.bottom
    59         self.collide_rect.midbottom = cur_pos
    60         self.rect.midbottom = cur_pos[0] + self.rect_offset[0], cur_pos[1] + self.rect_offset[1]
     57
     58        self.image = cand_image
     59        self.collide_rect = cand_collide_rect
     60        self.rect = cand_rect
     61        self.rect_offset = cand_rect_offset
    6162        self.init_pos()
    6263
     
    9091
    9192    def collided(self, other):
    92         self._recent_collisions.append(other.collide_rect)
    93         while len(self._recent_collisions) > 10:
    94             self._recent_collisions.pop(0)
     93        pass
    9594
    9695    def set_pos(self, pos):
Note: See TracChangeset for help on using the changeset viewer.