
class Invasores {

    float center;
    int ranRange;
  
    Invasores() {
        xdir = 1;
        speed = 1;
        this.center = (width/2) + 188;
    }
  
  
    void init() {
      int index = 0;
      for (int j = 0; j < 4; j++) {       ///////// CREATE ARRAY DE INSECTOS
        for (int i = 0; i < 12; i++) {
  
          String[] invStrings = {"img/inv"+j*2+".png", "img/inv"+(j*2+1)+".png"};
          //String[] invStrings = {"img/inv0.png", "img/inv1.png"};
          PImage[] newInvImgs = new PImage[2];
          AnimatedSprite newInvasor;
          loadAnimation(invStrings, newInvImgs);
          newInvasor = new AnimatedSprite(newInvImgs, 8, ((i*invSeqASprite.getWidth()*1.3)) + width/4, (invSeqASprite.getWidth()*j)+invSeqASprite.getWidth()+30, 37, 37 );  /////// + width/4 Center of screen
          newInvasor.setFPS(4);
          newInvasor.loop();
          invSpriteGrp[index] = newInvasor;
          index++;
          //newInvasor.shapeColor = invColors[j] ;
        }
      }
    }
  
  
    void update() {
  
      for (int i = 0; i < invSpriteGrp.length; i++) {                                     ///////// INSECTOS INVASORES GRUPO MUEVEN
  
            if (!hitGranOn) { invSpriteGrp[i].x += xdir; } 
            else { invSpriteGrp[i].x += random(-1.5, 1.5); }



            if (loseOnce) {
                if (granjero.hits(invSpriteGrp[i])) {                                           /////// SI ELGRANJERO ES TOCADO POR INSECTOS
                    loseOnce = false;
                    youLose();
                }
            }

            if (!misilExist) {                /////// MISILES DE INSECTOS
            
                if (i == 0) { numbers = randNums(1,48,8); } 
                  
                hitOnce = true;
                boolean check = false;
                check = isInArray(numbers, i);
                
                if (check) {
                    Misil misil = new Misil(invSpriteGrp[i].x, invSpriteGrp[i].y);
                    misiles.add(misil);
                }
            }
                
            invSpriteGrp[i].x += xdir; 
            invSpriteGrp[i].update(); 
            invSpriteGrp[i].draw();
            
            if (invSpriteGrp[i].x >= width || invSpriteGrp[i].x <= 0) {   /////// SI TOCAN EL BORDE CANVAS
                edge = true;
            }
      }
  
      if (edge) {           /////// BAJANDO CUANDO TOCA BORDE
      
          printArray(randNums(1,48,8));
          if (speed <= 1.19) { 
              speed = speed + 0.01;
              xdir = xdir * speed;
          }

          xdir *= -1;

          for (int i = 0; i < invSpriteGrp.length; i++) {
              invSpriteGrp[i].y += (invSpriteGrp[0].getHeight()/2);
          }
          edge = false;
      }
    }
  
    void initMenu() {
  
      //invMenuGrp = new Group();
  
      //for (let i = 0; i < 3; i++) {
      //    var newInvasor = createSprite(((i*invSeqA.getWidth()*1.6)) + this.center, 60);  /////// + width/4 Center of screen
      //    newInvasor.scale = 1.4;
      //    //newInvasor.shapeColor = invColors[j] ;
      //    newInvasor.addAnimation('stand', secuencias[i]);
      //    newInvasor.animation.frameDelay = 12;
      //    invMenuGrp.add(newInvasor);
      //}
    }
}


private static boolean isInArray(int[] nums, int num) {
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == num) {
            return true;
        }
    }
    return false;
}


import java.util.Random;

public static int[] randNums(int start, int end, int count) {
    Random rng = new Random();

    int[] result = new int[count];
    int cur = 0;
    int remaining = end - start;
    for (int i = start; i < end && count > 0; i++) {
        double probability = rng.nextDouble();
        if (probability < ((double) count) / (double) remaining) {
            count--;
            result[cur++] = i;
        }
        remaining--;
    }
    return result;
}
