unit uBomb;


interface

uses
  Classes, uBox, zglHeader, uParticle;
type

{ TBomb }

TBomb = class(TBox)
  Timer : Integer;
  Particle : TParticles;
  Count : Integer;
  constructor Create; overload;
  procedure Draw; override;
  procedure Update; override;
  procedure Activate(B : Boolean); virtual;

end;

implementation
uses
  uHero, uWorld, uEnemy, uMaterials;
{ TBomb }

constructor TBomb.Create;
begin
  Init(50, 50, 8, 16, $FFFFFF);
  Active := true;
  Particle := TParticles.Create;
end;

procedure TBomb.Draw;
begin
  if not Active then
  begin
    asprite2d_Draw(mBomb, DrawRect.X, DrawRect.Y, DrawRect.W, DrawRect.H, 0, random(2) + 1);
    text_DrawEx(mFont_1, DrawRect.X - 3, DrawRect.Y - 25, 0.75, 1, u_IntToStr(Timer div 30), $FF, $FF5050);
  end;
end;

procedure TBomb.Update;
var
  I : Integer;
begin
  Particle.Update;
  if not Active then
  begin
    inherited Update;
    if Timer > 0 then
    begin
      Timer := Timer - 1;
      if Timer < 50 then
      //for I := 1 to 3 do
        Particle.Add(DrawRect.X - 4 + random(8), DrawRect.Y - 5 + random(10), random(2) + 1);
    end
    else
    begin
      for I := 0 to Enemys.Count - 1 do
        TEnemy(Enemys[I]).Look4Point(zCircle.cX, zCircle.cY);
      Active := true;
    end;
  end;
end;

procedure TBomb.Activate(B: Boolean);
var
  tRect : zglTRect;

  function NewPos(X, Y : Single) : boolean;
  begin
    tRect.X := Hero.zRect.X + X;
    tRect.Y := Hero.zRect.Y + Y;
    result := noCollision(tRect);
  end;
begin
  if Active and (Count > 0) then
  begin
    Count := Count - 1;
    zCircle.cX := Hero.zCircle.cX;
    zCircle.cY := Hero.zCircle.cY;
    RectUpdate;

    tRect := zRect;
    if NewPos( - zRect.W - 1, 0)
    or NewPos( + zRect.W + 1, 0)
    or NewPos( 0,  - zRect.H - 1)
    or NewPos( 0,  + zRect.H + 1) then
    begin
      zRect := tRect;
      Active := false;
      A := 0;
      OnHero:= false;
      OldX := zRect.X + zCircle.Radius;
      OldY := zRect.Y + zCircle.Radius;
      Z := zCircle.cY;
      RectUpdate;
      Timer := 250;
    end;
  end;
end;

end.

