Chunks and Cross-chunk Tile Transitions

Good day folks!

I’m currently struggling to implement chunks into a game that wasn’t initially designed to have chunks. I’ll get straight to the point and say that it’s a mess, and I’m probably overcomplicating stuff so here is the main code…


/*
 * Globals are used throughout the code
 */

// how many chunks per axis in map
public static int chunkCount = 3;

public static int tileSize = 32;

// the amount of tiles per axis in chunks
public static int chunkSize = 8;

/*
 * The main Level.java code
 */

// Chunk array
public Chunk[][] chunks = new Chunk[Globals.chunkCount][Globals.chunkCount];

// Generation
public void generateMap() {
	for (int x = 0; x < Globals.chunkCount; x++) {
		for (int y = 0; y < Globals.chunkCount; y++) {
			chunks[x][y] = new Chunk(input, x * (Globals.chunkSize * Globals.tileSize), y * (Globals.chunkSize * Globals.tileSize));
		}
	}
}

// tick and render are basically the same apart from tick(chunkX, chunkY) and render(g, chunkX, chunkY)
for (int x = 0; x < Globals.chunkCount; x++) {
	for (int y = 0; y < Globals.chunkCount; y++) {
		if (chunks[x][y] != null) {
			chunks[x][y].tick(x, y);
		}
	}
}

/*
 * The main Chunk.java code
 */

// Tiles array
public Tile[][] tiles = new Tile[Globals.chunkSize][Globals.chunkSize];

// Constructor for chunk tile generation
for (int i = 0; i < Globals.chunkSize; i++) {
	for (int j = 0; j < Globals.chunkSize; j++) {
		if (Utils.randInt(0, 10) <= 1) {
			tiles[i][j] = new MudTile(Tile.mud, chunkX + i * Globals.tileSize, chunkY + j * Globals.tileSize);
		} else {
			tiles[i][j] = new GrassTile(Tile.grass, chunkX + i * Globals.tileSize, chunkY + j * Globals.tileSize);
		}
	}
}

for (int i = 0; i < Globals.chunkSize; i++) {
	for (int j = 0; j < Globals.chunkSize; j++) {
		tiles[i][j].setTransitionId(getId(i, j));
	}
}

// The render (as you can tell all of the code needed to render rectangles correctly over the entirety of the map of chunks/tiles
for (int i = 0; i < Globals.chunkSize; i++) {
	for (int j = 0; j < Globals.chunkSize; j++) {
		tiles[i][j].render(g);
		g.drawRect((chunkX * (Globals.chunkSize * Globals.tileSize)) + i * Globals.tileSize, (chunkY * (Globals.chunkSize * Globals.tileSize)) + j * Globals.tileSize, Globals.tileSize, Globals.tileSize);
	}
}

I can’t but help feel this is overcomplicated code to handle drawing a rectangle at the correct position in each chunk. I am also interested in cross-chunk tile transitions so it doesn’t treat the edges of each chunk as the end of the map and would be open to suggestions.

Thanks! :slight_smile:

I think the trick with chunks is that they’re only there for optimizations. A setBlock and getBlock method would benefit as all you have to do is call those functions and it should do the chunk figuring out in the methods, rather than you having to do the division and remainder stuff scattered in your code.

When I was developing a game like this, chunks were only for sending multiplayer data. The whole world acted like a big array of blocks but I just had getters and setters to retrieve it at their actual positions and not in-chunk positions, if that makes any sense.

Oh I see now, do I’d only do the chunk/tile calculations once inside a method that handles the tiles rather than all over the place. Thanks! :slight_smile: