unit GM_Scene;

interface
 uses
   IDX_Engine,
   GM_Vars,
   Windows,
   Math;

const
  cgfx_olen =0;
  cgfx_player =1;
  cgfx_star =2;
  cgfx_stard =3;
  cgfx_elka =4;
  cgfx_snegovik =5;
  cgfx_sliz =6;
  cgfx_bullet =7;
  cgfx_ufo =8;
  cgfx_glaz =9;
  cgfx_particle =10;

  cgfx_boos1 =0;
  cgfx_boos2 =1;


Const
  ParticleMax   = 1000;
  EnemyMax      = 100;
  BulletMax     = 500;
  EBulletMax    = 100;
  PowerupMax    = 100;
  StarsMax      = 100;

  gmsStart      =0;
  gmsPlayerDead =1;
  gmsNextLevel  =2;
  gmsGameFin    =3;

type
  TEAnim=record
   Name:string;
   Slide:array of TAtlasItem;
end;

Type
  TPlayer=record
    x:Single;
    y:Single;
    lives:Integer;
    health:Single;
end;

Type
  TOBullet=object
    alive:Boolean;
    x:Single;
    y:Single;
    angle:Single;
    btype:Integer;
    procedure  Update;
    procedure  Draw;
end;

Type
   TOEBullet=object
    alive:Boolean;
    x:Single;
    y:Single;
    angle:Single;
    procedure Update;
    procedure Draw;
end;


Type
  TOPowerup=object
    alive:Boolean;
    x:Single;
    y:Single;
    fadeout:Single;
    initialx:Single;
    tmpsin:Single;
    ptype:Integer;
    state:Byte;
    procedure Update;
    procedure Draw;
end;

Type
  TOParticle=object
    alive:Boolean;
    x:Single;
    y:Single;
    size :Single;
    angle :Single;
    energy :Single;
    maxenergy :Single;
    mycolor :Cardinal;
    fromboss:Integer;
    procedure Update;
    procedure Draw;
end;

Type
  TOEnemy=object
    alive:Boolean;
    x:Single;
    y:Single;
    health:Integer;
    image:Integer;
    ai:Integer;
    weapon:Integer;
    seed:Integer;
    t:Single;
    t2:Single;
    groupnumber:Integer;

    epos:array[0..3] of TPoint;

    Anim_Delay:Integer;
    Anim_Tmr:Cardinal;
    Anim_Num:Integer;

    procedure Update;
    procedure DrawShadow;
    procedure Draw;
    procedure GenerateDistPos;
end;

Type
  TOBoss=object
    alive:Boolean;
    initialx:Integer;
    initialy:Integer;
    x:Single;
    y:Single;
    angle:Single;
    btype:Integer;
    health:Integer;
    maxhealth:Integer;
    fadein:Single;

    Anim_Delay:Integer;
    Anim_Tmr:Cardinal;
    Anim_Num:Integer;

    procedure Update;
    procedure Draw;
end;

Type
  TOStars=object
    x:Single;
    y:Single;
    alpha:Integer;
    level:Single;
    size:Single;
    procedure Update;
    procedure Draw;
end;


procedure Scene_Init();
procedure Scene_Reset();
procedure Scene_Draw();
procedure Scene_Update();

var
  bullet:array[0..BulletMax]of TOBullet;
  ebullet:array[0..EBulletMax]of TOEBullet;
  powerup:array[0..PowerupMax] of TOPowerup;
  particle:array[0..ParticleMax] of TOParticle;
  enemy:array[0..EnemyMax] of TOenemy;
  stars:array[0..StarsMax] of TOStars;
  boss:TOboss;
  player:TPlayer;

  scrollingspeed:Single = 2;

  score,
  amaranth,
  amaranthold:Integer;
  boosinit:Boolean=False;
  bossdead:Integer=0;
  highscore:Integer;

  bullet_level:Integer=1;
  level:Integer=0;

  mousetimer,
  mousetimer2,
  espawntimer,
  traintimer,
  scrolling:Single;

  gametimer:Single;
  deltatime:Single;

  debugmode:Boolean=True ;

  EAanim:array of TEAnim;
  EABanim:array of TEAnim;

  gamestate:Byte=gmsStart;

procedure MakeBullet(x:Single; y:Single; mytype:Integer; angle:Single = 180);
procedure MakeEBullet(x:Single; y:Single; angle:Single);
procedure MakeBoss(bosstype:Integer);
procedure MakeEnemy(health:Integer; image:Integer; ai:Integer;
                    weapon:Integer; seed:Integer; groupnumber:Integer);

procedure MakeStars;
procedure MakeParticle(x,y:Single; size:Single; angle:Single;
                       maxenergy:Integer; mycolor:Cardinal; fromboss:Integer = 0);

procedure CheckEnemyCollision;

implementation
 uses
   OpenGl,
   SysUtils;

procedure initAtlas();
begin
Setlength(EAanim,11);

Setlength(EAanim[cgfx_olen].slide,1);
EAanim[cgfx_olen].slide[0].x1:=0.00;
EAanim[cgfx_olen].slide[0].y1:=1;
EAanim[cgfx_olen].slide[0].x2:=0.24609375;
EAanim[cgfx_olen].slide[0].y2:=0.62890625;
EAanim[cgfx_olen].slide[0].w:=63;
EAanim[cgfx_olen].slide[0].h:=95;

Setlength(EAanim[cgfx_player].slide,1);
EAanim[cgfx_player].slide[0].x1:=0.25;
EAanim[cgfx_player].slide[0].y1:=1;
EAanim[cgfx_player].slide[0].x2:=0.5234375;
EAanim[cgfx_player].slide[0].y2:=0.671875;
EAanim[cgfx_player].slide[0].w:=70;
EAanim[cgfx_player].slide[0].h:=84;

Setlength(EAanim[cgfx_star].slide,2);
EAanim[cgfx_star].slide[0].x1:=0.64;
EAanim[cgfx_star].slide[0].y1:=0.703125;
EAanim[cgfx_star].slide[0].x2:=0.99609375;
EAanim[cgfx_star].slide[0].y2:=0.41796875;
EAanim[cgfx_star].slide[0].w:=90;
EAanim[cgfx_star].slide[0].h:=73;
EAanim[cgfx_star].slide[1].x1:=0.53;
EAanim[cgfx_star].slide[1].y1:=1;
EAanim[cgfx_star].slide[1].x2:=0.87890625;
EAanim[cgfx_star].slide[1].y2:=0.70703125;
EAanim[cgfx_star].slide[1].w:=90;
EAanim[cgfx_star].slide[1].h:=75;

Setlength(EAanim[cgfx_stard].slide,3);
EAanim[cgfx_stard].slide[0].x1:=0.40;
EAanim[cgfx_stard].slide[0].y1:=0.19140625;
EAanim[cgfx_stard].slide[0].x2:=0.5859375;
EAanim[cgfx_stard].slide[0].y2:=0.07421875;
EAanim[cgfx_stard].slide[0].w:=47;
EAanim[cgfx_stard].slide[0].h:=30;
EAanim[cgfx_stard].slide[1].x1:=0.43;
EAanim[cgfx_stard].slide[1].y1:=0.66796875;
EAanim[cgfx_stard].slide[1].x2:=0.640625;
EAanim[cgfx_stard].slide[1].y2:=0.4140625;
EAanim[cgfx_stard].slide[1].w:=53;
EAanim[cgfx_stard].slide[1].h:=65;
EAanim[cgfx_stard].slide[2].x1:=0.26;
EAanim[cgfx_stard].slide[2].y1:=0.390625;
EAanim[cgfx_stard].slide[2].x2:=0.44921875;
EAanim[cgfx_stard].slide[2].y2:=0.1953125;
EAanim[cgfx_stard].slide[2].w:=49;
EAanim[cgfx_stard].slide[2].h:=50;

