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
\$\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.

| improve this answer | |
\$\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)]

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Draco18s no longer trusts SE Jan 18 '16 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 Jan 18 '16 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\$ – Draco18s no longer trusts SE Jan 18 '16 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 Jan 18 '16 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.