Hey guys!
So I’m still working on my 2D side-scrolling Tile game, using no libraries. All pure java. It’s running okay, but I believe this is where I get stuck:
public Level(String name) {
this.worldName = name;
Tiles = new Tile[100][100];
for (int x = 0; x < Tiles.length; x++) {
for (int y = 0; y < Tiles[0].length; y++) {
Tiles[x][y] = new Tile(new Rectangle(x * 32, y * 32, 32, 32));
}
}
}
Now, what I mean by “stuck” is, if I change the “Tiles = new Tile[100][100];” to “Tiles = new Tile[4000][4000];” my game freezes when I hit my play button… Which does this:
/* Initialize everything needed, here. */
public void init() {
this.camera = new Camera(0, 0);
this.level = new Level("test");
level.Generate();
textures = new TextureLoader("Tiles/Tilesheet.png");
textures.LoadTiles();
player = new Entity0Player(new Vector2(100, 350));
inputHandler = new InputHandler();
inputHandler.setGame(this);
inputHandler.setPlayer(player);
this.addKeyListener(inputHandler);
this.addMouseListener(inputHandler);
this.addMouseWheelListener(inputHandler);
this.GAME_WIDTH = getWidth() + 70;
this.GAME_HEIGHT = getHeight() + 90;
this.xOffset = (int)player.position.x - (GAME_WIDTH / 2);
this.yOffset = (int)player.position.y - (GAME_HEIGHT / 2);
this.cameraView = new Rectangle(xOffset, yOffset, GAME_WIDTH, GAME_HEIGHT);
start();
}
It seems to freeze on the “new Level()” portion because I’m trying to initialize each and every tile of the 4000x4000 tile array. So, my question:
How would I go about chunking so it only creates/initializes the ones needed and then unloads them when out of view to prevent lagging when the game generates a lot of tiles?