Hi i try to find a way to place objects on my world but without overlapping and i cant find a way.
here’s my code:
public class World2 {
PApplet parent;
public int worldW = 2000;
public int worldH = 2000;
Player player;
Prairie prairie;
ArrayList<Grass> grass;
public World2(PApplet p){
parent = p;
//////player//////
float px = worldW/2;
float py = worldH/2;
PVector playerVector = new PVector(px,py);
player = new Player(parent,playerVector);
//////////////////
/////prairie///////
prairie = new Prairie(parent);
///////////////////
/////grass/////////
int nombGrass = 1;
grass = new ArrayList<Grass>();
for(int i = 0; i < nombGrass; i++){
PVector l = new PVector(p.random(worldW-1750 , worldW-250), p.random(worldH-1750 , worldH-250));
grass.add(new Grass(p, l));
}
///////////////////
}
public void run(){
parent.background(0); ///fond noir = le néant
////// attention il faut pop les créature sur base du monde et non de l'écran !!!!
////// mtn il faut mettre dans le push les objets, ou il vont suivrent la camera
parent.pushMatrix();
//////fct pas correctement, a refaire avec une "camera"
parent.translate(-player.position.x+worldW-parent.width-200, -player.position.y+worldH-parent.height-650);
//////le fond blanc//////
parent.fill(255); ///le monde en lui meme
parent.rect(0, 0, worldW, worldH);
/////////////////////////
//////prairie////////////
prairie.run(parent);
/////////////////////////
/////grass///////////////
Iterator<Grass> G = grass.iterator();
while(G.hasNext()){
Grass g = G.next();
g.display(parent);
g.grow();
}
/////////////////////////
parent.popMatrix();
player.PlayerMvt(parent);
player.run(parent);
parent.fill(255,0,0);
parent.text("player x " + player.position.x + " " + "player y " + player.position.y, 10, 10);
}
}
and my grass (the object):
public class Grass {
PApplet parent;
ArrayList<PVector> grass;
public PVector position;
int r = 32;
@SuppressWarnings("deprecation")
public Grass(PApplet p,PVector l){
position = l.get();
parent = p;
grass = new ArrayList<PVector>();
}
@SuppressWarnings("deprecation")
public void add(PVector l){
grass.add(l.get());
}
public void display(PApplet p){
for(PVector g : grass){
p.ellipseMode(PConstants.CENTER);
p.fill(0);
p.ellipse(g.x, g.y, r, r);
}
}
public void grow(){
int totalgrass = 25;
if(parent.random(1) < 0.1 && grass.size() < totalgrass){
int nombGrass = 1;
for(int a = 0; a < nombGrass; a++){
grass.add(new PVector(parent.random(250, 1750),parent.random(250, 1750)));
}
}
}
public ArrayList<PVector> getGrass(){
return grass;
}
public PVector position(){
return position;
}
public float getR(){
return r;
}
}
can someone exmplain to me how to do it ?
Thanks for yours times