0
\$\begingroup\$

So, I've been playing around with the concept of a game, mechanically think of the Civilization games(Tile, turn-based strategy) but I am quite uncertain about where I should store the units location in the code. The tiles exist on a X and Y coordination of integers and that's how I imagine that the unit's would work too.

To clarify, I am unsure what's most effective, if it's example one: To have the tiles have a variable for which units are standing upon it and then when the unit move destroy them and recreate them on the tiles they are moving to.

That way when the tile is clicked it should be very easy to get the unit but it seems like it might be unsafe to remove and recreate units or sending a variable along. This might also be very unpractical when issuing an order to move somewhere, though, I am don't know.

The second example is to add the units to some kind of unit arraylist (or alike) but by that the game would have to find the unit with the right coordinates in said list (right?), this might appear as if the game is unresponsive and slow. Though it might not matter when you sort the list every x interval...

Any other ways to do it, ideas, fixes or even comments from you guys would be much appreciated.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

It's called a Hash data structure aka an associative array. It is often used to represent a sparse array. For instance like in your case, a huge world with myriads of tiles, that is sparsely occupied by only hundreds or thousands of units.

It is very fast because it knows where to look for values (in your case units) with a certain key (in your case positions [x, y] or tiles).

When a unit is created in some position, populate the hash with the position as key [x, y] (or alternatively use the tile it's on), and if there is no group of units under that key, add the unit as a new group if (hash[tile] == null) hash[tile] = new ArrayList<Unit>(unit);. If units exist there, add the new unit to the existing "positional" group else hash[tile].append(unit); // Code is untested.

Now you populate the Hash only with positions of interest as keys instead of having 10000's of objects that represent units (one for each tile).

When a unit moves from one place to another, you remove that unit from the collection stored under the key [x, y] or Tile XYZ and add it under the key [xNew, yNew] or Tile someOtherNiftyTile. You definitely don't delete and recreate, simply use list manipulation. If the old position is completely vacant now (empty list of units), you could delete the old key from the hash completely and that empty list as well; you could also put that list in a pool and simply remove the key.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks man, this seems like exactly what I wanted and what would be the ultimate solution to this kind of thing. Thank you very much it's much appreciated. \$\endgroup\$
    – Einhärjar
    Oct 22, 2014 at 9:24
  • \$\begingroup\$ @Einhärjar - You're very welcome. :) \$\endgroup\$
    – AturSams
    Oct 22, 2014 at 11:38

You must log in to answer this question.

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