Setlength(EAanim[cgfx_elka].slide,1);
EAanim[cgfx_elka].slide[0].x1:=0.09;
EAanim[cgfx_elka].slide[0].y1:=0.625;
EAanim[cgfx_elka].slide[0].x2:=0.4296875;
EAanim[cgfx_elka].slide[0].y2:=0.39453125;
EAanim[cgfx_elka].slide[0].w:=86;
EAanim[cgfx_elka].slide[0].h:=59;

Setlength(EAanim[cgfx_snegovik].slide,1);
EAanim[cgfx_snegovik].slide[0].x1:=0.67;
EAanim[cgfx_snegovik].slide[0].y1:=0.4140625;
EAanim[cgfx_snegovik].slide[0].x2:=0.99609375;
EAanim[cgfx_snegovik].slide[0].y2:=0.2109375;
EAanim[cgfx_snegovik].slide[0].w:=84;
EAanim[cgfx_snegovik].slide[0].h:=52;

Setlength(EAanim[cgfx_sliz].slide,1);
EAanim[cgfx_sliz].slide[0].x1:=0.45;
EAanim[cgfx_sliz].slide[0].y1:=0.41015625;
EAanim[cgfx_sliz].slide[0].x2:=0.6640625;
EAanim[cgfx_sliz].slide[0].y2:=0.20703125;
EAanim[cgfx_sliz].slide[0].w:=54;
EAanim[cgfx_sliz].slide[0].h:=52;

Setlength(EAanim[cgfx_bullet].slide,5);
EAanim[cgfx_bullet].slide[0].x1:=0.88;
EAanim[cgfx_bullet].slide[0].y1:=1;
EAanim[cgfx_bullet].slide[0].x2:=0.96875;
EAanim[cgfx_bullet].slide[0].y2:=0.796875;
EAanim[cgfx_bullet].slide[0].w:=22;
EAanim[cgfx_bullet].slide[0].h:=52;
EAanim[cgfx_bullet].slide[1].x1:=0.21;
EAanim[cgfx_bullet].slide[1].y1:=0.390625;
EAanim[cgfx_bullet].slide[1].x2:=0.25390625;
EAanim[cgfx_bullet].slide[1].y2:=0.25;
EAanim[cgfx_bullet].slide[1].w:=12;
EAanim[cgfx_bullet].slide[1].h:=36;
EAanim[cgfx_bullet].slide[2].x1:=0.18;
EAanim[cgfx_bullet].slide[2].y1:=0.19140625;
EAanim[cgfx_bullet].slide[2].x2:=0.26953125;
EAanim[cgfx_bullet].slide[2].y2:=0.05078125;
EAanim[cgfx_bullet].slide[2].w:=23;
EAanim[cgfx_bullet].slide[2].h:=36;
EAanim[cgfx_bullet].slide[3].x1:=0.00;
EAanim[cgfx_bullet].slide[3].y1:=0.625;
EAanim[cgfx_bullet].slide[3].x2:=0.08984375;
EAanim[cgfx_bullet].slide[3].y2:=0.4453125;
EAanim[cgfx_bullet].slide[3].w:=22;
EAanim[cgfx_bullet].slide[3].h:=46;
EAanim[cgfx_bullet].slide[4].x1:=0.00;
EAanim[cgfx_bullet].slide[4].y1:=0.44140625;
EAanim[cgfx_bullet].slide[4].x2:=0.0859375;
EAanim[cgfx_bullet].slide[4].y2:=0.26171875;
EAanim[cgfx_bullet].slide[4].w:=22;
EAanim[cgfx_bullet].slide[4].h:=46;

Setlength(EAanim[cgfx_ufo].slide,1);
EAanim[cgfx_ufo].slide[0].x1:=0.69;
EAanim[cgfx_ufo].slide[0].y1:=0.20703125;
EAanim[cgfx_ufo].slide[0].x2:=0.99609375;
EAanim[cgfx_ufo].slide[0].y2:=0.05859375;
EAanim[cgfx_ufo].slide[0].w:=78;
EAanim[cgfx_ufo].slide[0].h:=38;

Setlength(EAanim[cgfx_glaz].slide,1);
EAanim[cgfx_glaz].slide[0].x1:=0.00;
EAanim[cgfx_glaz].slide[0].y1:=0.2578125;
EAanim[cgfx_glaz].slide[0].x2:=0.17578125;
EAanim[cgfx_glaz].slide[0].y2:=0.109375;
EAanim[cgfx_glaz].slide[0].w:=45;
EAanim[cgfx_glaz].slide[0].h:=38;

Setlength(EAanim[cgfx_particle].slide,1);
EAanim[cgfx_particle].slide[0].x1:=0.27;
EAanim[cgfx_particle].slide[0].y1:=0.19140625;
EAanim[cgfx_particle].slide[0].x2:=0.3984375;
EAanim[cgfx_particle].slide[0].y2:=0.06640625;
EAanim[cgfx_particle].slide[0].w:=32;
EAanim[cgfx_particle].slide[0].h:=32;

Setlength(EABanim,2);

Setlength(EABanim[cgfx_boos1].slide,3);
EABanim[cgfx_boos1].slide[0].x1:=0.00;
EABanim[cgfx_boos1].slide[0].y1:=1;
EABanim[cgfx_boos1].slide[0].x2:=0.5625;
EABanim[cgfx_boos1].slide[0].y2:=0.787109375;
EABanim[cgfx_boos1].slide[0].w:=144;
EABanim[cgfx_boos1].slide[0].h:=109;
EABanim[cgfx_boos1].slide[1].x1:=0.00;
EABanim[cgfx_boos1].slide[1].y1:=0.78515625;
EABanim[cgfx_boos1].slide[1].x2:=0.56640625;
EABanim[cgfx_boos1].slide[1].y2:=0.580078125;
EABanim[cgfx_boos1].slide[1].w:=145;
EABanim[cgfx_boos1].slide[1].h:=105;
EABanim[cgfx_boos1].slide[2].x1:=0.00;
EABanim[cgfx_boos1].slide[2].y1:=0.578125;
EABanim[cgfx_boos1].slide[2].x2:=0.5625;
EABanim[cgfx_boos1].slide[2].y2:=0.390625;
EABanim[cgfx_boos1].slide[2].w:=144;
EABanim[cgfx_boos1].slide[2].h:=96;

Setlength(EABanim[cgfx_boos2].slide,1);
EABanim[cgfx_boos2].slide[0].x1:=0.00;
EABanim[cgfx_boos2].slide[0].y1:=0.388671875;
EABanim[cgfx_boos2].slide[0].x2:=0.46484375;
EABanim[cgfx_boos2].slide[0].y2:=0.224609375;
EABanim[cgfx_boos2].slide[0].w:=119;
EABanim[cgfx_boos2].slide[0].h:=84;

end;

procedure SaveHighscore;
var
  F1: file of Integer;
begin
  If score > highscore Then
  begin
    highscore:= score;
    AssignFile(F1, appPath+'Data\highscore.dat');
    Rewrite(F1);
    Write(F1, highscore);
    CloseFile(F1);
  end;

