Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Rocket;
- interface
- uses FMX.Objects, FMX.Ani, FMX.Types, FMX.Graphics, SysUtils, System.Types;
- const
- ANGLE_INC = 2;
- TICS_TILL_DIR_CHANGE = 30;
- TICS_TILL_EXLODE = 170;
- type
- TRocket = class
- private
- fPosition, fVelocity: TPointF;
- fAngle, fToAngle, fSpeed: Single;
- ExplosionType: Byte;
- TicsTillDirectionChange, TicsTillExlosion: Integer;
- fCurrentExplosionTics, fCurrentDirectionTics: Integer;
- fRocketImg, fExplodeImg: TImage;
- fExlosionAni, fRocketAnimationAni: TBitmapListAnimation;
- fDestroyed: Boolean;
- public
- Constructor Create; overload;
- Destructor Destroy; overload;
- Constructor Create(pX, pY, pAngle, pSpeed: Single); overload;
- procedure SetPosition(pPosition: TPointF);
- procedure SetVelocity(pVelocity: TPointF);
- procedure SetAngle(pDeg: Single);
- procedure SetToAngle(pDeg: Single);
- procedure SetSpeed(pSpeed: Single);
- procedure FreeRocket(Sender: TObject);
- procedure Update;
- property Destroyed: Boolean read fDestroyed;
- property Position: TPointF read fPosition write SetPosition;
- property Angle: Single read fAngle write SetAngle;
- property ToAngle: Single read fToAngle write SetToAngle;
- property CurrentExplosionTics: Integer read fCurrentExplosionTics;
- property CurrentDirectionTics: Integer read fCurrentDirectionTics;
- property Speed: Single read fSpeed write SetSpeed;
- property Velocity: TPointF read fVelocity write SetVelocity;
- end;
- implementation
- uses Animation;
- procedure TRocket.SetVelocity(pVelocity: TPointF);
- begin
- fVelocity := pVelocity;
- end;
- procedure TRocket.SetToAngle(pDeg: Single);
- begin
- fToAngle := pDeg;
- end;
- procedure TRocket.SetAngle(pDeg: Single);
- begin
- fAngle := pDeg;
- end;
- procedure TRocket.SetPosition(pPosition: TPointF);
- begin
- fPosition := pPosition;
- end;
- procedure TRocket.Update;
- var
- AnglesComparsion: Single;
- begin
- fRocketImg.RotationAngle := Angle;
- Velocity := TPointF.Create(Speed * cos((Angle - 90) * PI / 180),
- -Speed * sin((Angle + 90) * PI / 180));
- with Position do
- begin
- Position := TPointF.Create(X + Velocity.X, Y + Velocity.Y);
- fRocketImg.Position.X := X;
- fRocketImg.Position.Y := Y;
- end;
- Inc(fCurrentExplosionTics);
- Inc(fCurrentDirectionTics);
- if (fCurrentExplosionTics = TicsTillExlosion) then
- begin
- Speed := 0;
- fRocketImg.Visible := False;
- with fPosition do
- begin
- fExplodeImg.Position.X := X;
- fExplodeImg.Position.Y := Y;
- end;
- fExlosionAni.Enabled := True;
- end;
- if fCurrentDirectionTics = TicsTillDirectionChange then
- begin
- fCurrentDirectionTics := 0;
- ToAngle := 180 - Random(360);
- end
- else
- begin
- AnglesComparsion := Angle - ToAngle;
- if Abs(AnglesComparsion) > 3 then
- begin
- if AnglesComparsion < 0 then
- Angle := Angle + ANGLE_INC
- else
- Angle := Angle - ANGLE_INC;
- end;
- end;
- end;
- procedure TRocket.SetSpeed(pSpeed: Single);
- begin
- fSpeed := pSpeed;
- end;
- constructor TRocket.Create;
- begin
- inherited Create;
- end;
- destructor TRocket.Destroy;
- begin
- fRocketAnimationAni.Free;
- fRocketImg.Free;
- fExlosionAni.Free;
- fExplodeImg.Free;
- inherited Destroy;
- end;
- procedure TRocket.FreeRocket(Sender: TObject);
- begin
- fDestroyed := True;
- end;
- constructor TRocket.Create(pX, pY, pAngle, pSpeed: Single);
- begin
- inherited Create;
- Position := TPointF.Create(pX, pY);
- Self.fAngle := pAngle;
- ToAngle := pAngle;
- Speed := pSpeed;
- fDestroyed := False;
- ExplosionType := 1 + Random(2);
- TicsTillExlosion := 100 + Random(TICS_TILL_EXLODE);
- TicsTillDirectionChange := TICS_TILL_DIR_CHANGE;
- fCurrentExplosionTics := 0;
- fCurrentDirectionTics := 0;
- fRocketImg := TImage.Create(MainForm);
- with fRocketImg do
- begin
- Parent := MainForm;
- Position := TPosition.Create(TPointF.Create(pX, pY));
- RotationAngle := pAngle;
- Scale.X := 2;
- Scale.Y := 2;
- end;
- fRocketAnimationAni := TBitmapListAnimation.Create(fRocketImg);
- with fRocketAnimationAni do
- begin
- Parent := fRocketImg;
- AnimationBitmap := TBitmap.CreateFromFile('rocket.png');
- PropertyName := 'Bitmap';
- AnimationCount := 4;
- AnimationRowCount := 1;
- Duration := 0.3;
- Delay := 0;
- Loop := True;
- Enabled := True;
- end;
- fExplodeImg := TImage.Create(MainForm);
- with fExplodeImg do
- begin
- Scale.X := 3;
- Scale.Y := 3;
- Parent := MainForm;
- end;
- fExlosionAni := TBitmapListAnimation.Create(fExplodeImg);
- with fExlosionAni do
- begin
- Parent := fExplodeImg;
- AnimationBitmap := TBitmap.CreateFromFile('expl' + Inttostr(ExplosionType)
- + '.png');
- PropertyName := 'Bitmap';
- AnimationCount := 41;
- AnimationRowCount := 9;
- Duration := 4;
- Delay := 0;
- OnFinish := FreeRocket;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement