Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.07 KB | None | 0 0
  1. unit Rocket;
  2.  
  3. interface
  4.  
  5. uses FMX.Objects, FMX.Ani, FMX.Types, FMX.Graphics, SysUtils, System.Types;
  6.  
  7. const
  8.    ANGLE_INC = 2;
  9.    TICS_TILL_DIR_CHANGE = 30;
  10.    TICS_TILL_EXLODE = 170;
  11.  
  12. type
  13.    TRocket = class
  14.    private
  15.       fPosition, fVelocity: TPointF;
  16.       fAngle, fToAngle, fSpeed: Single;
  17.       ExplosionType: Byte;
  18.       TicsTillDirectionChange, TicsTillExlosion: Integer;
  19.       fCurrentExplosionTics, fCurrentDirectionTics: Integer;
  20.       fRocketImg, fExplodeImg: TImage;
  21.       fExlosionAni, fRocketAnimationAni: TBitmapListAnimation;
  22.       fDestroyed: Boolean;
  23.    public
  24.       Constructor Create; overload;
  25.       Destructor Destroy; overload;
  26.       Constructor Create(pX, pY, pAngle, pSpeed: Single); overload;
  27.       procedure SetPosition(pPosition: TPointF);
  28.       procedure SetVelocity(pVelocity: TPointF);
  29.       procedure SetAngle(pDeg: Single);
  30.       procedure SetToAngle(pDeg: Single);
  31.       procedure SetSpeed(pSpeed: Single);
  32.       procedure FreeRocket(Sender: TObject);
  33.       procedure Update;
  34.       property Destroyed: Boolean read fDestroyed;
  35.       property Position: TPointF read fPosition write SetPosition;
  36.       property Angle: Single read fAngle write SetAngle;
  37.       property ToAngle: Single read fToAngle write SetToAngle;
  38.       property CurrentExplosionTics: Integer read fCurrentExplosionTics;
  39.       property CurrentDirectionTics: Integer read fCurrentDirectionTics;
  40.       property Speed: Single read fSpeed write SetSpeed;
  41.       property Velocity: TPointF read fVelocity write SetVelocity;
  42.    end;
  43.  
  44. implementation
  45.  
  46. uses Animation;
  47.  
  48. procedure TRocket.SetVelocity(pVelocity: TPointF);
  49. begin
  50.    fVelocity := pVelocity;
  51. end;
  52.  
  53. procedure TRocket.SetToAngle(pDeg: Single);
  54. begin
  55.    fToAngle := pDeg;
  56. end;
  57.  
  58. procedure TRocket.SetAngle(pDeg: Single);
  59. begin
  60.    fAngle := pDeg;
  61. end;
  62.  
  63. procedure TRocket.SetPosition(pPosition: TPointF);
  64. begin
  65.    fPosition := pPosition;
  66. end;
  67.  
  68. procedure TRocket.Update;
  69. var
  70.    AnglesComparsion: Single;
  71. begin
  72.    fRocketImg.RotationAngle := Angle;
  73.    Velocity := TPointF.Create(Speed * cos((Angle - 90) * PI / 180),
  74.      -Speed * sin((Angle + 90) * PI / 180));
  75.    with Position do
  76.    begin
  77.       Position := TPointF.Create(X + Velocity.X, Y + Velocity.Y);
  78.       fRocketImg.Position.X := X;
  79.       fRocketImg.Position.Y := Y;
  80.    end;
  81.    Inc(fCurrentExplosionTics);
  82.    Inc(fCurrentDirectionTics);
  83.    if (fCurrentExplosionTics = TicsTillExlosion) then
  84.    begin
  85.       Speed := 0;
  86.       fRocketImg.Visible := False;
  87.       with fPosition do
  88.       begin
  89.          fExplodeImg.Position.X := X;
  90.          fExplodeImg.Position.Y := Y;
  91.       end;
  92.       fExlosionAni.Enabled := True;
  93.    end;
  94.    if fCurrentDirectionTics = TicsTillDirectionChange then
  95.    begin
  96.       fCurrentDirectionTics := 0;
  97.       ToAngle := 180 - Random(360);
  98.    end
  99.    else
  100.    begin
  101.       AnglesComparsion := Angle - ToAngle;
  102.       if Abs(AnglesComparsion) > 3 then
  103.       begin
  104.          if AnglesComparsion < 0 then
  105.             Angle := Angle + ANGLE_INC
  106.          else
  107.             Angle := Angle - ANGLE_INC;
  108.       end;
  109.    end;
  110. end;
  111.  
  112. procedure TRocket.SetSpeed(pSpeed: Single);
  113. begin
  114.    fSpeed := pSpeed;
  115. end;
  116.  
  117. constructor TRocket.Create;
  118. begin
  119.    inherited Create;
  120. end;
  121.  
  122. destructor TRocket.Destroy;
  123. begin
  124.    fRocketAnimationAni.Free;
  125.    fRocketImg.Free;
  126.    fExlosionAni.Free;
  127.    fExplodeImg.Free;
  128.    inherited Destroy;
  129. end;
  130.  
  131. procedure TRocket.FreeRocket(Sender: TObject);
  132. begin
  133.    fDestroyed := True;
  134. end;
  135.  
  136. constructor TRocket.Create(pX, pY, pAngle, pSpeed: Single);
  137. begin
  138.    inherited Create;
  139.    Position := TPointF.Create(pX, pY);
  140.    Self.fAngle := pAngle;
  141.    ToAngle := pAngle;
  142.    Speed := pSpeed;
  143.    fDestroyed := False;
  144.    ExplosionType := 1 + Random(2);
  145.    TicsTillExlosion := 100 + Random(TICS_TILL_EXLODE);
  146.    TicsTillDirectionChange := TICS_TILL_DIR_CHANGE;
  147.    fCurrentExplosionTics := 0;
  148.    fCurrentDirectionTics := 0;
  149.    fRocketImg := TImage.Create(MainForm);
  150.    with fRocketImg do
  151.    begin
  152.       Parent := MainForm;
  153.       Position := TPosition.Create(TPointF.Create(pX, pY));
  154.       RotationAngle := pAngle;
  155.       Scale.X := 2;
  156.       Scale.Y := 2;
  157.    end;
  158.    fRocketAnimationAni := TBitmapListAnimation.Create(fRocketImg);
  159.    with fRocketAnimationAni do
  160.    begin
  161.       Parent := fRocketImg;
  162.       AnimationBitmap := TBitmap.CreateFromFile('rocket.png');
  163.       PropertyName := 'Bitmap';
  164.       AnimationCount := 4;
  165.       AnimationRowCount := 1;
  166.       Duration := 0.3;
  167.       Delay := 0;
  168.       Loop := True;
  169.       Enabled := True;
  170.    end;
  171.    fExplodeImg := TImage.Create(MainForm);
  172.    with fExplodeImg do
  173.    begin
  174.       Scale.X := 3;
  175.       Scale.Y := 3;
  176.       Parent := MainForm;
  177.    end;
  178.    fExlosionAni := TBitmapListAnimation.Create(fExplodeImg);
  179.    with fExlosionAni do
  180.    begin
  181.       Parent := fExplodeImg;
  182.       AnimationBitmap := TBitmap.CreateFromFile('expl' + Inttostr(ExplosionType)
  183.         + '.png');
  184.       PropertyName := 'Bitmap';
  185.       AnimationCount := 41;
  186.       AnimationRowCount := 9;
  187.       Duration := 4;
  188.       Delay := 0;
  189.       OnFinish := FreeRocket;
  190.    end;
  191. end;
  192.  
  193. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement