unit uEnemy;

interface

uses zglHeader,
     uVar,
     uAdd;

procedure EnRender;
procedure EnUpdate;

implementation

procedure EnRender;
var i,j: integer;
begin
  for I := 1 to 256 do
    if enemies[i].exist then
       begin
         case enemies[i].eType of
           0: ASprite2d_draw(texEn1, enemies[i].pos.X, enemies[i].pos.Y, 48, 96, enemies[i].ang + 90, enemies[i].frame);
           1: ASprite2d_draw(texEn2, enemies[i].pos.X, enemies[i].pos.Y, 48, 96, enemies[i].ang + 90, enemies[i].frame);
         end;
         if debugOn then DrawORect(ORect(pt(enemies[i].pos.x + 24, enemies[i].pos.Y + 48), 24, 48, enemies[i].ang + 90));
       end;
end;

procedure EnUpdate;
var i, j, k: integer;  d, d1: single;
begin
  inc(fCounter, 2);
  if fCounter > 10 then fCounter := 0;  
  for I := 1 to 256 do
    if enemies[i].exist then
       case enemies[i].eType of
         0: begin
              if fCounter = 8 then inc(enemies[i].frame);
              if enemies[i].frame > 16 then enemies[i].frame := 1;
              d1 := 100000;
              for j := 1 to 3 do
                 begin
                  d := m_Distance(team.members[1].pos.x,
                                  team.members[1].pos.y,
                                  enemies[i].pos.X,
                                  enemies[i].pos.Y );
                  if d < d1 then
                    begin
                       d1 := d;
                       k  := j;
                    end;
                 end;

              enemies[i].fang := m_Angle(team.members[k].pos.x,
                                        team.members[k].pos.y,
                                        enemies[i].pos.X,
                                        enemies[i].pos.Y);

              if enemies[i].fang > enemies[i].ang then enemies[i].ang:=enemies[i].ang + 0.5 else  enemies[i].ang:=enemies[i].ang - 0.5;
            //  if(enemies[i].fang - enemies[i].ang) < 0 then enemies[i].ang:=enemies[i].ang + 0.2;
              
              enemies[i].pos.X := enemies[i].pos.X + 0.35 * m_Cos(round(enemies[i].ang));
              enemies[i].pos.Y := enemies[i].pos.Y + 0.35 * m_Sin(round(enemies[i].ang));
            end;
         1: begin
              if fCounter = 8 then inc(enemies[i].frame);
              if enemies[i].frame > 16 then enemies[i].frame := 1;
              d1 := 100000;
              for j := 1 to 3 do
                 begin
                  d := m_Distance(team.members[1].pos.x,
                                  team.members[1].pos.y,
                                  enemies[i].pos.X,
                                  enemies[i].pos.Y );
                  if d < d1 then
                    begin
                       d1 := d;
                       k  := j;
                    end;
                 end;

              enemies[i].fang := m_Angle(team.members[k].pos.x,
                                        team.members[k].pos.y,
                                        enemies[i].pos.X,
                                        enemies[i].pos.Y);

              if enemies[i].fang > enemies[i].ang then enemies[i].ang:=enemies[i].ang + 1 else  enemies[i].ang:=enemies[i].ang - 1;
            //  if(enemies[i].fang - enemies[i].ang) < 0 then enemies[i].ang:=enemies[i].ang + 0.2;
              
              enemies[i].pos.X := enemies[i].pos.X + 2.35 * m_Cos(round(enemies[i].ang));
              enemies[i].pos.Y := enemies[i].pos.Y + 2.35 * m_Sin(round(enemies[i].ang));
            end;
       end;


  if a/ 100 = a div 100 then
     for j := 1 to random(5 + a div 500) do
       for  i:= 1 to 256 do
          if not enemies[i].exist then
            begin
              enemies[i].exist := true;
              enemies[i].pos.x := 400 + 500 * m_cos( i * 35 );
              enemies[i].pos.y := 300 + 500 * m_sin( i * 35 );
              enemies[i].HP := 10;
              enemies[i].eType := random(200);
              if enemies[i].eType > 100 then enemies[i].eType := 1 else enemies[i].eType := 0;
              break;
            end;

end;

end.