end;

procedure LoadHighscore;
var
  F1: file of Integer;
begin
 highscore:=0;

 if fileexists(appPath+'Data\highscore.dat') then
 begin
  AssignFile(F1, appPath+'Data\highscore.dat');
  Reset(F1);
  Read(F1, highscore);
  CloseFile(F1);
 end;

end;


procedure Scene_Init();
begin
 Randomize;
 LoadHighscore;
 initAtlas;
 Scene_Reset;

 gameSound.PlaySound:=True;
 gameSound.PlayMusic:=True;
end;

procedure Scene_Reset();
var
  i:Integer;
begin
  MakeStars;

  gamestate:=gmsStart;
  gametimer:=GetTickCount;
  mousetimer := gametimer;
  mousetimer2 := gametimer;
  espawntimer := gametimer;
  traintimer:= gametimer;

  amaranth:= 0;
  amaranthold:= 0;
  boss.alive:=False;
  boosinit:=False;
  bossdead:= 0;
  bullet_level:= 1;
  level:= 1;
  score:= 0;
  player.lives := 5;
  player.health:= 100;

  player.x:= gameWND.DWidth;
  player.y:= gameWND.Height - 30;

  for i:= 0 to PowerupMax do   powerup[i].alive:= False;
  for i:= 0 to EBulletMax do   ebullet[i].alive:= False;
  for i:= 0 to EnemyMax do     enemy[i].alive:= False;
  for i:= 0 to BulletMax do    bullet[i].alive:= False;
  for i:= 0 to ParticleMax do  particle[i].alive:= False;
end;


procedure Scene_Draw();
var
  i:Integer;
  r1,g1,b1:Integer;
  r2,g2,b2,r,g,b:Integer;
  rdiff,gdiff,bdiff:Integer;
  colordist:Single;
  lw:Single;
begin

  for i:=0 to StarsMax do stars[i].Draw;


  Boss.Draw;

  for i:=0 To PowerupMax do powerup[i].Draw;
  for i:=0 To EBulletMax do ebullet[i].Draw;
  for i:=0 To EnemyMax do enemy[i].Draw;
  for i:=0 To BulletMax do bullet[i].Draw;
  for i:=0 To ParticleMax do particle[i].Draw;

 glEnable(GL_BLEND);

 IDXENG_Draw2d_TextureA(player.x,player.y,
                                EAanim[cgfx_player].slide[0].w,
                                EAanim[cgfx_player].slide[0].h,
                                TestGFX,
                                EAanim[cgfx_player].slide[0],
                                0,
                                True );

 colordist:= player.health / 100;

 r1:= 155;
 g1:= 0;
 b1:= 0;

 r2:= 100;
 g2:= 100;
 b2:= 100;

 rdiff:= r1 - r2;
 gdiff:= g1 - g2;
 bdiff:= b1 - b2;

 r:= IDXUtils_ClampByte(r1 - Trunc(rdiff * colordist));
 g:= IDXUtils_ClampByte(g1 - Trunc(gdiff * colordist));
 b:= IDXUtils_ClampByte(b1 - Trunc(bdiff * colordist));


 lw:=2.66;
 IDXENG_Draw2d_Rectangle(gameWND.Width - 271,
                         5,
                         lw*player.health,
                         12,rgb(r, g, b),200,0,True);

  IDXENG_Draw2d_Rectangle(gameWND.Width - 271,
                         5,
                         266,
                         12,$FFFFFF,100);

  lw:=(266/351);
  i:=amaranth;
  if i>351 then i:=351;
  IDXENG_Draw2d_Rectangle(gameWND.Width - 271,
                         20,
                         lw*i,
                         5,$F97C00,200,0,True);

  IDXENG_Draw2d_Rectangle(gameWND.Width - 271,
                         20,
                         266,
                         5,$FFFFFF,100);


  IDXENG_Draw2d_Text(MySysfont[2],'x'+IDXUtils_IntToStr(player.lives),gameWND.Width-28,20,$CDCDCD,255);
  IDXENG_Draw2d_Text(MySysfont[2],IDXUtils_IntToStr(score),3,20,$CDCDCD,255);

//  IDXENG_Draw2d_Text(MySysfont[2],IDXUtils_IntToStr(amaranth),3,40,$CDCDCD,255);

  case gamestate of
   gmsStart:
    begin


    end;
   gmsPlayerDead:
    begin
     IDXENG_Draw2d_Text(MySysfont[2],'Вы проиграли...',180,gameWND.DHeight-100,$CDCDCD,255);
     IDXENG_Draw2d_Text(MySysfont[2],'Деду Морозу так и не удалось выйти из своего',180,gameWND.DHeight-80,$CDCDCD,255);
     IDXENG_Draw2d_Text(MySysfont[2],'страшного сна. Он навеки застрял в этом кошмаре...',180,gameWND.DHeight-60,$CDCDCD,255);
     IDXENG_Draw2d_Text(MySysfont[2],'Нажмите пробел или Enter для продолжения',150,gameWND.DHeight-20,$CDCDCD,255);
    end;
   gmsNextLevel:
    begin
     IDXENG_Draw2d_Text(MySysfont[2],'Поздравляю!! Вы перешли на следующий уровень',150,gameWND.DHeight-100,$CDCDCD,255);
     IDXENG_Draw2d_Text(MySysfont[2],'Нажмите пробел или Enter для продолжения',150,gameWND.DHeight-60,$CDCDCD,255);
    end;
    gmsGameFin:
    begin
      IDXENG_Draw2d_Text(MySysfont[2],'Поздравляю!!!',180,gameWND.DHeight-160,$CDCDCD,255);
      IDXENG_Draw2d_Text(MySysfont[2],'Вы прошли страшный сон Деда Мороза',180,gameWND.DHeight-140,$CDCDCD,255);
      IDXENG_Draw2d_Text(MySysfont[2],'Новый год и подарки спасены',180,gameWND.DHeight-120,$CDCDCD,255);
      IDXENG_Draw2d_Text(MySysfont[2],'Вы сотворили великое дело и не дали',180,gameWND.DHeight-100,$CDCDCD,255);
      IDXENG_Draw2d_Text(MySysfont[2],'победить злу',180,gameWND.DHeight-80,$CDCDCD,255);
      IDXENG_Draw2d_Text(MySysfont[2],'Нажмите пробел или Enter для продолжения',150,gameWND.DHeight-20,$CDCDCD,255);
    end;

  end;
end;

procedure Scene_Update();
var
 enemyai:Integer;
 enemyseed:Integer;
 enemypic:Integer;
 i:Integer;
begin

 case gamestate of
  gmsPlayerDead,gmsGameFin :
  begin
   if gameWND.Keys[32] or gameWND.Keys[13] then
   begin
     SaveHighscore;
     GameStatus:=sGMenu;
     gameWND.Keys[32]:=False;
     gameWND.Keys[13]:=False;
     Exit;
   end;

  end;
  gmsNextLevel :
  begin
   if gameWND.Keys[32] or gameWND.Keys[13] then
   begin
      gametimer:= GetTickCount;
      mousetimer := gametimer;
      mousetimer2 := gametimer;
      espawntimer := gametimer;
      traintimer:= gametimer;
      amaranth := 0;
      amaranthold := 0;
      boss.alive := False;
      boosinit:= False;
      bossdead:= 0;
      bullet_level := 1;
      Inc(player.lives);
      player.health:= 100;
      player.x := gameWND.DWidth;
      player.y := gameWND.Height - 30;

        for i:= 0 to PowerupMax do   powerup[i].alive:= False;
        for i:= 0 to EBulletMax do   ebullet[i].alive:= False;
        for i:= 0 to EnemyMax do     enemy[i].alive:= False;
        for i:= 0 to BulletMax do    bullet[i].alive:= False;
        for i:= 0 to ParticleMax do  particle[i].alive:= False;

      gamestate:=gmsStart;

     gameWND.Keys[32]:=False;
     gameWND.Keys[13]:=False;
     Exit;
   end;

  end;

 end;


 if gameWND.Keys[VK_F1] then
   begin
     gameWND.Keys[VK_F1]:=False;
     amaranth:=350;
   end;

 if gamestate<>gmsStart then Exit;


   // gametimer:=gametimer + gameWND.deltaTime;
    deltatime:= 1;//gameWND.deltaTime * 90;


player.x:=gameWND.Mouse.X;
player.y:=gameWND.Mouse.Y;

   If player.x < 23 Then player.x := 23;
   If player.x > (gameWND.Width - 25) Then player.x := (gameWND.Width - 25);
   If player.y < 25 Then player.y := 25;
   If player.y > (gameWND.Height - 28) Then player.y := (gameWND.Height - 28);


 If (amaranth = 70) And (amaranthold <> 70) Then
 begin
  bullet_level := 2;
  IDXSound_Play(weapon_upgrade_sfx,160);
 end;

 If (amaranth = 140) And (amaranthold <> 140) Then
 begin
  bullet_level := 3;
  IDXSound_Play(weapon_upgrade_sfx,160);
 end;

 If (amaranth = 210) And (amaranthold <> 210) Then
 begin
  bullet_level:= 4;
  IDXSound_Play(weapon_upgrade_sfx,160);
 end;

 If (amaranth = 280) And (amaranthold <> 270) Then
 begin
  bullet_level := 5;
  IDXSound_Play(weapon_upgrade_sfx,160);
 end;

 amaranthold:= amaranth;

 if gameWND.Mouse.bLeft or (gameWND.Keys[32]) then
 begin
  if (abs(GetTickCount - mousetimer2)>120) then
  begin
    mousetimer2:=GetTickCount;


      Case bullet_level of
          1:
          begin
              MakeBullet (player.x, player.y, 1);
              MakeBullet (player.x, player.y, 1, 172);
              MakeBullet (player.x, player.y, 1, 188);
          end;
          2:
          begin
              MakeBullet (player.x, player.y, 1, 175);
              MakeBullet (player.x, player.y, 1, 185);
              MakeBullet (player.x, player.y, 1, 165);
              MakeBullet (player.x, player.y, 1, 195);
          end;
          3:
          begin
              MakeBullet (player.x, player.y, 1);
              MakeBullet (player.x, player.y, 1, 172);
              MakeBullet (player.x, player.y, 1, 188);
              MakeBullet (player.x, player.y, 1, 165);
              MakeBullet (player.x, player.y, 1, 195);
          end;
          4:
          begin
              MakeBullet (player.x, player.y, 1, 172);
              MakeBullet (player.x, player.y, 1, 188);
              MakeBullet (player.x, player.y, 1, 172);
              MakeBullet (player.x, player.y, 1, 188);
              MakeBullet (player.x, player.y, 1, 165);
              MakeBullet (player.x, player.y, 1, 195);
          end;
          5:
          begin
              MakeBullet (player.x, player.y, 1);
              MakeBullet (player.x, player.y, 1);

              MakeBullet (player.x, player.y, 1, 172);
              MakeBullet (player.x, player.y, 1, 188);
              MakeBullet (player.x, player.y, 1, 172);
              MakeBullet (player.x, player.y, 1, 188);

              MakeBullet (player.x, player.y, 1, 165);
              MakeBullet (player.x, player.y, 1, 195);
          end;
      End;
  end;

  if (abs(GetTickCount - mousetimer)>80) then
  begin
    mousetimer:=GetTickCount;

    IDXSound_Play(bullet_sfx,100,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));

      Case bullet_level of
          1:
          begin
               MakeBullet (player.x - 10, player.y - 22, 0);
               MakeBullet (player.x + 10, player.y - 22, 0);

          end;
          2:
          begin
                MakeBullet (player.x - 10, player.y - 22, 0);
                MakeBullet (player.x + 10, player.y - 22, 0);
                MakeBullet (player.x, player.y - 27, 0);
          end;
          3:
          begin
              MakeBullet (player.x - 10, player.y - 22, 0);
              MakeBullet (player.x + 10, player.y - 22, 0);
              MakeBullet (player.x - 3.5, player.y - 27, 0);
              MakeBullet (player.x + 3.5, player.y - 27, 0);
          end;
          4:
          begin
              MakeBullet (player.x - 7, player.y - 22, 0);
              MakeBullet (player.x + 7, player.y - 22, 0);
              MakeBullet (player.x - 21, player.y - 22, 0);
              MakeBullet (player.x + 21, player.y - 22, 0);
          end;
          5:
          begin
              MakeBullet (player.x - 18, player.y - 22, 0);
              MakeBullet (player.x + 18, player.y - 22, 0);
              MakeBullet (player.x - 9, player.y - 27, 0);
              MakeBullet (player.x + 9, player.y - 27, 0);
              MakeBullet (player.x, player.y - 35, 0);
          end;
      End;
  end;
 end;

  If (amaranth > 350) And (boosinit = False)  Then
  begin
   MakeBoss(level);
   boosinit:=True;
  end;


  If (abs(GetTickCount - traintimer) >= 100) Then
  begin
     traintimer:= GetTickCount;
     for i:= 1 To IDXUtils_randomInt(0, 5) do
     begin
         MakeParticle(player.x + IDXUtils_randomInt(-5, 5),
                       player.y + 50,
                       IDXUtils_randomFloat(0.2, 0.8),
                       IDXUtils_randomInt(0, 359),
                       100,
                       $FFFFFF);
     end;

  end;


 if (amaranth <= 350) Then
 If (abs(GetTickCount - espawntimer) >= 1800) Then
 begin
            espawntimer:= GetTickCount;

            enemyai:= IDXUtils_randomInt(0, 5);
            enemyseed:=IDXUtils_randomInt(0, 10000);

          Case level of
            1:
            begin
                    If (enemyai = 0) Or (enemyai = 3) Then enemypic := 0;
                    If (enemyai = 2) Or (enemyai = 4) Then enemypic := 1;
                    If (enemyai = 1) Or (enemyai = 5) Then enemypic := 2;
            end;
            2:
            begin
                    If (enemyai = 0) Or (enemyai = 3) Then enemypic := 3;
                    If (enemyai = 2) Or (enemyai = 4) Then enemypic := 4;
                    If (enemyai = 1) Or (enemyai = 5) Then enemypic := 5;
            end;
          end;

        for i:= 1 To IDXUtils_randomInt(2, 5) do
         MakeEnemy(Trunc((bullet_level * 0.85) * 125), enemypic, enemyai, bullet_level - 1, enemyseed, i);

 end;

  scrolling:=scrolling + scrollingspeed * deltaTime;

  If scrolling > 600 Then scrolling:=scrolling - 600;

  If player.health > 100 Then player.health:= 100;

  for i:=0 To EBulletMax do ebullet[i].Update;

  for i:=0 To BulletMax do bullet[i].Update;

  For i:=0 To EnemyMax do enemy[i].Update;

  Boss.Update;

  for i:=0 to StarsMax do stars[i].Update;

  for i:=0 to PowerupMax do powerup[i].Update;

  for i:=0 To ParticleMax do particle[i].Update;

  CheckEnemyCollision;

 If bossdead = 100 Then
 begin
    Inc(level);
    if (level > 2) then
     gamestate:=gmsGameFin
    else
     gamestate:=gmsNextLevel;
 End;

  If player.health <= 0 Then
  begin


     For i:= 1 To 180 do
     begin
       IDXSound_Play(player_die_sfx,170,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));

          MakeParticle(player.x + IDXUtils_randomInt(-10, 10),
                       player.y + IDXUtils_randomInt(-10, 10),
                       IDXUtils_randomFloat(1.5, 3.0),
                       IDXUtils_randomInt(0, 359),
                       2100,
                       Rgb(IDXUtils_randomInt(200, 255), IDXUtils_randomInt(70, 150), IDXUtils_randomInt(0, 20)));
     end;
      Dec(player.lives);
      player.health:= 100;
  end;

  If player.lives = 0 Then gamestate:=gmsPlayerDead;

