Friday, August 5, 2011

Java 2D TileMap Code Optimization

Hey guys, just thought I would give you guys a quick tip on 2D tile map optimization that's been helping me in this transition from Xna to Java. Normally what I do in this kind of a game is have a 2D array of tiles (Tile class) which make up the contents of the map. I then set each tile to be the appropriate kind of tile (i.e. dirt, grass, rock, etc).

With this new Java version of Prospectus though, I thought I could do a little better than that, so I set off to find a different way to get the same result. I came up with the idea of using a 2D array of characters instead of tiles. I then use that character map to decide what tile I need to draw in my draw method, as I need it. I then use a few different methods that allow me to retrieve the correct type of tile to draw/do calculations with for a specific x and y coordinate on the map, solely based on what character is that the x and y coordinate.

This, I believe, saves me time and speeds up the execution of my code. Instead of having to keep each tile on the entire map in memory, now I only care about the tiles that are on the screen (plus a couple rows and columns for cleanliness). This also helps with start-up time because I'm setting character literals instead of user-defined class instances.

I hope this has given some of you ideas on how to improve your own games in Java or C#/Xna (this method will work for either language). As I near completion of the port to Java, I'm getting more and more excited about where Prospectus can and will go in the future. Thanks for reading and be sure to check back soon for more updates.

-Cheers

No comments:

Post a Comment