unit tenemy;


interface
uses
  DGLEngine_header,windows,Math,SysUtils;
  type
      en=record
          health,typ,vector,v,angle,cash,yb,n:longint;
          x,y,speedX,speedY,stop,speed,time,timemax:extended;

      end;
      Genemy=class
                  tex:array[0..10]of cardinal;
                  n,shag:longint;
                  a:array[0..1000]of en;
                  procedure load_tex;
                  procedure e_create(level:longint;xb,yb:longint);
                  procedure e_draw(k:longint);
                  procedure e_move;
                  procedure destr(i:longint);

       end;
var n:longint;
implementation
uses unit2;

{
  RIGHT=1
  LEFT=2
  UP=3
  DOWN=4
}
procedure genemy.destr;
var
 j:longint;
 begin
      if  a[i].health<=0 then
       begin
            for j:=1 to npat do
             if pat[j].t=i then pat[j].x:=0;

            for j:=i+1 to n do
              a[j-1]:=a[j];
              if unit2.yb=0 then
              money:=money+a[i].cash else
              unit2.yb:=0;
              n:=n-1;
       end;

 end;

procedure genemy.load_tex;
 var
    i:longint;
 begin
      for i:=1 to 5 do
      tex[i]:=loadtexturefromfile('data\image\enemy'+inttostr(i)+'.bmp',texdetail_best,transcolor_black);
 end;

procedure genemy.e_create;
var
   i:longint;
begin
  n:=lvl[level].n;
 for i:=1 to lvl[level].n do
           with a[i] do
            begin
            n:=lvl[level].n;
            cash:=lvl[level].cash;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=lvl[level].helth;
                 typ:=lvl[level].typ;
                 vector:=1;
                 speedX:=1;
                 speed:=lvl[level].speed;
            end;
     {if level=1 then
      begin
            n:=8;
           for i:=1 to n do
           with a[i] do
            begin
             n:=5;
            cash:=1;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=3;
                 typ:=2;
                 vector:=1;
                 speedX:=1;
                 speed:=1;
                 speedY:=0;
            end;
      end;
       if level=2 then
      begin
          n:=8;
           for i:=1 to n do
           with a[i] do
            begin
             n:=8;
             cash:=1;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=5;
                 typ:=2;
                 vector:=1;
                 speedX:=1;
                 speed:=1;
                 speedY:=0;
                 v:=1;
            end;
      end;
           if level=3 then
      begin
          n:=8;
           for i:=1 to n do
           with a[i] do
            begin
             n:=8;
            cash:=1;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=13;
                 typ:=2;
                 vector:=1;
                 speedX:=1;
                 speed:=1.2;
                 speedY:=0;
            end;
      end;
           if level=4 then
      begin
            n:=20;
           for i:=1 to n do
           with a[i] do
            begin
               n:=12;
            cash:=2;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=17;
                 typ:=2;
                 vector:=1;
                 speedX:=1;
                 speed:=1.4;
                 speedY:=0;
            end;
      end;
           if level=5 then
      begin

           for i:=1 to n do
           with a[i] do
            begin
              n:=20;
            cash:=2;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=25;
                 typ:=2;
                 vector:=1;
                 speedX:=1;
                 speed:=1.6;
                 speedY:=0;
            end;
      end;
          if level=6 then
      begin
            n:=1;
           for i:=1 to n do
           with a[i] do
            begin
              n:=1;
            cash:=30;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=150;
                 typ:=5;
                 vector:=1;
                 speedX:=1;
                 speed:=1.2;
                 speedY:=0;
            end;
      end;
          if level=7 then
      begin
             n:=13;
           for i:=1 to n do
           with a[i] do
            begin
            cash:=2;
               n:=13;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=38;
                 typ:=3;
                 vector:=1;
                 speedX:=1;
                 speed:=1;
                 speedY:=0;
            end;
      end;
          if level=8 then
      begin
            n:=10;
           for i:=1 to n do
           with a[i] do
            begin
               n:=10;
            cash:=3;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=55;
                 typ:=3;
                 vector:=1;
                 speedX:=1;
                 speed:=1.5;
                 speedY:=0;
            end;
      end;
         if level=9 then
      begin
              n:=6;
           for i:=1 to n do
           with a[i] do
            begin
                 n:=6;
            cash:=3;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=125;
                 typ:=3;
                 vector:=1;
                 speedX:=1;
                 speed:=1.1;
                 speedY:=0;
            end;
      end;
         if level=10 then
      begin
           n:=8;
           for i:=1 to n do
           with a[i] do
            begin
                n:=8;
            cash:=4;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=135;
                 typ:=3;
                 vector:=1;
                 speedX:=1;
                 speed:=1;
                 speedY:=0;
            end;
      end;
       if level=11 then
      begin
           n:=9;
           for i:=1 to n do
           with a[i] do
            begin
                  n:=9;
            cash:=60;
            shag:=0;
            time:=0;
                 x:=-(i-1)*40;
                 y:=80;
                 health:=150;
                 typ:=3;
                 vector:=1;
                 speedX:=1;
                 speed:=1.2;
                 speedY:=0;
            end;
      end;    }
