0
\$\begingroup\$

What is better way of setting up a map for a java 2D topdown game? Would it be having an ArrayList of tiles, a simple 2D array, a HashMap? (Note: the tiles/blocks or whatever are squared)

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

It depends on what you need to store. If you just want the positions of the tiles, a 2D array is probably the easiest way to store it (unless you have a dynamic world size). If you want the connections (for movement costs, etc.) then a Graph is probably what you want to use.

\$\endgroup\$
1
\$\begingroup\$

Usually a single one dimensional array would work fine, just remember that, to get the tile you want you have to use the formula tile[x +(y*tiles_in_row)]

\$\endgroup\$
4
  • \$\begingroup\$ While 1D works 2D is easier on the developer in terms of actual code. It's a lot easier to write [x][y] than to write [x +(y*width)] and the performance impact is exceedingly minimal. \$\endgroup\$ Commented Jan 18, 2016 at 18:17
  • \$\begingroup\$ If you want to store an 8bit integer for cell in a 255^2 map the memory usage and performance will be near the same in a 1D or a 2D array, but in a bigger maps (I mean, huge), the performance will decrease in a 2D array, iterations over large 1D arrays are more efficient than looping through dimensions \$\endgroup\$
    – PRDeving
    Commented Jan 18, 2016 at 20:59
  • \$\begingroup\$ Ay what sufficiently large values of huge are you referring to? That is: how large does the array need to be before the performance makes a noticeable impact? \$\endgroup\$ Commented Jan 18, 2016 at 21:03
  • \$\begingroup\$ The difference is apreciable since you set a 2^2 map, but, if you set a 2048^2 map f.e. you can see how performance drops, won't be a big dropdown but its there, and added to other performance issues, could steal some fps to your final product \$\endgroup\$
    – PRDeving
    Commented Jan 18, 2016 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .