{"size": 1106, "ext": "js", "max_stars_repo_path": "src/obstacles.js", "max_stars_repo_name": "BrunoMendes41/Alien-Rush", "max_stars_repo_stars_event_min_datetime": "2020-05-23T20:15:43.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-23T20:15:43.000Z", "max_issues_repo_path": "src/obstacles.js", "max_issues_repo_name": "BrunoMendes41/Alien-Rush", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/obstacles.js", "max_forks_repo_name": "BrunoMendes41/Alien-Rush", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0416666667, "max_line_length": 53, "alphanum_fraction": 0.5804701627}
class Obstacles 
{
  constructor(){
    this.obstaclesInScreen = [];
    this.spawnTime = 1;    
    // sprite
    this.image = new Image();
    this.image.src = 'images/obstacle.png';
  }

  spawnObstacle(context){       
    this.obstaclesInScreen.push({
      x: 600,
      y: randomInt(10,600),
      width: 43,
      height: 43,
      movingSpeed: randomInt(8, 11)
    })
  }   
  
  updateObstaclesInScreen(){
    // adicionando novos obstaculos na tela
    if(this.spawnTime <= 0){     
        this.spawnObstacle();
        this.spawnTime = 10;     
    } else {
      this.spawnTime--;
    }

    //removendo obstaculos que passam a tela
    //e atualizando posição conforme a velocidade    
    for(let i in this.obstaclesInScreen){
      let obstacle = this.obstaclesInScreen[i];
      if(obstacle.x <= -10){
        this.obstaclesInScreen.splice(i, 1);
      } else {
        obstacle.x -= obstacle.movingSpeed; 
      }
    }
  }

  draw(context){
    let img = this.image; 
    for(let obstacle of this.obstaclesInScreen){  
      context.drawImage(img, obstacle.x, obstacle.y);
    }
  }  
}