end;


procedure TOBullet.Update;
begin
  if alive =False then Exit;

  y:=y + (Cos(angle * Pi_RAD) * 18) * deltaTime;
  x:=x + (Sin(angle * Pi_RAD) * 18) * deltaTime;

  If y < -16 Then alive:= False;

end;


procedure TOBullet.Draw;
begin

if alive=False then Exit;

  if level=1 then
  begin
    if btype=0 then
    begin
     IDXENG_Draw2d_TextureA(x,
                           y,
                        EAanim[cgfx_bullet].slide[1].w,
                        EAanim[cgfx_bullet].slide[1].h,
                        TestGFX,
                        EAanim[cgfx_bullet].slide[1],
                        0,
                        True
                        );

   // IDXENG_Draw2d_Rectangle(x - 2, y - 17, 8, 31);
    end;
    if btype=1 then
    begin
     IDXENG_Draw2d_TextureA(x,
                           y,
                        EAanim[cgfx_bullet].slide[2].w*0.5,
                        EAanim[cgfx_bullet].slide[2].h*0.5,
                        TestGFX,
                        EAanim[cgfx_bullet].slide[2],
                        0,
                        True
                        );



//      IDXENG_Draw2d_Rectangle(x - 15, y - 5, 32, 11);
    end;
  end
  else
  if level=2 then
  begin
    if btype=0 then
    begin
          IDXENG_Draw2d_TextureA(x,
                           y,
                        EAanim[cgfx_bullet].slide[3].w*0.5,
                        EAanim[cgfx_bullet].slide[3].h*0.5,
                        TestGFX,
                        EAanim[cgfx_bullet].slide[3],
                        0,
                        True
                        );

//IDXENG_Draw2d_Rectangle(x - 1, y - 17, 5, 31);
    end;
    if btype=1 then
    begin
          IDXENG_Draw2d_TextureA(x,
                           y,
                        EAanim[cgfx_bullet].slide[4].w*0.9,
                        EAanim[cgfx_bullet].slide[4].h*0.75,
                        TestGFX,
                        EAanim[cgfx_bullet].slide[4],
                        0,
                        True
                        );

// IDXENG_Draw2d_Rectangle(x - 15, y - 5, 32, 11);
    end;
  end;
end;


procedure MakeBullet(x:Single; y:Single; mytype:Integer; angle:Single = 180);
var
  id:Integer;
begin
  for id:= 0 to BulletMax do
  If bullet[id].alive = False Then
  begin
      bullet[id].alive:= True;
      bullet[id].x := x;
      bullet[id].y := y;
      bullet[id].angle := angle;
      bullet[id].btype := mytype;
      Break;
  end;
end;


procedure TOEBullet.Update;
begin
 if alive =False then Exit;

 x:=x + (Cos(angle * Pi_RAD) * (8 + ((bullet_level - 1) / 4))) * deltaTime;
 y:=y + (Sin(angle * Pi_RAD) * (8 + ((bullet_level - 1) / 4))) * deltaTime;

 If (x < -20) Or (x > (gameWND.Width + 20)) Or (y < -20) Or (y > (gameWND.Height + 20)) Then alive:=False;

end;

procedure TOEBullet.Draw;
begin
  if alive =False then Exit;

  glEnable(GL_BLEND);

  if level=1 then
  begin
    IDXENG_Draw2d_TextureA(x,
                           y,
                        EAanim[cgfx_particle].slide[0].w*0.55,
                        EAanim[cgfx_particle].slide[0].h*0.55,
                        TestGFX,
                        EAanim[cgfx_particle].slide[0],
                        0,
                        True,
                        RGB(255, 200, 0));
  end;
  if level=2 then
  begin
    IDXENG_Draw2d_TextureA(x,
                           y,
                        EAanim[cgfx_bullet].slide[0].w*0.55,
                        EAanim[cgfx_bullet].slide[0].h*0.55,
                        TestGFX,
                        EAanim[cgfx_bullet].slide[0],
                        0,
                        True,
                        RGB(255, 200, 0));
  end;

//  IDXENG_Draw2d_Rectangle(x - 6, y - 6, 12, 12);


end;

procedure MakeEBullet(x:Single; y:Single; angle:Single);
var
  id:Integer;
begin
    For id:= 0 to EBulletMax do
        If ebullet[id].alive=False  Then
        begin
            ebullet[id].alive:=True;
            ebullet[id].x := x;
            ebullet[id].y := y;
            ebullet[id].angle := angle;
            IDXSound_Play(enemy_bullet_sfx,130,Trunc((x / (gameWND.Width * 2) * 255 + 64)));
            break;
        end;
end;


procedure TOPowerup.Update;
begin
 if alive=False then Exit;

 If (fadeout < 255) Then
 begin
   state:=0;
   y:=y + ScrollingSpeed * deltaTime;
   fadeout:=fadeout - 8 * deltaTime;
 end;

 If fadeout = 255 then
 begin
  state:=1;
  tmpsin:=tmpsin + 5 * deltaTime;

  If tmpsin > 360 Then tmpsin:=tmpsin - 360;

  x:= initialx + (Cos(tmpsin * Pi_RAD) * 15);
  y:=y + ScrollingSpeed * deltaTime;

  If (x < -20) Or (x > (gameWND.Width + 20)) Or (y < -20) Or (y > (gameWND.Height + 20)) Then alive:=False;

  If IDXUtils_RectCircleOverlap(x - 32, y - 32, 64, 64, player.x + 1, player.y + 2, 24) Then
  begin
    IDXSound_Play(get_powerup_sfx,120,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));

      Inc(amaranth);
      player.health:=player.health + 2;

      fadeout:=fadeout -1 * deltaTime;
  end;
 end;

 If fadeout <= 0 Then alive:=False;


end;


procedure TOPowerup.Draw;
var
  a:Integer;
begin
if alive =False then Exit;
 glEnable(GL_BLEND);
 if state=1 then
 begin
  a:=255;
 end
 else
 begin
  a:=Trunc(fadeout);
 end;

 IDXENG_Draw2d_TextureA(x,
                        y,
                        EAanim[cgfx_particle].slide[0].w*2.4,
                        EAanim[cgfx_particle].slide[0].h*2.4,
                        TestGFX,
                        EAanim[cgfx_particle].slide[0],
                        0,True,RGB(229,43,80),a);
end;

procedure MakePowerup(x:Single; y:Single; ptype:Integer);
var
  id:Integer;
begin
    For id := 0 To PowerupMax do
    If powerup[id].alive= False Then
    begin
        powerup[id].alive:= True;
        powerup[id].x := x;
        powerup[id].y := y;
        powerup[id].ptype := ptype;
        powerup[id].initialx := x;
        powerup[id].fadeout := 255;
        powerup[id].state:=1;
        break;
    end;

end;


procedure TOParticle.Update;
begin
 if alive =False then Exit;

  angle:=angle + IDXUtils_randomInt(-20, 20);

  y:=y + (Cos(angle * Pi_RAD) * IDXUtils_randomInt(1, 3)) * deltaTime;
  x:=x + (Sin(angle * Pi_RAD) * IDXUtils_randomInt(1, 3)) * deltaTime;

  energy:=energy - IDXUtils_randomInt(10, 15) * deltaTime;

  If energy < 0 Then energy:= 0;

  If (fromboss = 1) And (energy <= 0) Then Inc(bossdead);

  If energy <= 0 Then alive:=False;

end;


procedure TOParticle.Draw;
begin
if alive =False then Exit;
 IDXENG_Draw2d_TextureA(x,
                        y,
                        EAanim[cgfx_particle].slide[0].w*size,
                        EAanim[cgfx_particle].slide[0].h*size,
                        TestGFX,
                        EAanim[cgfx_particle].slide[0],
                        0,True,
                        mycolor,
                        trunc((energy / maxenergy) * 255)
                        );

end;


procedure MakeParticle(x,y:Single; size:Single; angle:Single;
                       maxenergy:Integer; mycolor:Cardinal; fromboss:Integer = 0);
var
  id:Integer;
begin
    For id:= 0 To ParticleMax  do
        If particle[id].alive= False Then
        begin
            particle[id].alive:= True;
            particle[id].x := x;
            particle[id].y := y;
            particle[id].size := size;
            particle[id].angle := angle;
            particle[id].energy := maxenergy;
            particle[id].maxenergy := maxenergy;
            particle[id].mycolor := mycolor;
            particle[id].fromboss := fromboss;
            Break;
        end;
end;

procedure TOEnemy.GenerateDistPos;
var
  dx,dy:Integer;
begin
 Case ai of
    0:
    begin
     dx:= IDXUtils_randomInt(20, Trunc(gameWND.Width / 4));
     epos[1].X:= dx;
     epos[0].X:= dx;
     epos[3].X:= gameWND.Width - dx;
     epos[2].X:= gameWND.Width - dx;

     dy:= IDXUtils_randomInt(200, gameWND.Height);
     epos[1].Y:= -100;
     epos[0].Y:= dy;
     epos[3].Y:= dy;
     epos[2].Y:= -100;

    end;
    1:
    begin
      dx:= IDXUtils_randomInt(20, gameWND.Width - 20);
      epos[1].X:= dx;
      epos[0].X:= dx;
      epos[3].X:= dx;
      epos[2].X:= dx;

      epos[1].Y:= -100;
      epos[0].Y:= 167;
      epos[3].Y:= 434;
      epos[2].Y:= 700;

    end;
    2:
    begin
      epos[1].X:= -100;
      epos[0].X:= 233;
      epos[3].X:= 566;
      epos[2].X:= 900;

      dy:= IDXUtils_randomInt(20, gameWND.Height - 250);
      epos[1].Y:= dy;
      epos[0].Y:= dy;
      epos[3].Y:= dy;
      epos[2].Y:= dy;

    end;
    3:
    begin
     dx:= IDXUtils_randomInt(20, Trunc(gameWND.Width / 4));
     epos[1].X:= gameWND.Width - dx;
     epos[0].X:= gameWND.Width - dx;
     epos[3].X:= dx;
     epos[2].X:= dx;

     dy:= IDXUtils_randomInt(200, gameWND.Height);
     epos[1].Y:= -100;
     epos[0].Y:= dy;
     epos[3].Y:= dy;
     epos[2].Y:= -100;
    end;
    4:
    begin
     epos[1].X:= 900;
     epos[0].X:= 566;
     epos[3].X:= 233;
     epos[2].X:= -100;

     dy:= IDXUtils_randomInt(20, gameWND.Height - 250);
     epos[1].Y:= dy;
     epos[0].Y:= dy;
     epos[3].Y:= dy;
     epos[2].Y:= dy;
    end;
    5:
    begin
     epos[1].X:= IDXUtils_randomInt(20, gameWND.Width - 20);
     epos[0].X:= IDXUtils_randomInt(100, gameWND.Width - 100);
     epos[3].X:= IDXUtils_randomInt(100, gameWND.Width - 100);
     epos[2].X:= IDXUtils_randomInt(20, gameWND.Width - 20);

     epos[1].Y:= -100;
     epos[0].Y:= IDXUtils_randomInt(100, gameWND.Height - 100);
     epos[3].Y:= IDXUtils_randomInt(100, gameWND.Height - 100);
     epos[2].Y:= gameWND.Height + 100;

    End;
 end;


end;

procedure TOEnemy.Update;
var
  i,chance:Integer;
  dx,dy,angle:Single;
begin
if alive =False then Exit;

  y:=y + scrollingspeed * deltaTime;
  t:=t + 0.005 * deltaTime;

  t2:= t - (groupnumber * 0.1);

    x:=IDXUtils_Bezier(epos[0].x, epos[1].x, epos[2].x, epos[3].x, t2);
    y:=IDXUtils_Bezier(epos[0].y, epos[1].y, epos[2].y, epos[3].y, t2);

        If health <= 0 Then
        begin
          IDXSound_Play(enemy_die_sfx,120,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));
            MakePowerup(x, y, 0);
            score:=score + 10;
            for i:= 1 to 50 do
                MakeParticle(x + IDXUtils_randomInt(-10, 10), y + IDXUtils_randomInt(-10, 10), IDXUtils_randomFloat(0.6, 1.2), IDXUtils_randomInt(0, 359), 255, Rgb(IDXUtils_randomInt(200, 255), IDXUtils_randomInt(70, 150), IDXUtils_randomInt(0, 20)));

            alive:=False;
        end;
//
        If (x > 0) And (x < gameWND.Width) And (y > 0) And (y < gameWND.Height) Then
        begin
            If bullet_level = 1 Then chance:= Trunc(65.0 * (1 / deltaTime));
            If bullet_level = 2 Then chance:= Trunc(60.0 * (1 / deltaTime));
            If bullet_level = 3 Then chance:= Trunc(55.0 * (1 / deltaTime));
            If bullet_level = 4 Then chance:= Trunc(50.0 * (1 / deltaTime));
            If bullet_level = 5 Then chance:= Trunc(35.0 * (1 / deltaTime));

            If IDXUtils_randomInt(0, chance) = 0 Then
            begin
                dx:= (player.x + IDXUtils_randomInt(-40, 40)) - x;
                dy:= (player.y + IDXUtils_randomInt(-40, 40)) - y;
                angle := (arctan2(dy, dx) * Pi_DEG);
                MakeEBullet(x, y, angle);
            End;
        end;

        if (GetTickCount-Anim_Tmr)>=Anim_Delay then
        begin
          Anim_Tmr:=GetTickCount;
          Inc(Anim_Num);
          if  Anim_Num>High(EAanim[image].slide) then Anim_Num:=0;
        end;

 If (t2 > 1.9) Or (health <= 0) Then alive:=False;

end;

procedure TOEnemy.DrawShadow;
begin
if alive =False then Exit;

end;

procedure TOEnemy.Draw;
var
  ta:Integer;
  sa:Integer;
begin
if alive =False then Exit;

glEnable(GL_BLEND);
 IDXENG_Draw2d_TextureA(x,
                        y,
                        EAanim[image].slide[Anim_Num].w,
                        EAanim[image].slide[Anim_Num].h,
                        TestGFX,
                        EAanim[image].slide[Anim_Num],
                        0,
                        True);
end;

procedure MakeEnemy(health:Integer; image:Integer; ai:Integer;
                    weapon:Integer; seed:Integer; groupnumber:Integer);
var
  id:Integer;
begin
    For id:= 0 To EnemyMax do
        If enemy[id].alive= False Then
        begin
            enemy[id].x := -100;
            enemy[id].y := -100;
            enemy[id].alive:= True;
            enemy[id].health := health;
            enemy[id].ai := ai;
            enemy[id].weapon := weapon;
            enemy[id].seed := seed;
            enemy[id].groupnumber := groupnumber;
            enemy[id].t := 0;
            enemy[id].GenerateDistPos;

            enemy[id].Anim_Delay:=400;
            enemy[id].Anim_Tmr:=GetTickCount;
            enemy[id].Anim_Num:=0;

            case image of
             0: enemy[id].image:=cgfx_ufo;
             1: enemy[id].image:=cgfx_star;
             2: enemy[id].image:=cgfx_sliz;
             3: enemy[id].image:=cgfx_glaz;
             4: enemy[id].image:=cgfx_elka;
             5: enemy[id].image:=cgfx_olen;
             6: enemy[id].image:=cgfx_snegovik;
            end;

            Break;
        end;

end;


procedure CheckEnemyCollision;
var
  i,j:Integer;
  e1x, e1y, e2x, e2y:Single;
  b1x, b1y, b2x, b2y:Single;
begin
    For j:= 0 To EnemyMax do
    begin
        If enemy[j].alive Then
        begin

            Case enemy[j].image of
                cgfx_ufo:
                begin
                    e1x := enemy[j].x - 27;
                    e1y := enemy[j].y - 16;
                    e2x := 56;
                    e2y := 34;
                end;
                cgfx_star:
                begin
                    e1x := enemy[j].x - 30;
                    e1y := enemy[j].y - 10;
                    e2x := 62;
                    e2y := 26;
                end;
                cgfx_sliz:
                begin
                    e1x := enemy[j].x - 24;
                    e1y := enemy[j].y - 15;
                    e2x := 47;
                    e2y := 28;
                end;
                cgfx_glaz, cgfx_elka:
                begin
                    e1x := enemy[j].x - 27;
                    e1y := enemy[j].y - 16;
                    e2x := 56;
                    e2y := 34;
                end;
                cgfx_olen:
                begin
                    e1x := enemy[j].x - 24;
                    e1y := enemy[j].y - 24;
                    e2x := 47;
                    e2y := 46;
                end;

            End;

            For i:= 0 To BulletMax do
            begin
                If bullet[i].alive Then
                begin

                  if bullet[i].btype=0 then
                  begin
                     if bullet_level=3 then
                     begin
                        b1x := bullet[i].x - 1;
                        b1y := bullet[i].y - 17;
                        b2x := 5;
                        b2y := 31;
                     end
                     else
                     begin
                      b1x := bullet[i].x - 2;
                      b1y := bullet[i].y - 17;
                      b2x := 8;
                      b2y := 31;
                     end;
                  end
                  else
                  if bullet[i].btype=1 then
                  begin
                    b1x := bullet[i].x - 15;
                    b1y := bullet[i].y - 5;
                    b2x := 32;
                    b2y := 11;
                  end;

                    If IDXUtils_RectsOverlap(b1x, b1y, b2x, b2y, e1x, e1y, e2x, e2y) Then
                    begin
                        bullet[i].alive:= False;

                        if bullet[i].btype=0 then
                        begin
                          If bullet_level <= 3 Then Dec(enemy[j].health,20) else Dec(enemy[j].health,25);
                        end
                        else
                        if bullet[i].btype=1 then
                        begin
                           Dec(enemy[j].health,15);
                        end;

                    end;
                end;
            end;

            If IDXUtils_RectCircleOverlap(e1x, e1y, e2x, e2y, player.x + 1, player.y + 2, 24) Then
            begin
              IDXSound_Play(player_hit_sfx,100,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));
               player.health:=player.health-6;
               Dec(enemy[j].health,15);
            end;

        end;
    end;

    For i:= 0 To EBulletMax do
    begin
        If ebullet[i].alive Then
        begin
             b1x:= ebullet[i].x - 6;
             b1y:= ebullet[i].y - 6;
             b2x:= 12;
             b2y:= 12;

            If IDXUtils_RectCircleOverlap(b1x, b1y, b2x, b2y, player.x + 1, player.y + 2, 24) Then
            begin
              IDXSound_Play(player_hit_sfx,100,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));
                player.health:=player.health-6;
                ebullet[i].alive:= False;
            end;
        end;
    end;
end;


procedure TOBoss.Update;
var
  dx,dy,ang:Single;
  e1x, e1y, e2x, e2y:Single;
  i:Integer;
  b1x, b1y, b2x, b2y:Single;
  aspr:Integer;
begin
  if alive=False then Exit;

 if btype=1 then aspr:=cgfx_boos1;
 if btype=2 then aspr:=cgfx_boos2;

 if Abs(GetTickCount-Anim_Tmr)>=Anim_Delay then
 begin
   Anim_Tmr:=GetTickCount;
   Inc(Anim_Num);
   if  Anim_Num>High(EABanim[aspr].slide) then Anim_Num:=0;
 end;


 if btype=1 then
 begin
   x:= initialx + Sin(angle * Pi_RAD) * 150;
 end;

 if btype=2 then
 begin
   x:= initialx + Sin(angle * Pi_RAD) * 150;
   y:= (initialy + Sin(angle * Pi_RAD) * 80) + 50;
 end;

 angle:=angle + 2 * deltatime;
 If angle > 360 Then angle:=angle - 360;

 If fadein > 255 Then
 begin
   If IDXUtils_randomInt(0, trunc(4.0 * (1 / deltatime))) = 0 Then
   begin
      dx:= (player.x + IDXUtils_randomInt(-200, 200)) - x;
      dy:= (player.y + IDXUtils_randomInt(-80, 80)) - y;
      ang:= (Arctan2(dy, dx) * Pi_DEG);
      MakeEBullet(x, y, ang);
   End;

    Case btype of
     1:
     begin
            e1x:= x - 170;
            e1y:= y - 110;
            e2x:= 352;
            e2y:= 230;
     end;
     2:
     begin
            e1x:= x - 186;
            e1y:= y - 110;
            e2x:= 384;
            e2y:= 230;
     end;
    end;


    For i:= 0 to BulletMax do
    begin
                If bullet[i].alive Then
                begin

                    if bullet[i].btype=0 then
                    begin
                        if bullet_level=3 then
                        begin
                          b1x:= bullet[i].x - 1;
                          b1y:= bullet[i].y - 17;
                          b2x:= 5;
                          b2y:= 31;
                        end
                        Else
                        begin
                          b1x := bullet[i].x - 2;
                          b1y := bullet[i].y - 17;
                          b2x := 8;
                          b2y := 31;
                        End;
                    end;
                    if bullet[i].btype=1 then
                    begin
                        b1x := bullet[i].x - 15;
                        b1y := bullet[i].y - 5;
                        b2x := 32;
                        b2y := 11;
                    end;

                    If IDXUtils_RectsOverlap(b1x, b1y, b2x, b2y, e1x, e1y, e2x, e2y) Then
                    begin
                        bullet[i].alive:=False;
                        MakeParticle(b1x + IDXUtils_randomInt(-10, 10),
                                     b1y + IDXUtils_randomInt(-10, 10),
                                     IDXUtils_randomFloat(0.6, 1.2),
                                     IDXUtils_randomInt(0, 359), 255,
                                     Rgb(IDXUtils_randomInt(200, 255),
                                     IDXUtils_randomInt(70, 150),
                                     IDXUtils_randomInt(0, 20)));

