hedgewars/uGearsHandlersRope.pas
changeset 7595 bbac3c0f0c68
parent 7594 5f03595335e6
child 7596 2c12b8cbd002
equal deleted inserted replaced
7594:5f03595335e6 7595:bbac3c0f0c68
   102 end;
   102 end;
   103 
   103 
   104 procedure doStepRopeWork(Gear: PGear);
   104 procedure doStepRopeWork(Gear: PGear);
   105 var 
   105 var 
   106     HHGear: PGear;
   106     HHGear: PGear;
   107     len, tx, ty, nx, ny, ropeDx, ropeDy, mdX, mdY: hwFloat;
   107     len, tx, ty, nx, ny, ropeDx, ropeDy, mdX, mdY, t: hwFloat;
   108     lx, ly, cd: LongInt;
   108     lx, ly, cd, i: LongInt;
   109     haveCollision,
   109     haveCollision,
   110     haveDivided: boolean;
   110     haveDivided: boolean;
   111 
   111 
   112 begin
   112 begin
   113     if GameTicks mod 8 <> 0 then exit;
   113     if GameTicks mod 8 <> 0 then exit;
   114 
   114 
   115     HHGear := Gear^.Hedgehog^.Gear;
   115     HHGear := Gear^.Hedgehog^.Gear;
       
   116     haveCollision:= false;
       
   117     if (Gear^.Message and gmLeft  <> 0) and (not TestCollisionXwithGear(HHGear, -1)) then
       
   118         HHGear^.dX := HHGear^.dX - _0_0128
       
   119     else haveCollision:= true;
       
   120 
       
   121     if (Gear^.Message and gmRight <> 0) and (not TestCollisionXwithGear(HHGear,  1)) then
       
   122         HHGear^.dX := HHGear^.dX + _0_0128
       
   123     else haveCollision:= true;
       
   124 
   116 
   125 
   117     if ((HHGear^.State and gstHHDriven) = 0)
   126     if ((HHGear^.State and gstHHDriven) = 0)
   118        or (CheckGearDrowning(HHGear)) or (Gear^.PortalCounter <> 0) then
   127        or (CheckGearDrowning(HHGear)) or (Gear^.PortalCounter <> 0) then
   119         begin
   128         begin
   120         RopeDeleteMe(Gear, HHGear);
   129         RopeDeleteMe(Gear, HHGear);
   121         exit
   130         exit
   122         end;
   131         end;
   123 
       
   124     if (Gear^.Message and gmLeft  <> 0) and (not TestCollisionXwithGear(HHGear, -1)) then
       
   125         HHGear^.dX := HHGear^.dX - _0_0128;
       
   126 
       
   127     if (Gear^.Message and gmRight <> 0) and (not TestCollisionXwithGear(HHGear,  1)) then
       
   128         HHGear^.dX := HHGear^.dX + _0_0128;
       
   129 
   132 
   130     // vector between hedgehog and rope attaching point
   133     // vector between hedgehog and rope attaching point
   131     ropeDx := HHGear^.X - Gear^.X;
   134     ropeDx := HHGear^.X - Gear^.X;
   132     ropeDy := HHGear^.Y - Gear^.Y;
   135     ropeDy := HHGear^.Y - Gear^.Y;
   133 
   136 
   147 
   150 
   148         if (GameFlags and gfMoreWind) <> 0 then
   151         if (GameFlags and gfMoreWind) <> 0 then
   149             // apply wind if there's no obstacle
   152             // apply wind if there's no obstacle
   150             if not TestCollisionXwithGear(HHGear, hwSign(cWindSpeed)) then
   153             if not TestCollisionXwithGear(HHGear, hwSign(cWindSpeed)) then
   151                 HHGear^.dX := HHGear^.dX + cWindSpeed * 64 / HHGear^.Density;
   154                 HHGear^.dX := HHGear^.dX + cWindSpeed * 64 / HHGear^.Density;
       
   155         end
       
   156     else haveCollision:= true;
       
   157 
       
   158     if ((Gear^.Message and gmDown) <> 0) and (Gear^.Elasticity < Gear^.Friction) then
       
   159         if not (TestCollisionXwithGear(HHGear, hwSign(ropeDx))
       
   160         or (TestCollisionYwithGear(HHGear, hwSign(ropeDy)) <> 0)) then
       
   161             Gear^.Elasticity := Gear^.Elasticity + _2_4
       
   162     else haveCollision:= true;
       
   163 
       
   164     if ((Gear^.Message and gmUp) <> 0) and (Gear^.Elasticity > _30) then
       
   165         if not (TestCollisionXwithGear(HHGear, -hwSign(ropeDx))
       
   166         or (TestCollisionYwithGear(HHGear, -hwSign(ropeDy)) <> 0)) then
       
   167             Gear^.Elasticity := Gear^.Elasticity - _2_4
       
   168     else haveCollision:= true;
       
   169 
       
   170     if haveCollision then
       
   171         begin
       
   172         if TestCollisionXwithGear(HHGear, hwSign(HHGear^.dX)) and not TestCollisionXwithGear(HHGear, hwSign(HHGear^.dX)) then
       
   173             HHGear^.dX.isNegative:= not HHGear^.dX.isNegative;
       
   174         if (TestCollisionYwithGear(HHGear, hwSign(HHGear^.dY)) <> 0) and (TestCollisionYwithGear(HHGear, -hwSign(HHGear^.dY)) = 0) then
       
   175             HHGear^.dY.isNegative:= not HHGear^.dX.isNegative;
   152         end;
   176         end;
   153 
   177 
   154     mdX := ropeDx + HHGear^.dX;
   178     mdX := ropeDx + HHGear^.dX;
   155     mdY := ropeDy + HHGear^.dY;
   179     mdY := ropeDy + HHGear^.dY;
   156     len := _1 / Distance(mdX, mdY);
   180     len := _1 / Distance(mdX, mdY);
   163     Gear^.dY := mdY;
   187     Gear^.dY := mdY;
   164 
   188 
   165     /////
   189     /////
   166     tx := HHGear^.X;
   190     tx := HHGear^.X;
   167     ty := HHGear^.Y;
   191     ty := HHGear^.Y;
   168 
       
   169     if ((Gear^.Message and gmDown) <> 0) and (Gear^.Elasticity < Gear^.Friction) then
       
   170         if not (TestCollisionXwithGear(HHGear, hwSign(ropeDx))
       
   171         or (TestCollisionYwithGear(HHGear, hwSign(ropeDy)) <> 0)) then
       
   172             Gear^.Elasticity := Gear^.Elasticity + _2_4;
       
   173 
       
   174     if ((Gear^.Message and gmUp) <> 0) and (Gear^.Elasticity > _30) then
       
   175         if not (TestCollisionXwithGear(HHGear, -hwSign(ropeDx))
       
   176         or (TestCollisionYwithGear(HHGear, -hwSign(ropeDy)) <> 0)) then
       
   177             Gear^.Elasticity := Gear^.Elasticity - _2_4;
       
   178 
   192 
   179     HHGear^.X := Gear^.X + mdX * Gear^.Elasticity;
   193     HHGear^.X := Gear^.X + mdX * Gear^.Elasticity;
   180     HHGear^.Y := Gear^.Y + mdY * Gear^.Elasticity;
   194     HHGear^.Y := Gear^.Y + mdY * Gear^.Elasticity;
   181 
   195 
   182     HHGear^.dX := HHGear^.X - tx;
   196     HHGear^.dX := HHGear^.X - tx;