changeset 271:56a529a69e97

Only backout / move-off "solid" collisions
author Neil Muller <drnlmuller@gmail.com>
date Fri, 08 Apr 2011 17:26:06 +0200
parents e47efa33903b
children 630ebb87b38a
files skaapsteker/physics.py skaapsteker/sprites/base.py skaapsteker/sprites/player.py
diffstat 3 files changed, 27 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/physics.py	Fri Apr 08 17:13:25 2011 +0200
+++ b/skaapsteker/physics.py	Fri Apr 08 17:26:06 2011 +0200
@@ -206,24 +206,29 @@
         self._collision_groups[layer] = pygame.sprite.Group()
 
     def _backout_collisions(self, sprite, others, dt):
-        frac, normal, idx = -1.0, None, None
+        frac, normal, idx = 0.0, None, None
         v_x, v_y = sprite.velocity
         abs_v_x, abs_v_y = abs(v_x), abs(v_y)
 
-        for i, other in enumerate(others):
-            clip = sprite.collide_rect.clip(other.collide_rect)
-            # TODO: avoid continual "if abs_v_? > EPSILON"
-            frac_x = clip.width / abs_v_x if abs_v_x > EPSILON else dt
-            frac_y = clip.height / abs_v_y if abs_v_y > EPSILON else dt
-            if frac_x > frac_y:
-                if frac_y > frac:
-                    frac, normal, idx = frac_y, (0, 1), i
-            else:
-                if frac_x > frac:
-                    frac, normal, idx = frac_x, (1, 0), i
+        # We only backout of "solide" collisions
+        if sprite.block:
+            for i, other in enumerate(others):
+                if other.block or other.floor:
+                    clip = sprite.collide_rect.clip(other.collide_rect)
+                    # TODO: avoid continual "if abs_v_? > EPSILON"
+                    frac_x = clip.width / abs_v_x if abs_v_x > EPSILON else dt
+                    frac_y = clip.height / abs_v_y if abs_v_y > EPSILON else dt
+                    if frac_x > frac_y:
+                        if frac_y > frac:
+                            frac, normal, idx = frac_y, (0, 1), i
+                    else:
+                        if frac_x > frac:
+                            frac, normal, idx = frac_x, (1, 0), i
 
-        sprite.deltap(max(-1.1 * frac, -dt))
-        sprite.bounce(others[idx], normal)
+                if idx is not None:
+                    # We can see no solide collisions now
+                    sprite.deltap(max(-1.1 * frac, -dt))
+                    sprite.bounce(others[idx], normal)
 
         for other in others:
             sprite.collided(other)
--- a/skaapsteker/sprites/base.py	Fri Apr 08 17:13:25 2011 +0200
+++ b/skaapsteker/sprites/base.py	Fri Apr 08 17:26:06 2011 +0200
@@ -195,6 +195,7 @@
     collides_with = set([PC_LAYER])
 
     debug_color = (240, 240, 240)
+    bounce_factor = (0, 0)    #  NPC's don't bounce by default
 
     block = False
 
@@ -284,6 +285,8 @@
     collides_with = set([PC_LAYER])
     wants_updates = True
 
+    blocks = False
+
     image_file = 'torii.png'
 
     debug_color = (120, 240, 120)
--- a/skaapsteker/sprites/player.py	Fri Apr 08 17:13:25 2011 +0200
+++ b/skaapsteker/sprites/player.py	Fri Apr 08 17:26:06 2011 +0200
@@ -16,6 +16,8 @@
     collides_with = set([MONSTER_LAYER])
     wants_updates = True
 
+    block = True
+
     _max_sprint_time = 2
 
     def __init__(self, the_world, soundsystem):
@@ -155,12 +157,13 @@
         if self.attacking and hasattr(other, 'damage'):
             # FIXME: Check if we're facing the right way
             other.damage(5)
-        if other not in self._last_collide:
+        if other not in self._last_collide and (other.floor or other.block):
             self._last_collide.append(other)
             self._collide_pos = self.collide_rect.midbottom
             self._collisions_seen = 0
-        else:
+        elif other in self._last_collide:
             self._collisions_seen += 1
+        print other, self._collisions_seen, self._last_collide
         if hasattr(other, 'collided_player'):
             other.collided_player(self)
             print 'Health', self.the_world.fox.cur_health