hedgewars/uGearsHedgehog.pas
changeset 11512 0e305b7153e6
parent 11510 64c1ea171204
child 11513 d44d2ab7ce4a
--- a/hedgewars/uGearsHedgehog.pas	Fri Jan 15 05:50:18 2016 +0100
+++ b/hedgewars/uGearsHedgehog.pas	Fri Jan 15 13:18:40 2016 +0100
@@ -873,6 +873,55 @@
 
 
 ////////////////////////////////////////////////////////////////////////////////
+procedure moveHedgehogOutOfWall(Gear: PGear);
+var
+    hx: hwFloat;
+    colly, collxl, collxr: boolean;
+begin
+colly:= (TestCollisionYwithGear(Gear, 1) <> 0);
+
+while colly do
+    begin
+    // don't use TestCollisionXwithXYShift, because we don't gears
+    // save real x-position
+    hx:= Gear^.X;
+
+    Gear^.X:= hx + _1;
+    collxl:= (TestCollisionX(Gear, -1) <> 0);
+
+    Gear^.X:= hx - _1;
+    collxr:= (TestCollisionX(Gear,  1) <> 0);
+
+    // restore original value
+    Gear^.X:= hx;
+
+    // do nothing if trapped between 2 walls
+    if collxl = collxr then
+        break;
+
+    // if we in a wall - fix that
+    if collxl then
+        begin
+        // check for obstacle
+        if (TestCollisionX(Gear,  1) <> 0) then
+            break;
+        Gear^.X:= Gear^.X + _1;
+        end
+    else
+        begin
+        // check for obstacle
+        if (TestCollisionX(Gear, -1) <> 0) then
+            break;
+        Gear^.X:= Gear^.X - _1;
+        end;
+
+        colly:= (TestCollisionYwithGear(Gear, 1) <> 0);
+        end;
+
+
+end;
+
+////////////////////////////////////////////////////////////////////////////////
 procedure doStepHedgehogMoving(Gear: PGear);
 var isFalling, isUnderwater: boolean;
     land: Word;
@@ -893,7 +942,15 @@
 if Gear^.dY.QWordValue > 8160437862 then
     Gear^.dY.QWordValue:= 8160437862;
 
-isFalling:= (Gear^.dY.isNegative) or (TestCollisionYKick(Gear, 1) = 0);
+isFalling:= (Gear^.dY.isNegative);
+if (not isFalling) then
+    begin
+    // make sure we're not just stuck in wall
+    moveHedgehogOutOfWall(Gear);
+    // test for floor/obstacle below
+    isFalling:= (TestCollisionYKick(Gear, 1) = 0);
+    end;
+
 if isFalling then
     begin
     land:= TestCollisionYKick(Gear, -1);