Loading a map chunk BEGINNER QUESTION

Ok, i apologize if you have trouble understanding me, my biggest problem in this world is putting my thoughts into words or on paper, so that others can understand

i’m working on a 2d engine purely using the java Graphics class

right now i have an 2d array of tiles(originally arraylist but fixed), i’m wanting to get a 20 x 20 chunk out of the entire arraylist, based on a location

HERES THE ISSUE i’ve ran into a problem where the location given to load the chunks will also determine how big the chunk loaded is, which should be a 20x20 tile chunk, i’m guessing it has to do with the function trying to get tiles that arent there(nulls) for example if i wanted to grab a chunk from location 1x1 then it would only grab 10 tiles into the positive direction, making it only a 10x10 chunk

heres my code


public Tile[][] getChunk(Locatable locatable){
		Location location = locatable.getLocation();
		Tile[][] chunk = new Tile[20][20];
		for(Tile tile : tiles){//tiles being the tiles array
			int tileX = tile.getLocation().getX(), tileY = tile.getLocation().getY();
			if(tileX < location.getX() + 10 && tileX > location.getX() -10
					&& tileY < location.getY() + 10 && tileY > location.getY() - 10){
				chunk[tileX][tileY] = tile;
			}
		}
		return chunk;
	}

Don’t use lists for tiles. Use arrays.

Well if the tiled Area is a square/Cube.
You just point to the center of it and grab the tiles around in the % you want.

Like, if you want to get the chunk of the position 100x100, and you want to get 10x10,

You will get 95->105 x 95 ->105

Get it? I hope that helps.

You didn’t say what the problem was, but looking at the code I’m guessing it’s an IndexOutOfBounds exception.

You’re using tileX/Y as the array indices when you’re inserting a tile into chunks, but they’re the indices of the ‘world’ tiles array, instead they should be 0…20 range.

i.e. something like this:


...

for( int x = 0; x < 20; ++x ) {
	// Calc world X coord
	final int tx = location.getX() + x;
	if( ( tx < 0 ) || ( tx >= width ) ) continue;

	for( int y = 0; y < 20; ++y ) {
		// Calc world Y coord
		final int ty = location.getY() + y;
		if( ( ty < 0 ) || ( ty >= height ) ) continue;

		// Calc 1-D index into world array
		final int idx = ty * width + tx;
		chunk[ x ][ y ] = tiles[ idx ];
	}
}

Any tiles at your location that are outside of the world will be null.

This assumes you know the width and height of the tiles array (easy to work out presumably from your loader).

It also has the added bonus that you don’t need to iterate through the entire world tiles array to pick out the 20x20 you want.

Hope this helps.

  • stride

ok thanks, this isn’nt dynamic but i should be able to find a work around

also note that im using a 3d array for chunks, chunk[1] being a 2d tile array

and i did point out the problem but not obviously, it is obvious now, ill get back to you when i have the time to put some work in

First off, why 3D array when you only need two dimensioms of tiles?

Second, it is dynamic. Instead of using < 20, just replace 20 with a variable.

its either a 3d array or a chunk class that stores a 2d tile array, then i would have a 1d array of chunks, which would basically be the same as a 3d array of tiles wouldn’t it not? the first dimension being the place keeper and the other 2 being the actual 2d array of tiles

if im wrong please correct me, i’m still a novice

Java is an object orientated language. If something is easier to use by using objects, then use them; that’s what they’re for. Creating a chunk class is definitely the better option since it’ll be easier to manage and to debug.

My rule of thumb for when to create a class is if the following conditions are true:

  • It’s convenient to do so
  • Each object has unique states but shared behaviours
  • You will create multiple instances of the object

ah thanks for clarifying, also realized creating a 3d array of tiles would be totally atrocious as the first dimension wouldn’t be holding any tiles

will get back on this topic once i finished a decent map editor and parser for the map files