hedgewars/HHHandlers.inc
changeset 1 30f2d1037d5d
child 4 bcbd7adb4e4b
equal deleted inserted replaced
0:475c0f2f9d17 1:30f2d1037d5d
       
     1 (*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  *)
       
    33 
       
    34 procedure doStepHedgehog(Gear: PGear); forward;
       
    35 ////////////////////////////////////////////////////////////////////////////////
       
    36 procedure doStepHedgehogDriven(Gear: PGear);
       
    37 const StepTicks: LongWord = 0;
       
    38 begin
       
    39 if isinMultiShoot and (Gear.Damage = 0) then exit;
       
    40 AllInactive:= false;
       
    41 if (TurnTimeLeft = 0) or (Gear.Damage > 0) then
       
    42    begin
       
    43    if ((Gear.State and (gstMoving or gstFalling)) = 0)
       
    44       and (CurAmmoGear = nil) then Gear.dX:= 0.0000001 * Sign(Gear.dX);
       
    45    {$WARNINGS OFF}Gear.State:= Gear.State and not gstHHDriven;{$WARNINGS ON}
       
    46    exit
       
    47    end;
       
    48 
       
    49 if CurAmmoGear <> nil then
       
    50    begin
       
    51    CurAmmoGear.Message:= Gear.Message;
       
    52    exit
       
    53    end;
       
    54 
       
    55 if (Gear.Message and gm_Attack)<>0 then
       
    56    if (Gear.State and (gstAttacked or gstHHChooseTarget) = 0)and(CurAmmoGear = nil) then
       
    57       with PHedgehog(Gear.Hedgehog)^ do
       
    58 //        if ((Gear.State and gstFalling  ) = 0)or((Ammo[CurSlot, CurAmmo].Propz and ammoprop_AttackInFall) <> 0)
       
    59 //        and((Gear.State and gstHHJumping) = 0)or((Ammo[CurSlot, CurAmmo].Propz and ammoprop_AttackInJump) <> 0) then
       
    60             begin
       
    61             Gear.State:= Gear.State or gstAttacking;
       
    62             if Gear.Power = cMaxPower then ParseCommand('-attack')
       
    63                else begin
       
    64                if (Ammo[CurSlot, CurAmmo].Propz and ammoprop_Power) = 0 then Gear.Power:= cMaxPower
       
    65                   else begin
       
    66                   if Gear.Power = 0 then
       
    67                      begin
       
    68                      AttackBar:= CurrentTeam.AttackBar;
       
    69                      PlaySound(sndThrowPowerUp)
       
    70                      end;
       
    71                   inc(Gear.Power)
       
    72                   end
       
    73                end;
       
    74             end else Gear.Message:= Gear.Message and not gm_Attack;
       
    75 
       
    76 
       
    77 if (Gear.State and gstFalling) <> 0 then
       
    78    begin
       
    79    if ((Gear.Message and gm_HJump) <> 0) and ((Gear.State and gstHHJumping) <> 0) then
       
    80       if (abs(Gear.dX) < 0.0000002) and (Gear.dY < -0.02) then
       
    81          begin
       
    82          Gear.dY:= -0.25;
       
    83          Gear.dX:= Sign(Gear.dX) * 0.02
       
    84          end;
       
    85    Gear.Message:= Gear.Message and not (gm_LJump or gm_HJump);
       
    86    if TestCollisionXwithGear(Gear, Sign(Gear.dX)) then Gear.dX:= 0.0000001 * Sign(Gear.dX);
       
    87    Gear.X:= Gear.X + Gear.dX;
       
    88    Gear.dY:= Gear.dY + cGravity;
       
    89    if (Gear.dY < 0)and TestCollisionYwithGear(Gear, -1) then Gear.dY:= 0; 
       
    90    Gear.Y:= Gear.Y + Gear.dY;
       
    91    if (Gear.dY >= 0)and HHTestCollisionYwithGear(Gear, 1) then
       
    92       begin
       
    93       CheckHHDamage(Gear);
       
    94       if ((abs(Gear.dX) + abs(Gear.dY)) < 0.55)
       
    95          and ((Gear.State and gstHHJumping) <> 0) then Gear.dX:= 0.0000001 * Sign(Gear.dX);
       
    96       Gear.State:= Gear.State and not (gstFalling or gstHHJumping);
       
    97       StepTicks:= 200;
       
    98       Gear.dY:= 0
       
    99       end;
       
   100    CheckGearDrowning(Gear);
       
   101    exit
       
   102    end;
       
   103 
       
   104 if StepTicks > 0 then dec(StepTicks);
       
   105 
       
   106 if ((Gear.State and (gstMoving or gstFalling)) = 0) then
       
   107    if (Gear.Message and gm_Up    )<>0 then if Gear.Angle > 0         then dec(Gear.Angle)
       
   108       else else
       
   109    if (Gear.Message and gm_Down  )<>0 then if Gear.Angle < cMaxAngle then inc(Gear.Angle);
       
   110 
       
   111 if ((Gear.State and (gstAttacking or gstMoving or gstFalling)) = 0)and(StepTicks = 0) then
       
   112    begin
       
   113    if ((Gear.Message and gm_LJump )<>0) then
       
   114       begin
       
   115       Gear.Message:= 0;
       
   116       if not HHTestCollisionYwithGear(Gear, -1) then
       
   117          if not TestCollisionXwithXYShift(Gear, 0, -2, Sign(Gear.dX)) then Gear.Y:= Gear.Y - 2 else
       
   118          if not TestCollisionXwithXYShift(Gear, 0, -1, Sign(Gear.dX)) then Gear.Y:= Gear.Y - 1;
       
   119       if not (TestCollisionXwithGear(Gear, Sign(Gear.dX))
       
   120          or   HHTestCollisionYwithGear(Gear, -1)) then
       
   121          begin
       
   122          Gear.dY:= -0.15;
       
   123          Gear.dX:= Sign(Gear.dX) * 0.15;
       
   124          Gear.State:= Gear.State or gstFalling or gstHHJumping;
       
   125          exit
       
   126          end;
       
   127       end;
       
   128    if ((Gear.Message and gm_HJump )<>0) then
       
   129       begin
       
   130       Gear.Message:= 0;
       
   131       if not HHTestCollisionYwithGear(Gear, -1) then
       
   132          begin
       
   133          Gear.dY:= -0.20;
       
   134          Gear.dX:= 0.0000001 * Sign(Gear.dX);
       
   135          Gear.X:= Gear.X - Sign(Gear.dX)*0.00008; // компенсация сдвига %)
       
   136          Gear.State:= Gear.State or gstFalling or gstHHJumping;
       
   137          exit
       
   138          end;
       
   139       end;
       
   140    if (Gear.Message and gm_Left  )<>0 then Gear.dX:= -1.0 else
       
   141    if (Gear.Message and gm_Right )<>0 then Gear.dX:=  1.0 else exit;
       
   142    PHedgehog(Gear.Hedgehog).visStepPos:= (PHedgehog(Gear.Hedgehog).visStepPos + 1) and 7;
       
   143    StepTicks:= 40;
       
   144    if TestCollisionXwithGear(Gear, Sign(Gear.dX)) then
       
   145       begin
       
   146       if not (TestCollisionXwithXYShift(Gear, 0, -6, Sign(Gear.dX))
       
   147          or HHTestCollisionYwithGear(Gear, -1)) then Gear.Y:= Gear.Y - 1;
       
   148       if not (TestCollisionXwithXYShift(Gear, 0, -5, Sign(Gear.dX))
       
   149          or HHTestCollisionYwithGear(Gear, -1)) then Gear.Y:= Gear.Y - 1;
       
   150       if not (TestCollisionXwithXYShift(Gear, 0, -4, Sign(Gear.dX))
       
   151          or HHTestCollisionYwithGear(Gear, -1)) then Gear.Y:= Gear.Y - 1;
       
   152       if not (TestCollisionXwithXYShift(Gear, 0, -3, Sign(Gear.dX))
       
   153          or HHTestCollisionYwithGear(Gear, -1)) then Gear.Y:= Gear.Y - 1;
       
   154       if not (TestCollisionXwithXYShift(Gear, 0, -2, Sign(Gear.dX))
       
   155          or HHTestCollisionYwithGear(Gear, -1)) then Gear.Y:= Gear.Y - 1;
       
   156       if not (TestCollisionXwithXYShift(Gear, 0, -1, Sign(Gear.dX))
       
   157          or HHTestCollisionYwithGear(Gear, -1)) then Gear.Y:= Gear.Y - 1;
       
   158       end;
       
   159    if not TestCollisionXwithGear(Gear, Sign(Gear.dX)) then Gear.X:= Gear.X + Gear.dX;
       
   160    
       
   161    if not HHTestCollisionYwithGear(Gear, 1) then
       
   162    begin
       
   163    Gear.Y:= Gear.Y + 1;
       
   164    if not HHTestCollisionYwithGear(Gear, 1) then
       
   165    begin
       
   166    Gear.Y:= Gear.Y + 1;
       
   167    if not HHTestCollisionYwithGear(Gear, 1) then
       
   168    begin
       
   169    Gear.Y:= Gear.Y + 1;
       
   170    if not HHTestCollisionYwithGear(Gear, 1) then
       
   171    begin
       
   172    Gear.Y:= Gear.Y + 1;
       
   173    if not HHTestCollisionYwithGear(Gear, 1) then
       
   174    begin
       
   175    Gear.Y:= Gear.Y + 1;
       
   176    if not HHTestCollisionYwithGear(Gear, 1) then
       
   177    begin
       
   178    Gear.Y:= Gear.Y + 1;
       
   179    if not HHTestCollisionYwithGear(Gear, 1) then
       
   180       begin
       
   181       Gear.Y:= Gear.Y - 6;
       
   182       Gear.dY:= 0;
       
   183       Gear.dX:= 0.0000001 * Sign(Gear.dX);
       
   184       Gear.State:= Gear.State or gstFalling
       
   185       end;
       
   186    SetAllHHToActive
       
   187    end
       
   188    end
       
   189    end
       
   190    end
       
   191    end
       
   192    end
       
   193    end
       
   194 end;
       
   195 
       
   196 ////////////////////////////////////////////////////////////////////////////////
       
   197 procedure doStepHedgehogFree(Gear: PGear);
       
   198 begin
       
   199 if not HHTestCollisionYwithGear(Gear, 1) then
       
   200    begin
       
   201    if (Gear.dY < 0) and HHTestCollisionYwithGear(Gear, -1) then Gear.dY:= 0;
       
   202    Gear.State:= Gear.State or gstFalling or gstMoving;
       
   203    Gear.dY:= Gear.dY + cGravity
       
   204    end else begin
       
   205    CheckHHDamage(Gear);
       
   206    if Gear.dY > 0    then Gear.dY:= 0;
       
   207    Gear.State:= Gear.State and not gstFalling;
       
   208    if ((Gear.State and gstMoving) <> 0) then Gear.dX:= Gear.dX * Gear.Friction
       
   209    end;
       
   210 
       
   211 
       
   212 if (Gear.State and gstMoving) <> 0 then
       
   213    if TestCollisionXwithGear(Gear, Sign(Gear.dX)) then
       
   214       if ((Gear.State and gstFalling) = 0) then
       
   215          if abs(Gear.dX) > 0.01 then
       
   216             if not TestCollisionXwithXYShift(Gear, 0, -1, Sign(Gear.dX)) then begin Gear.dX:= Gear.dX * 0.9; Gear.Y:= Gear.Y - 1 end else
       
   217             if not TestCollisionXwithXYShift(Gear, 0, -2, Sign(Gear.dX)) then begin Gear.dX:= Gear.dX * 0.9; Gear.Y:= Gear.Y - 2 end else
       
   218             if not TestCollisionXwithXYShift(Gear, 0, -3, Sign(Gear.dX)) then begin Gear.dX:= Gear.dX * 0.9; Gear.Y:= Gear.Y - 3 end else
       
   219             if not TestCollisionXwithXYShift(Gear, 0, -4, Sign(Gear.dX)) then begin Gear.dX:= Gear.dX * 0.9; Gear.Y:= Gear.Y - 4 end else
       
   220             if not TestCollisionXwithXYShift(Gear, 0, -5, Sign(Gear.dX)) then begin Gear.dX:= Gear.dX * 0.3; Gear.Y:= Gear.Y - 5 end else
       
   221             if abs(Gear.dX) > 0.02 then Gear.dX:= -0.5 * Gear.dX
       
   222                                    else begin
       
   223                                         Gear.State:= Gear.State and not gstMoving;
       
   224                                         Gear.dX:= 0.0000001 * Sign(Gear.dX)
       
   225                                         end
       
   226                                    else begin
       
   227                                         Gear.State:= Gear.State and not gstMoving;
       
   228                                         Gear.dX:= 0.0000001 * Sign(Gear.dX)
       
   229                                         end
       
   230          else Gear.dX:= -0.8 * Gear.dX;
       
   231 
       
   232 if ((Gear.State and gstFalling) = 0)and
       
   233    (sqr(Gear.dX) + sqr(Gear.dY) < 0.0008) then
       
   234    begin
       
   235    Gear.State:= Gear.State and not gstMoving;
       
   236    Gear.dX:= 0.0000001 * Sign(Gear.dX);
       
   237    Gear.dY:= 0
       
   238    end else Gear.State:= Gear.State or      gstMoving;
       
   239 
       
   240 if (Gear.State and gstMoving) <> 0 then
       
   241    begin
       
   242    Gear.X:= Gear.X + Gear.dX;
       
   243    Gear.Y:= Gear.Y + Gear.dY
       
   244    end else
       
   245    if Gear.Health = 0 then
       
   246       begin
       
   247       if AllInactive then
       
   248          begin
       
   249          doMakeExplosion(round(Gear.X), round(Gear.Y), 30, EXPLAutoSound);
       
   250          AddGear(round(Gear.X), round(Gear.Y), gtGrave, 0).Hedgehog:= Gear.Hedgehog;
       
   251          DeleteGear(Gear);
       
   252          SetAllToActive
       
   253          end;
       
   254       AllInactive:= false;  (* почему этого тут не было? *)
       
   255       exit
       
   256       end;
       
   257 
       
   258 AllInactive:= false;
       
   259 
       
   260 if (not CheckGearDrowning(Gear)) and
       
   261    ((Gear.State and gstMoving) = 0) then
       
   262       begin
       
   263       Gear.State:= 0;
       
   264       Gear.Active:= false;
       
   265       AddGearCR(Gear);
       
   266       exit
       
   267       end
       
   268 end;
       
   269 
       
   270 ////////////////////////////////////////////////////////////////////////////////
       
   271 procedure doStepHedgehog(Gear: PGear);
       
   272 begin
       
   273 if (Gear.Message and gm_Destroy) <> 0 then
       
   274    begin
       
   275    DeleteGear(Gear);
       
   276    exit
       
   277    end;
       
   278 if Gear.CollIndex < High(Longword) then DeleteCR(Gear);
       
   279 if (Gear.State and gstHHDriven) = 0 then doStepHedgehogFree(Gear)
       
   280                                     else doStepHedgehogDriven(Gear)
       
   281 end;