hedgewars/uCollisions.pas
changeset 511 2b5b9e00419d
parent 505 fcba7d7aea0d
child 513 69e06d710d46
equal deleted inserted replaced
510:4e994e1b7abb 511:2b5b9e00419d
    36 function TestCollisionY(Gear: PGear; Dir: LongInt): boolean;
    36 function TestCollisionY(Gear: PGear; Dir: LongInt): boolean;
    37 function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean;
    37 function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean;
    38 function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean;
    38 function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean;
    39 
    39 
    40 implementation
    40 implementation
    41 uses uMisc, uConsts, uLand, uLandGraphics;
    41 uses uMisc, uConsts, uLand, uLandGraphics, uConsole;
    42 
    42 
    43 type TCollisionEntry = record
    43 type TCollisionEntry = record
    44                        X, Y, Radius: LongInt;
    44                        X, Y, Radius: LongInt;
    45                        cGear: PGear;
    45                        cGear: PGear;
    46                        end;
    46                        end;
    50     cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry;
    50     cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry;
    51     ga: TGearArray;
    51     ga: TGearArray;
    52 
    52 
    53 procedure AddGearCI(Gear: PGear);
    53 procedure AddGearCI(Gear: PGear);
    54 begin
    54 begin
    55 if Gear^.CollIndex < High(Longword) then exit;
    55 if Gear^.CollisionIndex >= 0 then exit;
    56 TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true);
    56 TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true);
    57 with cinfos[Count] do
    57 with cinfos[Count] do
    58      begin
    58      begin
    59      X:= hwRound(Gear^.X);
    59      X:= hwRound(Gear^.X);
    60      Y:= hwRound(Gear^.Y);
    60      Y:= hwRound(Gear^.Y);
    61      Radius:= Gear^.Radius;
    61      Radius:= Gear^.Radius;
    62      ChangeRoundInLand(X, Y, Radius - 1, +1);
    62      ChangeRoundInLand(X, Y, Radius - 1, true);
    63      cGear:= Gear
    63      cGear:= Gear
    64      end;
    64      end;
    65 Gear^.CollIndex:= Count;
    65 Gear^.CollisionIndex:= Count;
    66 inc(Count)
    66 inc(Count)
    67 end;
    67 end;
    68 
    68 
    69 procedure DeleteCI(Gear: PGear);
    69 procedure DeleteCI(Gear: PGear);
    70 begin
    70 begin
    71 if Gear^.CollIndex < Count then
    71 if Gear^.CollisionIndex >= 0 then
    72    begin
    72    begin
    73    with cinfos[Gear^.CollIndex] do
    73    with cinfos[Gear^.CollisionIndex] do
    74         ChangeRoundInLand(X, Y, Radius - 1, -1);
    74         ChangeRoundInLand(X, Y, Radius - 1, false);
    75    cinfos[Gear^.CollIndex]:= cinfos[Pred(Count)];
    75    cinfos[Gear^.CollisionIndex]:= cinfos[Pred(Count)];
    76    cinfos[Gear^.CollIndex].cGear^.CollIndex:= Gear^.CollIndex;
    76    cinfos[Gear^.CollisionIndex].cGear^.CollisionIndex:= Gear^.CollisionIndex;
    77    Gear^.CollIndex:= High(Longword);
    77    Gear^.CollisionIndex:= -1;
    78    dec(Count)
    78    dec(Count)
    79    end;
    79    end;
    80 end;
    80 end;
    81 
    81 
    82 function CheckGearsCollision(Gear: PGear): PGearArray;
    82 function CheckGearsCollision(Gear: PGear): PGearArray;
    91 my:= hwRound(Gear^.Y);
    91 my:= hwRound(Gear^.Y);
    92 
    92 
    93 for i:= 0 to Pred(Count) do
    93 for i:= 0 to Pred(Count) do
    94    with cinfos[i] do
    94    with cinfos[i] do
    95       if (Gear <> cGear) and
    95       if (Gear <> cGear) and
    96          (sqrt(sqr(mx - x) + sqr(my - y)) <= Radius + Gear^.Radius) then
    96          (sqr(mx - x) + sqr(my - y) <= sqr(Radius + Gear^.Radius)) then
    97              begin
    97              begin
    98              ga.ar[ga.Count]:= cinfos[i].cGear;
    98              ga.ar[ga.Count]:= cinfos[i].cGear;
    99              inc(ga.Count)
    99              inc(ga.Count)
   100              end;
   100              end;
   101 CheckGearsCollision:= Result
   101 CheckGearsCollision:= Result