i have enemy class. and i am createing bunch of them and storing them in arraylist.
i am printing them in random places.
for(...){
enemy_class = new Enemy((WINDOW_WIDTH/2)+r.nextInt(WINDOW_WIDTH/2), r.nextInt(WINDOW_HEIGHT));
//add in arraylist
}
the problem is that some times they get create on top of each other. so my plan is to set collision on enemy so that they cant go through each other.
i set a Rectange getbound in enemy class. but not sure how to set collision on all the enemies in arraylist.
/*** enemy dont draw on top of each other ***/
public void enemy_enemy_collision(ArrayList<Enemy> enemy_store_temp)
{
for(int i = 0; i < enemy_store_temp.size(); i++){
Enemy enemy_class_temp = (Enemy)enemy_store_temp.get(i);
if(this.getBounds().intersects(this.getBounds()))
{
}
}
}
the if statment is not right bc it is not checking collison with all the enemies in arraylist.
i was thinking some thing like:
for(int i = 0; i < enemy_store_temp.size(); i++){
Enemy enemy_class_temp = (Enemy)enemy_store_temp.get(i);
for(int x = i+1 ; x < enemy_store_temp.size(); x++){
Enemy enemy_class_temp_next = (Enemy)enemy_store_temp.get(x);
if(this.getBounds().intersects(enemy_class_temp_next.getBounds()))
{
x+=30;
}
}
}
so this way i am check collison enemy1 with all the enemies but not enemy 1. than enemy 2 with all the enemy but not enemy 2.
but this way x+= 30 can still move enemy on top of each other.