unit uBonus;

interface
uses
  uHero, GSound, zglHeader, LexUtils;

type
  tBonus = class
    PHero : THero;
    dX, dY, mX, mY,
    pX, pY : Single;
    minX : Integer;
    T : Byte;
    BonusY, BonusYP,
    Angle, pAngle : Single;
    Enabled : Boolean;
    constructor Create(Hero : THero; X : Single);
    procedure Init1(X : Single);
    procedure Init;
    procedure Draw;
    procedure Update;
    procedure Check;
  end;
implementation
uses
  uConst, u_Player, uQuest, uMaterials;
{ tBonus }

procedure tBonus.Check;

procedure Move;
var
  A : Single;
begin
  pengine2d_AddEmitter( @em_bonus_boom, nil, pX + 16, pY + BonusY );
  A := LineAngleXY(dX - pX, dY - pY) * P180 ;
  mX := Cos(A) * 10;
  mY := Sin(A) * 10;
end;

procedure QuestMove(Q : Integer);
begin
  if QuestT = Q then
  begin
     Quest.LNow := Quest.LNow + 1;
     dX := WinW - 32;
     dY := WinH - 16;
     PlaySound(10);
  end
  else
  begin
    iPlayer.Point := iPlayer.Point + 1;
    dX := WinW div 2;
    dY := 32;
    PlaySound(9);
  end;
  Move;
end;

procedure MoneyMove;
begin
  dX := 32;
  dY := WinH - 32;
  Move;
end;

procedure PointMove;
begin
  dX := WinW div 2;
  dY := 16;
  Move;
  PlaySound(9);
end;

begin
  case T of
  1 : begin
        QuestMove(1);
      end;
  2, 5, 6, 8 : begin
        QuestMove(2);
      end;
  3, 4 : begin
        QuestMove(3);
      end;

  7 : begin
        if PHero.Health < 5 then
        begin
          PHero.Health := PHero.Health + 1;
          dX := 47;
          dY := 195;
          Move;
          PlaySound(4);
        end
        else
        begin
          iPlayer.Point := iPlayer.Point + 10;
          PointMove;
        end;
      end;
  9, 13 : begin
        if PHero.Magic < 250 then
        begin
          PHero.Magic := PHero.Magic + 25;
          dX := 16;
          dY := 243;
          Move;
          PlaySound(2);
        end
        else
        begin
          iPlayer.Point := iPlayer.Point + 5;
          PointMove;
        end;
      end;
  10 : begin
        iPlayer.Point := iPlayer.Point + 10;
        PointMove;
        PlaySound(8);
      end;
  11 : begin
        iPlayer.Money := iPlayer.Money + 1;
        iPlayer.Point := iPlayer.Point + 3;
        MoneyMove;
        PlaySound(5);
      end;
  12 : begin
        iPlayer.Money := iPlayer.Money + 5;
        iPlayer.Point := iPlayer.Point + 9;
        PlaySound(6);
        MoneyMove;
      end;
  14 : begin
          PHero.Health := PHero.Health - 1;
          dX := 47;
          dY := 195;
          Move;
          PlaySound(12);
      end;
   15 : begin
          PHero.Health := PHero.Health - 1;
          dX := 47;
          dY := 195;
          Move;
          PlaySound(10);
      end;
  16: begin
          PHero.Magic := PHero.Magic - 10;
          dX := 16;
          dY := 243;
          Move;
          PlaySound(3);
      end;
  else
    iPlayer.Point := iPlayer.Point + 10;
    PointMove;
  end;

end;

constructor tBonus.Create(Hero: THero; X : Single);
begin
  PHero := Hero;
  Init;
  pX := pX + X;
end;

procedure tBonus.Draw;
begin
  asprite2d_Draw(mBonus, pX, pY - 16 + BonusY, 32, 32, Angle, T);
end;

procedure tBonus.Init;
begin
  Enabled := true;
  pY := random(bonusDY) * 16 + bonusPY;
  pX := WinWx15 + random(bonusDX)*32;
  case QuestT of
  1 : if random > 0.5 then T := 1 else T := random(16) + 1;
  2 : if random > 0.3 then T := 2 else T := random(16) + 1;
  3 : if random > 0.65 then T := 2 else T := random(16) + 1;
  else
  if random > 0.6 then T := 9 else T := random(16) + 1;
  end;

  pAngle := + 1;
  minX := - WinWd2 - random(WinWd2);
  BonusYP := random(360);
end;

procedure tBonus.Init1(X : Single);
begin
  Init;
  pX := pX + X;
end;

procedure tBonus.Update;



begin
  if Enabled then
  begin
    pX := pX - cCamSpeed;
    if pX < minX then
      Init;

    if PHero.Collis(pX + 8, pY + BonusY)
    then
    begin
      Enabled := False;
      Check;
    end;

    Angle := Angle + pAngle;
    if pAngle < 0 then
    begin
      if Angle < -20 then
        pAngle := + 1;
    end
    else
    begin
      if Angle > 20 then
        pAngle := - 1.5;
    end;
    BonusY := sin(BonusYP * pi/180) * 10;
    if BonusYP > 360 then
      BonusYP := 0
    else
      BonusYP := BonusYP + 2.5;
  end
  else
  begin
    pX := pX + mX;
    pY := pY + mY;
    pengine2d_AddEmitter( @em_bonus_boom, nil, pX + 16, pY + BonusY );
    if  (pX > dX - 32) and (pX < dX + 32)
    and (pY > dY - 32) and (pY < dY + 32) then
      Init;
  end;
end;


end.
