I’m making a game with Libgdx, currently implementing terrain generation. Here is the code:
@Override
public void render(float delta) {
ScreenUtils.clear(Color.BLACK);
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
shapeRenderer.setProjectionMatrix(camera.combined);
spriteBatch.begin();
Project5.INSTANCE.landTiles.forEach(landTile -> {
spriteBatch.draw(landTile.texture,landTile.x,landTile.y);
});
spriteBatch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.GOLDENROD);
shapeRenderer.circle(player.x,player.y,32);
shapeRenderer.end();
int speed=1000;
if(Gdx.input.isKeyPressed(Input.Keys.W))
{
player.y+=delta*speed;
}
if(Gdx.input.isKeyPressed(Input.Keys.S))
{
player.y-=delta*speed;
}
if(Gdx.input.isKeyPressed(Input.Keys.A))
{
player.x-=delta*speed;
}
if(Gdx.input.isKeyPressed(Input.Keys.D))
{
player.x+=delta*speed;
}
camera.position.set(player.x,player.y,0);
Vector2 cameraPosition=new Vector2(MathUtils.round(camera.position.x/Project5.chunkSize/64),MathUtils.round(camera.position.y/Project5.chunkSize/64));
generateChunk(cameraPosition);
generateChunk(cameraPosition.sub(1,1));
System.out.println(Project5.INSTANCE.generatedChunks.size());
}
private void generateChunk(Vector2 cameraPosition) {
ArrayList<Vector2> generatedChunks= Project5.INSTANCE.generatedChunks;
if(!generatedChunks.contains(cameraPosition))
{
generatedChunks.add(cameraPosition);
float nextX= cameraPosition.x*Project5.chunkSize;
float nextY= cameraPosition.y*Project5.chunkSize;
for (float x =nextX; x <nextX +Project5.chunkSize; x++) {
for (float y=nextY;y<nextY+Project5.chunkSize; y++) {
LandTile landTile = new LandTile(Project5.INSTANCE.water, (int) ((x-Project5.chunkSize/2) * 64), (int) (y-Project5.chunkSize/2)*64);
Project5.INSTANCE.landTiles.add(landTile);
}
}
}
}
For some reason second ‘generateChunk’ call keeps adding the same vector. Here’s what debugger is showing:
It shows that it’s adding vector 0,0, but generatedChunks contains only vectors -1,-1. Am I blind? This seems wrong.