Add an ArrayList to another ArrayList


#21

Hi i start to write and i have my first issues :slight_smile:

First i forgot to say, im using Java with Eclipse.

So here what i wrote for now :

1: my class Game (where i launch world)

public class Game extends PApplet{
	
	public static void main(String[] args){
		PApplet.main("Game");
	}
	
	public void settings(){
		size(1200,900);
		
	}
	
	World world;
	
	public void setup(){
		world = new World(this);
		surface.setResizable(true);
	}
	
	public void draw(){
		world.makeVegetalPassif(20, 20);
		world.render();
	}
}

then the World :

public class World {
	PApplet parent;
	public int worldW = 2000;
	public int worldH = 2000;
	
	Player player;
	
	Prairie prairie;
	
	ArrayList<VegetalPassif> vegepas = new ArrayList<VegetalPassif>();
	
	public World(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);
		///////////////////
		
	}
	
	public void makeVegetalPassif(int nbrGrass2, int nbrMushroom){
		for(int i = 0; i < nbrGrass2 + nbrMushroom; i++){
			PVector next = findSuitablePosition(100);
			if(next == null){
				break;
			}
			if(i < nbrGrass2){
				vegepas.add(new Grass2(parent,next));
			}
			else{
				vegepas.add(new Mushroom(parent,next));
			}
		}
	}
	
	private PVector findSuitablePosition(int maxTime){
		PVector pp;
		boolean found;
		int time = parent.millis();
		do{
			found = true;
                        ////////////here i make a change since i use a bigger world than the screen///////////
			pp = new PVector(parent.random(worldW-1750 , worldW-250), parent.random(worldH-1750 , worldH-250));
			for(VegetalPassif vegepassif : vegepas){
				if(PVector.dist(vegepassif.pos, pp) < 32){
					found = false;
					break;
				}
			}
		}while(!found && parent.millis()- time <= maxTime);
		return found ? pp : null;
	}
	
	public void render(){
		
		parent.background(0); ///fond noir = le nΓ©ant
		
			
		parent.pushMatrix();
		parent.pushStyle();
		
		//////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);
		/////////////////////////
		
		////////VegetalPassif////////
		for(VegetalPassif vegepassif : vegepas){
			vegepassif.render();
		}
		/////////////////////////////
		
		
		parent.popStyle();
		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);
		
	}
}

VegetalPassif (its like Obstacle)

public class VegetalPassif {
	PApplet p;
	public PVector pos = new PVector();
	int color;
	int diam; ///diametre
	
	VegetalPassif(PApplet parent, PVector pos){
		p = parent;
		this.pos.set(pos);
	}
	
	public void render(){
	    p.pushMatrix();
	    p.pushStyle();
	    p.noStroke();
	    p.fill(color);
	    p.translate(pos.x, pos.y);
	    p.ellipse(0, 0, diam, diam);
	    p.popStyle();
	    p.popMatrix();
	}
}

Grass2 (the β€œ2” is because i dont want to erase the class i wrote before):

public class Grass2 extends VegetalPassif {
	
	
	public Grass2(PApplet p, PVector pos){
		super(p,pos);
		diam = 32;
		color = 006633;
	}
}

and Mushroom:


public class Mushroom extends VegetalPassif {
	
	
	public Mushroom(PApplet p, PVector pos){
		super(p,pos);
		diam = 32;
		color = 666600;
	}
}

i didnt change the player and prairie.
i use a world bigger than my screen to have more space (i should told you that im sorry).
the player for now is used only to see my world.

So, nothing appeard on the screen except my player and the prairie and its super laggy.


#22

i found that its not render but the world is stuck in a loop.
So i have two issues:

first: makeVegetalPassif and findSuitablePosition are stuck in a loop
second: nothing is render exept player and prairie


#23

The draw method is executed ~60fps so the first line world.makeVegetalPassif(20, 20); is also executed 60 times a second. So you are adding 1200 Grass2 and 1200 Mushroom a second there will not be suitable positions for all but the findSuitablePosition method allows 100ms per attempt, a maximum of 4 minutes per frame!


#24

ho i didnt know that. how do i manage that ?


#25

Do not create lots of objects in the draw loop. Perhaps do it when a key is typed so someother event.