unit uTeam;

interface

uses zglHeader,
     uVar,
     uAdd,
     uSmplPart,
     uBlood;

procedure TeamRender;
procedure TeamUpdate;
procedure BulletUpd;
function InSector(a : single) : boolean;

implementation

function InSector(a : single) : boolean;
var a1, a2 : single;
begin
  a1 := team.members[1].ang + 180 - 35;
  a2 := team.members[1].ang + 180 + 35;

  if a1 > 359 then a1 := a1 - 360;
  if a2 > 359 then a2 := a2 - 360;

  if (a > a1) and (a < a2) then result:= true else result := false;
end;

procedure TeamRender;
var i, k: integer; d3 : single; d1 : single;
begin
  for i := 1 to 3 do
    with team.members[i] do
     SSprite2d_Draw(texMan,
                    pos.x,
                    pos.y, 64, 64,
                    ang + 90);

 for I := 1 to 128 do
   if bullets[i].exist then
   begin
     SSprite2d_draw(texBllt, bullets[i].pos.x, bullets[i].pos.Y, 32, 8, bullets[i].ang);
     if debugOn then
        DrawORect(ORect(pt(bullets[i].pos.x + 4, bullets[i].pos.Y + 1), 8, 2, bullets[i].ang));
     d1 := m_distance(bullets[i].pos.x + 3,
                      bullets[i].pos.y + 1,
                      team.members[1].pos.x + 32,
                      team.members[1].pos.y + 32);

  //   SSprite2d_Draw(texTrace, bullets[i].pos.X - (d1 + d1 * m_cos(round(bullets[i].ang)))/2 , bullets[i].pos.Y , d1, 4, bullets[i].ang);
   end;
 d3 := m_distance(mouse_x, mouse_y,
                  team.members[1].pos.x,
                  team.members[1].pos.y);
 d3 := d3/500;
 pr2d_circle(mouse_x, mouse_y, team.members[1].switch * d3, $ff0000);

 for i := 0 to team.members[1].shots - 1 do
   pr2d_Line(10 + i*2, 570, 10 + i*2, 590, $ff00ff);
end;

procedure TeamUpdate;
var d1, d2, d3 : single; index : integer; i, j, k : integer;
begin
   team.members[1].ang := m_Angle(Mouse_x, mouse_y,
                                  team.members[1].pos.x + 32,
                                  team.members[1].pos.y + 32);

      if key_down(K_W) then team.members[1].pos.Y := team.members[1].pos.Y - 2;
      if key_down(K_S) then team.members[1].pos.Y := team.members[1].pos.Y + 2;
      if key_down(K_A) then team.members[1].pos.X := team.members[1].pos.X - 2;
      if key_down(K_D) then team.members[1].pos.X := team.members[1].pos.X + 2;

   light.position.x := team.members[1].pos.x + 32;
   light.position.y := team.members[1].pos.y + 32;
   light.angle      := deg2rad * team.members[1].ang;

   light2.position.x := team.members[3].pos.x + 32;
   light2.position.y := team.members[3].pos.y + 32;
   light2.angle      := deg2rad * team.members[2].ang;

   light3.position.x := team.members[2].pos.x + 32;
   light3.position.y := team.members[2].pos.y + 32;
   light3.angle      := deg2rad * team.members[3].ang;

   l1.position := light.position;
   l2.position := light2.position;
   l3.position := light2.position;

   l1.intensity := l1.intensity - 0.1;
   l2.intensity := l2.intensity - 0.1;
   l3.intensity := l3.intensity - 0.1;

//  Перегруппировка.
//  Автоматическая, при отдалении игрока на расстояние большее чем 75 пикс
//  Принудительная при нажатии клавиши 1.

   if m_distance(team.members[1].pos.x,
                 team.members[1].pos.y,
                 team.members[2].pos.x,
                 team.members[2].pos.y) > 100 then
                   begin
                     team.members[2].move   := true;
                     team.members[2].fpos.x := team.members[1].pos.x + 50 * m_cos((round(team.members[1].ang + 110))) ;
                     team.members[2].fpos.y := team.members[1].pos.y + 50 * m_sin((round(team.members[1].ang + 110))) ;
                   end;

   if m_distance(team.members[2].pos.x,
                 team.members[2].pos.y,
                 team.members[3].pos.x,
                 team.members[3].pos.y) > 100 then
                   begin
                     team.members[3].move   := true;
                     team.members[3].fpos.x := team.members[1].pos.x + 80 * m_cos((round(team.members[1].ang + 190))) ;
                     team.members[3].fpos.y := team.members[1].pos.y + 80 * m_sin((round(team.members[1].ang + 190))) ;
                   end;

   if key_down(K_1) then
      begin
        team.members[2].move   := true;
        team.members[2].fpos.x := team.members[1].pos.x + 50 * m_cos((round(team.members[1].ang + 110)));
        team.members[2].fpos.y := team.members[1].pos.y + 50 * m_sin((round(team.members[1].ang + 110)));
        team.members[3].move   := true;
        team.members[3].fpos.x := team.members[1].pos.x + 80 * m_cos((round(team.members[1].ang + 190))) ;
        team.members[3].fpos.y := team.members[1].pos.y + 80 * m_sin((round(team.members[1].ang + 190))) ;
      end;

// собственно перемещение
   if team.members[2].move then
                 begin
                   if team.members[2].fpos.x > team.members[2].pos.x then team.members[2].pos.x := team.members[2].pos.x + 2;
                   if team.members[2].fpos.x < team.members[2].pos.x then team.members[2].pos.x := team.members[2].pos.x - 2;
                   if team.members[2].fpos.y > team.members[2].pos.y then team.members[2].pos.y := team.members[2].pos.y + 2;
                   if team.members[2].fpos.y < team.members[2].pos.y then team.members[2].pos.y := team.members[2].pos.y - 2;

                   if m_distance(team.members[2].fpos.x,
                                 team.members[2].fpos.y,
                                 team.members[2].pos.x,
                                 team.members[2].pos.y) < 1 then team.members[2].move := false;
                 end;


   if team.members[3].move then
                 begin
                   if team.members[3].fpos.x > team.members[3].pos.x then team.members[3].pos.x := team.members[3].pos.x + 2;
                   if team.members[3].fpos.x < team.members[3].pos.x then team.members[3].pos.x := team.members[3].pos.x - 2;
                   if team.members[3].fpos.y > team.members[3].pos.y then team.members[3].pos.y := team.members[3].pos.y + 2;
                   if team.members[3].fpos.y < team.members[3].pos.y then team.members[3].pos.y := team.members[3].pos.y - 2;

                   if m_distance(team.members[3].fpos.x,
                                 team.members[3].fpos.y,
                                 team.members[3].pos.x,
                                 team.members[3].pos.y) < 1 then team.members[3].move := false;
                 end;

//  АИ прекрывающего.
//  Ищет близжайшего врага в секторе и открывает огонь
//  не обращает внимания на разброс.
   d2 := 1000000;
   for i := 1 to 256 do
     if enemies[i].exist then
        begin
          d1 :=    m_distance(enemies[i].pos.x,
                              enemies[i].pos.y,
                              team.members[3].pos.x,
                              team.members[3].pos.y);
          if d1 < d2 then
             begin
               index := i;
               d2 := d1;
             end;
        end;

   team.members[3].ang := m_Angle(enemies[index].pos.X,
                                  enemies[index].pos.Y,
                                  team.members[3].pos.x + 32,
                                  team.members[3].pos.y + 32);


// СТРЕЛЬБА
// для первого игрока
  inc(team.members[1].rpm_timer);
  inc(team.members[2].rpm_timer);
  inc(team.members[3].rpm_timer);
  team.members[1].switch := team.members[1].switch - team.members[1].switch_down;
  if team.members[1].switch < team.members[1].switch_min then team.members[1].switch := team.members[1].switch_min;

  if Key_Press(K_R) then team.members[1].phase := 1;
  
