unit uBullet;

interface
uses
  zglHeader, uMaterials, uList, LexUtils,
  uMap, uBoom;

type
  TBulletList = class(TList)
    Map : TMap;
    Booms : TBoomList;
    constructor Create(const pMap : TMap; const pBooms : TBoomList);  overload;
    function  Add(E : TBulletList) : TList; overload;
  end;

  TBullet = class(TBulletList)
    W, H, Angle,
    goalX0, goalY0, goalX1, goalY1 : Integer;
    pX, pY, cX, cY, mX, mY, Speed : Single;
    constructor Create(const Angle_, X, Y, goalX_, goalY_ : Integer); overload;
    procedure Init; override;
    procedure Draw; override;
    procedure Update; override;
  end;
implementation
uses
  uConst;
{ TBulletList }

function TBulletList.Add(E: TBulletList): TList;
begin
  E.Map := (Root as TBulletList).Map;
  E.Booms := (Root as TBulletList).Booms;
  Result := inherited Add(E);
end;

constructor TBulletList.Create(const pMap: TMap; const pBooms : TBoomList);
begin
  inherited Create;
  Map := pMap;
  Booms := pBooms;
end;

{ TBullet }

constructor TBullet.Create(const Angle_, X, Y, goalX_, goalY_: Integer);
begin
  Speed := 3;
  W := 16;
  H := 32;
  //____________
  cX := X;
  cY := Y;
  pX := cX - W div 2;
  pY := cY - H div 2;
  Angle := Angle_ - 90;
  goalX0 := goalX_ - 32;
  goalY0 := goalY_ - 32;
  goalX1 := goalX_ + 32;
  goalY1 := goalY_ + 32;
end;

procedure TBullet.Draw;
begin
  ssprite2d_Draw(m_bullet, pX, pY, W, H, Angle);
  ssprite2d_Draw(m_goal,goalX0, goalY0, 64, 64, 0);
  Next.Draw;
end;

procedure TBullet.Init;
var
  A : Single;
begin
  A := (Angle - 90) * P180;
  mX := cos(A) * Speed;
  mY := sin(A) * Speed;
end;

procedure TBullet.Update;
begin
  Next.Update;
  pX := pX + mX;
  pY := pY + mY;
  cX := cX + mX;
  cY := cY + mY;
  if  (cX > goalX0) and (cY > goalY0)
  and (cX < goalX1) and (cY < goalY1) then
  begin
    Booms.Add(TBoom.Create(trunc(cX), trunc(cY)));
    Del;
  end
  else
  if (cX < -16) or (cY < -16)  or (cX > WinW + 16) or (cY > WinH + 16) then
    Del;
end;

end.