end;

procedure genemy.e_draw;
var
   i:longint;
 begin
      for i:=1 to n do
       if (a[i].health>0)and(a[i].x>0) then
        begin
            begin2d;
            case k of
                  1:
                  begin
                   drawtexture2d(tex[a[i].typ],round(a[i].x)-unit2.xpos-40,round(a[i].y)-unit2.ypos-40,80,80,a[i].angle,255,-1);
                   drawrectangle2d(round(a[i].x)-unit2.xpos-40,round(a[i].y)-unit2.ypos-60,trunc(a[i].health/(lvl[unit2.level].helth)*70),7,trunc(a[i].health/lvl[level].helth*100000+1000),255,true);
                  end;

                  2: drawtexture2d(tex[a[i].typ],(trunc(a[i].x)div 40)*4+850,(trunc(a[i].y)div 40)*4+420,8,8,a[i].angle,255,-1);
              end;
            end2d;
       end;
 end;

procedure genemy.e_move;
var
   i:longint;
   v:longint;
 begin
 shag:=shag+1;
 if shag mod 100=0 then
  begin
       for i:=1 to n do
        if a[i].x<0 then
         begin
              a[i].x:=0;
              break;
         end;
  end;
 for i:=1 to n do
 if (a[i].health>0)and(a[i].x>=0) then
 with a[i] do
 begin
 angle:=angle+1;
  if angle>=360 then angle:=angle-360;
      if time<>0 then time:=time+1;
      if time>=timemax then time:=0;
      if vector=4 then
       begin
            if ((round(y+40)div 40+1)*40-y<=speed)and((round(y+40)div 40+1)*40-y>0)
               then y:=(round(y+40)div 40+1)*40
            else
            begin
            if unit2.a[round(y+40)div 40+1,round(x)div 40+1].s='2' then
             begin
                  y:=y+speedy*speed;
                  if time<>0 then y:=y-stop*speed/100;

                  v:=4;
             end
                else
                 begin

                        if v=2 then
                      begin
                           vector:=3;
                           speedy:=-1;
                           speedx:=0.00;
                      end
                      else
                      begin
                //           y:=(round(y+40)div 40+1)*40;
                           vector:=1;
                           speedy:=0.00;
                           speedx:=1;
                      end;
                 end;
           end;
          end;
            if vector=3 then
       begin
       if (y-(round(y-40)div 40+1)*40<=speed)and(y-(round(y-40)div 40+1)*40>0)
               then y:=(round(y)div 40)*40
               else
            begin
            if unit2.a[round(y-41)div 40+1,round(x)div 40+1].s='2' then
             begin
                 v:=3;
                  y:=y+speedy*speed;
                  if time<>0 then y:=y+stop*speed/100;
             end
                else

                 begin
                      if v=1 then
                      begin
                           vector:=4;
                           speedy:=1;
                           speedx:=0.0;
                      end
                      else
                      begin
                    //      y:=(round(y)div 40)*40;
                           vector:=2;
                           speedy:=0.0;
                           speedx:=-1;
                      end;
                 end;
           end;
          end;
        if vector=2 then
       begin
            if (x-(round(x) div 40)*40<=speed)and (x-(round(x) div 40)*40>0)
               then x:=(round(x) div 40)*40
            else
            begin
            if unit2.a[round(y)div 40+1,round(x-41) div 40+1].s='2' then
             begin
                  x:=x+speedx*speed;
                  if time<>0 then x:=x+stop*speed/100;
                  v:=2;
             end
                else
                //    x:=(round(x) div 40)*40;
                 begin
                       if v=3 then
                      begin
                           vector:=1;
                           speedy:=0.0;
                           speedx:=1;
                      end
                      else
                      begin
                           vector:=4;
                           speedy:=1;
                           speedx:=0.0;
                      end;
                 end;
           end;
         end;
       if vector=1 then
       begin
        if ((round(x) div 40+1)*40-x<=speed)and ((round(x) div 40+1)*40-x>0)
               then x:=(round(x) div 40+1)*40
            else
         begin
            if (unit2.a[round(y)div 40+1,round(x+40)div 40+1].s='2') then
             begin
                  x:=x+speedx*speed;
                  if time<>0 then
                      x:=x-stop*speed/100;
                  v:=1;
             end
                else
                 begin

                        if v=4 then
                      begin
                           vector:=2;
                           speedy:=0;
                           speedx:=-1.0;
                     end
                      else
                      begin
                  //         x:=(round(x) div 40+1)*40;
                           vector:=3;
                           speedy:=-1;
                           speedx:=0.00;
                      end;
                 end;
           end;
        end;
       end;
 end;

end.