// Перезарядка
  if team.members[1].phase = 1 then
    begin
      inc(team.members[1].reload_timer);
      if team.members[1].reload_timer > team.members[1].reload then
         begin
           team.members[1].phase := 0;
           team.members[1].reload_timer := 0;
           team.members[1].shots := team.members[1].max_shots;
         end;
    end;

  if mouse_down(M_BLEFT) and (team.members[1].phase <> 1) then
     if team.members[1].rpm_timer > team.members[1].rpm then
        begin
        case team.members[1].wpnID of
        1: begin
            team.members[1].rpm_timer := 0;
            l1.intensity := 1;
            team.members[1].switch := team.members[1].switch + 4;
            if team.members[1].switch > 50 then team.members[1].switch := 50;
            for I := 1 to 128 do
              if not bullets[i].exist then
                begin
                  bullets[i].exist := true;
                  dec(team.members[1].shots);
                  if team.members[1].shots = 0 then team.members[1].phase := 1;
                  log_add('bullet add in slot #'+u_IntToStr(i));
                  bullets[i].pos.X := team.members[1].pos.x + 32 + 16 * m_cos(round(team.members[1].ang));
                  bullets[i].pos.Y := team.members[1].pos.y + 32 + 16 * m_sin(round(team.members[1].ang));
                //рассчитываем угол полёта исходя из позииции
                //курсора и разброса
                // выбираем рандомный угол
                  d1 := random(360);
                // выбираем рандомный радиус из интервала
                  d2 := random(round(team.members[1].switch * 1000))/1000;
                // считаем коэффициент отдаления прицела от игрока
                // если мы целим в противника "в упор", то разброс будет минимальным
                // на дистанции он принимает рельные значения.
                // разброс равен 100% на дистанции в 500 пикселей
                  d3 := m_distance(mouse_x, mouse_y,
                                 team.members[1].pos.x,
                                 team.members[1].pos.y);
                  d3 := d3/500;
                // считаем угол
                  bullets[i].ang := m_Angle(Mouse_x + d2 * d3 * m_cos(round(d1)),
                                          mouse_y + d2 * d3 * m_sin(round(d2)),
                                          bullets[i].pos.X,
                                          bullets[i].pos.Y);
                  bullets[i].spd := 10;
                  bullets[i].dmg := 3;
                  break;
                end;
        end;
        2: begin
            team.members[1].rpm_timer := 0;
            l1.intensity := 1;
            snd_Play( sndShoot, FALSE, CalcX2D( team.members[1].pos.x ), CalcY2D( team.members[1].pos.y ) );
            team.members[1].switch := team.members[1].switch + 24;
            if team.members[1].switch > 150 then team.members[1].switch := 150;
            dec(team.members[1].shots);
            for k := 0 to 9 do
            for I := 1 to 128 do
              if not bullets[i].exist then
                begin
                  bullets[i].exist := true;
                  if team.members[1].shots = 0 then team.members[1].phase := 1;
            //      log_add('bullet add in slot #'+u_IntToStr(i));
                  bullets[i].pos.X := team.members[1].pos.x + 32 + 16 * m_cos(round(team.members[1].ang));
                  bullets[i].pos.Y := team.members[1].pos.y + 32 + 16 * m_sin(round(team.members[1].ang));
                //рассчитываем угол полёта исходя из позииции
                //курсора и разброса
                // выбираем рандомный угол
                  d1 := random(360);
                // выбираем рандомный радиус из интервала
                  d2 := random(round(team.members[1].switch * 1000))/1000;
                // считаем коэффициент отдаления прицела от игрока
                // если мы целим в противника "в упор", то разброс будет минимальным
                // на дистанции он принимает рельные значения.
                // разброс равен 100% на дистанции в 500 пикселей
                  d3 := m_distance(mouse_x, mouse_y,
                                 team.members[1].pos.x,
                                 team.members[1].pos.y);
                  d3 := d3/500;
                // считаем угол
                  bullets[i].ang := m_Angle(Mouse_x + d2 * d3 * m_cos(round(d1)),
                                          mouse_y + d2 * d3 * m_sin(round(d1)),
                                          bullets[i].pos.X,
                                          bullets[i].pos.Y);
                  bullets[i].spd := 10;
                  bullets[i].dmg := 3;
                  break;
                end;
        end;
        end;
        end;

  // второй игрок
    if team.members[2].phase = 1 then
    begin
      inc(team.members[2].reload_timer);
      if team.members[2].reload_timer > team.members[2].reload then
         begin
           team.members[2].phase := 0;
           team.members[2].reload_timer := 0;
           team.members[2].shots := team.members[2].max_shots;
         end;
    end;

    if team.members[2].phase <> 1 then
    for I := 1 to 256 do
      if enemies[i].exist then
           if m_distance(team.members[2].pos.x,
                         team.members[2].pos.y,
                         enemies[i].pos.x,
                         enemies[i].pos.y) < 180 then
           begin
             team.members[2].ang := m_Angle(team.members[2].pos.x + 32,
                                            team.members[2].pos.y + 32,
                                            enemies[i].pos.x,
                                            enemies[i].pos.y) + 180;
             if team.members[2].ang > 359 then team.members[2].ang := team.members[2].ang - 360;
             team.members[2].phase := 2;
             break;
           end else team.members[2].phase := 0;

    if team.members[2].phase = 2 then
    if team.members[2].rpm_timer > team.members[2].rpm then
        begin
            snd_Play( sndShoot2, FALSE, CalcX2D( team.members[2].pos.x ), CalcY2D( team.members[2].pos.y ) );
            team.members[2].rpm_timer := 0;
            team.members[2].switch := team.members[1].switch + 4;
            if team.members[2].switch > 50 then team.members[2].switch := 50;
            for I := 1 to 128 do
              if not bullets[i].exist then
                begin
                  bullets[i].exist := true;
                  l2.intensity := 1;
                  dec(team.members[2].shots);
                  if team.members[2].shots = 0 then team.members[2].phase := 1;
                  bullets[i].pos.X := team.members[2].pos.x + 32 + 16 * m_cos(round(team.members[2].ang));
                  bullets[i].pos.Y := team.members[2].pos.y + 32 + 16 * m_sin(round(team.members[2].ang));
                //рассчитываем угол полёта исходя из позииции
                //курсора и разброса
                // выбираем рандомный угол
            {      d1 := random(360);
                // выбираем рандомный радиус из интервала
                  d2 := random(round(team.members[1].switch * 1000))/1000;
                // считаем коэффициент отдаления прицела от игрока
                // если мы целим в противника "в упор", то разброс будет минимальным
                // на дистанции он принимает рельные значения.
                // разброс равен 100% на дистанции в 500 пикселей
                  d3 := m_distance(mouse_x, mouse_y,
                                 team.members[1].pos.x,
                                 team.members[1].pos.y);
                  d3 := d3/500; }
                // считаем угол
                  bullets[i].ang := team.members[2].ang + random(12) - 6;
                  bullets[i].spd := 10;
                  bullets[i].dmg := 3;
                  break;
                end;
        end;

    // третий игрок
    if team.members[3].phase = 1 then
    begin
      inc(team.members[3].reload_timer);
      if team.members[3].reload_timer > team.members[3].reload then
         begin
           team.members[3].phase := 0;
           team.members[3].reload_timer := 0;
           team.members[3].shots := team.members[3].max_shots;
         end;
    end;

    if team.members[3].phase <> 1 then
    for I := 1 to 256 do
      if enemies[i].exist then
           if (m_distance(team.members[3].pos.x,
                         team.members[3].pos.y,
                         enemies[i].pos.x,
                         enemies[i].pos.y) < 400) then
           begin
             team.members[3].ang := m_Angle(team.members[3].pos.x + 32,
                                            team.members[3].pos.y + 32,
                                            enemies[i].pos.x,
                                            enemies[i].pos.y) + 180;
             if team.members[3].ang > 359 then team.members[3].ang := team.members[3].ang - 360;
             if InSector(team.members[3].ang) then
                begin
                  team.members[3].phase := 2;
                  break;
                end else
                team.members[2].phase := 0;
           end else team.members[2].phase := 0;

    if team.members[3].phase = 2 then
    if team.members[3].rpm_timer > team.members[3].rpm then
        begin
            team.members[3].rpm_timer := 0;
        //    snd_Play( sndShoot3, FALSE, CalcX2D( team.members[3].pos.x ), CalcY2D( team.members[3].pos.y ) );
            l3.intensity := 1;
            team.members[3].switch := team.members[1].switch + 4;
            if team.members[3].switch > 50 then team.members[3].switch := 50;
            for I := 1 to 128 do
              if not bullets[i].exist then
                begin
                  bullets[i].exist := true;
                  dec(team.members[3].shots);
                  if team.members[3].shots = 0 then team.members[2].phase := 1;
                  bullets[i].pos.X := team.members[3].pos.x + 32 + 16 * m_cos(round(team.members[3].ang));
                  bullets[i].pos.Y := team.members[3].pos.y + 32 + 16 * m_sin(round(team.members[3].ang));
                //рассчитываем угол полёта исходя из позииции
                //курсора и разброса
                // выбираем рандомный угол
            {      d1 := random(360);
                // выбираем рандомный радиус из интервала
                  d2 := random(round(team.members[1].switch * 1000))/1000;
                // считаем коэффициент отдаления прицела от игрока
                // если мы целим в противника "в упор", то разброс будет минимальным
                // на дистанции он принимает рельные значения.
                // разброс равен 100% на дистанции в 500 пикселей
                  d3 := m_distance(mouse_x, mouse_y,
                                 team.members[1].pos.x,
                                 team.members[1].pos.y);
                  d3 := d3/500; }
                // считаем угол
                  bullets[i].ang := team.members[3].ang + random(8) - 4;
                  bullets[i].spd := 10;
                  bullets[i].dmg := 3;
                  break;
                end;
        end;


    // поедание

    for I := 1 to 3 do
      for j := 1 to 256 do
        if enemies[i].exist then
          if ColORectVsORect(ORect(pt(enemies[j].pos.x + 24, enemies[j].pos.Y + 48),
                                      24, 48, enemies[j].ang + 90),
                                ORect(pt(team.members[i].pos.x + 32,  team.members[i].pos.y + 32),
                                      16, 16, team.members[i].ang))  then dec(team.hp);

    if team.hp <= 0 then
      begin
        MessageBoxA( 0, 'Game Over', 'Error', $00000010 );
        zgl_Exit;
      end;
    
          

end;


procedure BulletUpd;
var i, j,k: integer;
begin
  for I := 1 to 128 do
    if bullets[i].exist then
      begin
        bullets[i].pos.X := bullets[i].pos.x + bullets[i].spd * m_cos(round(bullets[i].ang));
        bullets[i].pos.Y := bullets[i].pos.Y + bullets[i].spd * m_sin(round(bullets[i].ang));
        if bullets[i].pos.X < -1000 then bullets[i].exist := false;
        if bullets[i].pos.Y < -1000 then bullets[i].exist := false;
        if bullets[i].pos.X > 1000 then bullets[i].exist := false;
        if bullets[i].pos.Y > 1000 then bullets[i].exist := false;

        for j := 1 to 256 do
          if enemies[j].exist then
             if ColORectVsORect(ORect(pt(enemies[j].pos.x + 24, enemies[j].pos.Y + 48),
                                      24, 48, enemies[j].ang + 90),
                                ORect(pt(bullets[i].pos.x + 4,  bullets[i].pos.Y + 1),
                                      8, 2, bullets[i].ang)) then
              begin
                bullets[i].exist := false;
                enemies[j].HP := enemies[j].HP - bullets[i].dmg;
                if enemies[j].HP <= 0 then enemies[j].dead := true;
                for k := 0 to 2 do
                  AddBlood(pt(bullets[i].pos.x + (random(20) + k*10 )* m_cos(round(bullets[i].ang)),
                              bullets[i].pos.y + (random(20) + k*10) * m_sin(round(bullets[i].ang))));
              end;
            
      end;
end;

end.                                                                         ..
