changeset 416:3db2fc263d11

Kill sprites that leave the level area.
author Simon Cross <hodgestar@gmail.com>
date Sat, 09 Apr 2011 17:42:20 +0200
parents cfbf1bf9a54c
children e289a111c743
files skaapsteker/levelscene.py skaapsteker/physics.py
diffstat 2 files changed, 13 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/levelscene.py	Sat Apr 09 17:40:21 2011 +0200
+++ b/skaapsteker/levelscene.py	Sat Apr 09 17:42:20 2011 +0200
@@ -34,7 +34,7 @@
 
         self._level_surface = self._level.get_surface()
         self._clip_rect = None
-        self._world = physics.World()
+        self._world = physics.World(self._level_surface.get_rect())
         self._paused = False
 
         # Helper images for hud
--- a/skaapsteker/physics.py	Sat Apr 09 17:40:21 2011 +0200
+++ b/skaapsteker/physics.py	Sat Apr 09 17:42:20 2011 +0200
@@ -168,12 +168,16 @@
         self.rect = old_rect
         return not bool(new_collisions - old_collisions)
 
+    def fix_bounds(self):
+        print "Killing", self, self.rect, self._float_pos
+        self.kill()
+
 
 class World(object):
 
     GRAVITY = 0.0, 9.8 * 80.0 # pixels / s^2
 
-    def __init__(self):
+    def __init__(self, bounds):
         self._all = pygame.sprite.LayeredUpdates()
         self._mobiles = pygame.sprite.Group()
         self._gravitators = pygame.sprite.Group()
@@ -182,6 +186,7 @@
         self._actors = pygame.sprite.Group()
         self._collision_groups = { None: pygame.sprite.Group() }
         self._last_time = None
+        self._bounds = bounds
 
     def freeze(self):
         self._last_time = None
@@ -264,6 +269,12 @@
         for sprite in self._mobiles:
             sprite.apply_friction()
 
+        # kill sprites outside the world
+        inbound = self._bounds.colliderect
+        for sprite in self._mobiles:
+            if not inbound(sprite):
+                sprite.fix_bounds()
+
         # position update and collision check (do last)
         for sprite in self._mobiles:
             sprite.deltap(dt)