//                         IDXSound_Play(enemy_die_sfx,120,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));

                          if bullet[i].btype=0 then
                          begin
                           If bullet_level <= 3 Then
                            health:=health - 20
                           else
                            health:=health - 25;
                          end;

                          if bullet[i].btype=1 then
                          begin
                           health:=health - 15;
                          end;


                    end;
                end;
    end;



   If IDXUtils_RectCircleOverlap(e1x, e1y, e2x, e2y, player.x + 1, player.y + 2, 24) Then
   begin
     IDXSound_Play(player_hit_sfx,100,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));

      player.health:=player.health - 6;
      health:=health - 15;
   end;

    If health <= 0 Then
    begin
        alive:=False;

       IDXSound_Play(enemy_die_sfx,50,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));
       IDXSound_Play(enemy_die_sfx,50,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));
       IDXSound_Play(enemy_die_sfx,50,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));
       IDXSound_Play(enemy_die_sfx,50,Trunc((player.x / (gameWND.Width * 2) * 255 + 64)));

        For i:= 1 To 100 do
        begin
            MakeParticle(x + IDXUtils_randomInt(-100, 100),
                         y + IDXUtils_randomInt(-100, 100),
                         IDXUtils_randomFloat(4.0, 7.0),
                         IDXUtils_randomInt(0, 359), 2100,
                         Rgb(IDXUtils_randomInt(200, 255),
                             IDXUtils_randomInt(70, 150),
                             IDXUtils_randomInt(0, 20)), 1);
        end;
    End;
 end
 else
 begin
   fadein:= fadein + 1 * deltatime;
 end;


end;

procedure TOBoss.Draw;
var
  tmpW,tmpH:Integer;
  lw:Single;
begin
if alive=False then Exit;


 tmpW:= EABanim[cgfx_boos1].slide[Anim_Num].w;
 tmpH:= EABanim[cgfx_boos1].slide[Anim_Num].h;

 lw:=(tmpW/maxhealth);



 If fadein > 255 Then
 begin

  IDXENG_Draw2d_Rectangle(x-(tmpW div 2),
                         y-(tmpH div 2)-16,
                         lw*health,
                         6,$000000F9,200,0,True);

  IDXENG_Draw2d_Rectangle(x-(tmpW div 2),
                        y-(tmpH div 2)-16,
                        tmpW,
                        6,$FFFFFF,200);

    Case btype of
     1:
     begin
        IDXENG_Draw2d_TextureA(x,
                        y,
                        EABanim[cgfx_boos1].slide[Anim_Num].w,
                        EABanim[cgfx_boos1].slide[Anim_Num].h,
                        TestGFX2,
                        EABanim[cgfx_boos1].slide[Anim_Num],
                        0,
                        True);

     end;
     2:
     begin

       IDXENG_Draw2d_TextureA(x,
                        y,
                        EABanim[cgfx_boos2].slide[Anim_Num].w,
                        EABanim[cgfx_boos2].slide[Anim_Num].h,
                        TestGFX2,
                        EABanim[cgfx_boos2].slide[Anim_Num],
                        0,
                        True);
     End;
    end;

 end
 else
 begin

   IDXENG_Draw2d_Rectangle(x-(tmpW div 2),
                           y-(tmpH div 2)-16,
                           lw*health,
                           6,$000000F9,Trunc(fadein),0,True);

   IDXENG_Draw2d_Rectangle(x-(tmpW div 2),
                          y-(tmpH div 2)-16,
                          tmpW,
                          6,$FFFFFF,Trunc(fadein));

  Case btype of
   1:
   begin
     IDXENG_Draw2d_TextureA(x,
                        y,
                        EABanim[cgfx_boos1].slide[Anim_Num].w,
                        EABanim[cgfx_boos1].slide[Anim_Num].h,
                        TestGFX2,
                        EABanim[cgfx_boos1].slide[Anim_Num],
                        0,
                        True,
                        $FFFFFF,
                        Trunc(fadein));

//     IDXENG_Draw2d_Rectangle(x - 170, y - 110, 352, 230);
   end;
   2:
   begin
     IDXENG_Draw2d_TextureA(x,
                        y,
                        EABanim[cgfx_boos2].slide[Anim_Num].w,
                        EABanim[cgfx_boos2].slide[Anim_Num].h,
                        TestGFX2,
                        EABanim[cgfx_boos2].slide[Anim_Num],
                        0,
                        True,
                        $FFFFFF,
                        Trunc(fadein));

//      IDXENG_Draw2d_Rectangle(x - 186, (y + 50) - 110, 384, 230);
   End;
  end;
 end;


//  IDXENG_Draw2d_Text(MySysfont[2],IDXUtils_IntToStr(health),x,y,$CDCDCD,255);
end;



procedure MakeBoss(bosstype:Integer);
begin
    boss.alive := True;
    boss.initialx := gameWND.DWidth;
    boss.initialy := Trunc(gameWND.Height / 5);
    boss.x := gameWND.DWidth;
    boss.y := Trunc(gameWND.Height / 5);
    boss.angle := 0;
    boss.btype := bosstype;
    If bosstype = 1 Then boss.maxhealth := 50000;
    If bosstype = 2 Then boss.maxhealth := 75000;

    boss.health:=boss.maxhealth;

    boss.fadein := 0;

    boss.Anim_Delay:=400;
    boss.Anim_Tmr:=GetTickCount;
    boss.Anim_Num:=0;

end;


procedure TOStars.Update;
begin
 y:=y + ScrollingSpeed * deltaTime * level;

 If y > gameWND.Height Then
 begin
  x := IDXUtils_randomInt(0, gameWND.Width);
  y := 0;
  alpha := IDXUtils_randomInt(0, 255);
  level := IDXUtils_randomFloat(1, 1.5);
  size := IDXUtils_randomFloat(0.2, 0.5);
 end;

end;

procedure TOStars.Draw;
begin
 IDXENG_Draw2d_Pixel(x,y,$FFFFFF);
end;


procedure MakeStars;
var
  id:Integer;
begin
    For id:= 0 To StarsMax do
    begin
        stars[id].x := IDXUtils_randomInt(0, gameWND.Width);
        stars[id].y := IDXUtils_randomInt(0, gameWND.Height);
        stars[id].alpha := IDXUtils_randomInt(0, 255);
        stars[id].level := IDXUtils_randomFloat(1, 1.5);
        stars[id].size := IDXUtils_randomFloat(0.2, 0.5);
    end;
end;



end.
