hedgewars/uGearsUtils.pas
changeset 13894 9954f9b800f5
parent 13893 7163f5ec3ce2
child 13899 f9d135768d17
equal deleted inserted replaced
13893:7163f5ec3ce2 13894:9954f9b800f5
  1055 end;
  1055 end;
  1056 
  1056 
  1057 function CheckGearNearImpl(Kind: TGearType; X, Y: hwFloat; rX, rY: LongInt, exclude: PGear): PGear;
  1057 function CheckGearNearImpl(Kind: TGearType; X, Y: hwFloat; rX, rY: LongInt, exclude: PGear): PGear;
  1058 begin
  1058 begin
  1059     var t: PGear;
  1059     var t: PGear;
  1060     width, halfWidth, dX, dY: hwFloat;
  1060     width, dX, dY: hwFloat;
       
  1061     bound: LongInt;
  1061     isHit: Boolean;
  1062     isHit: Boolean;
  1062 begin
  1063 begin
  1063     t:= GearsList;
  1064     t:= GearsList;
       
  1065     bound:= _1_5 * int2hwFloat(max(rX, rY));
  1064     rX:= sqr(rX);
  1066     rX:= sqr(rX);
  1065     rY:= sqr(rY);
  1067     rY:= sqr(rY);
  1066     width:= int2hwFloat(RightX - LeftX);
  1068     width:= int2hwFloat(RightX - LeftX);
  1067     halfWidth:= width / _2;
       
  1068 
  1069 
  1069     while t <> nil do
  1070     while t <> nil do
  1070     begin
  1071     begin
  1071         if (t <> exclude) and (t^.Kind = Kind) then
  1072         if (t <> exclude) and (t^.Kind = Kind) then
  1072         begin
  1073         begin
  1073             dX := X - t^.X;
  1074             dX := X - t^.X;
  1074             dY := Y - t^.Y;
  1075             dY := Y - t^.Y;
  1075             isHit := not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1);
  1076             isHit := (hwAbs(dX) + hwAbs(dY) < bound)
       
  1077                 and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1));
  1076 
  1078 
  1077             if (not isHit) and (WorldEdge = weWrap) then
  1079             if (not isHit) and (WorldEdge = weWrap) then
  1078             begin
  1080             begin
  1079                 if (dX > halfWidth) and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
  1081                 if (hwAbs(dX - width) + hwAbs(dY) < bound)
       
  1082                     and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
  1080                     isHit := true
  1083                     isHit := true
  1081                 else if (dX < -halfWidth) and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
  1084                 else if (hwAbs(dX + width) + hwAbs(dY) < bound)
       
  1085                     and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
  1082                     isHit := true
  1086                     isHit := true
  1083             end;
  1087             end;
  1084 
  1088 
  1085             if isHit then
  1089             if isHit then
  1086             begin
  1090             begin