unit uBomb;

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

type
  TBombList = class(TList)
    Map : TMap;
    //Bullets : TBulletList;
    constructor Create(const pMap : TMap{; const pBullets : TBulletList}); overload;
    function  Add(E : TBombList) : TList; overload;
    procedure InBoom(X, Y, R, X0, Y0, X1, Y1 : Single); virtual;
  end;

  TBomb = class(TBombList)
    pX, pY, mX, mY, Angle : Single;
    W, H  : Integer;

    constructor Create(X, Y : Single);
    procedure Init; override;
    procedure Draw; override;
    procedure Update; override;
    procedure InBoom(X, Y, R, X0, Y0, X1, Y1 : Single); override;
  end;
implementation

uses Math;

{ TBombList }

function TBombList.Add(E: TBombList): TList;
begin
  E.Map := (Root as TBombList).Map;
//  E.Bullets := (Root as TBombList).Bullets;
  Result := inherited Add(E);
end;

constructor TBombList.Create(const pMap: TMap{;
  const pBullets: TBulletList});
begin
  inherited Create;
  Map := pMap;
 // Bullets := pBullets;
end;

procedure TBombList.InBoom(X, Y, R, X0, Y0, X1, Y1: Single);
begin

end;

{ TBomb }

constructor TBomb.Create;
begin
  pX := X;
  pY := Y;
  W := 32;
  H := 64;
  mX := 0;
  mY := (2 + random(2))/10;
end;

procedure TBomb.Draw;
begin
  ssprite2d_Draw(m_bomb, pX - W div 2, pY - H div 2, W, H, Angle);
  Next.Draw;
end;

procedure TBomb.Init;
begin

end;

procedure TBomb.Update;
begin
  Next.Update;
  pX := pX + mX;
  pY := pY + mY;
  mY := mY + 0.01;
  if pY > WinH + 32 then
  else
  if Map.Collis(pX, pY) then
    Del;
end;

procedure TBomb.InBoom(X, Y, R, X0, Y0, X1, Y1: Single);
var
  bbdX, bbdY, A : Single;
  I : Integer;
begin
  (Next as TBombList).InBoom(X, Y, R, X0, Y0, X1, Y1);
  if (pX > X0) and (pY > Y0) and (pX < X1) and (pY < Y1) then
  begin
    bbdX := pX - X;
    bbdY := pY - Y;
    if PointDistanceXY(bbdX, bbdY) < R then
    begin
      Points := Points + 1;
      for I := 1 to 3 + random(4) do
        Map.PParticles.Add(pX + random(8) - 4, pY + random(8) - 4, 1);
      Del;
    end
    else
    begin
     { if pX > X then M := +1
      else
      if pX < X then M := -1;  }
      A := asmroll(bbdX, bbdY);
      Angle := A * D180P;

      mX := cos(A);
      mY := sin(A);
    end;
  end;

end;

end.
