-2
\$\begingroup\$

I am making a simple tile based game. The tiles are Ground and Wall. The entities (dynamic objects) are Box and Player. I am storing tiles as 2d array of enum variables and both Box and Player has own class that extends abstract Entity. Box are saved as list. While doing my level editor I have faced with several problems like check if tile already has some entity in it, is tile solid or soft and so on. What approach is better: to store tiles like entities (each tile in own object that contains it's own position and possibly contains link to an entity and objects are saved as list) or like 2d array and always cycle through it to make some check?

\$\endgroup\$
  • \$\begingroup\$ Depends on details of your game. You got millions of tiles? \$\endgroup\$ – Almo Feb 23 '16 at 18:45
  • \$\begingroup\$ Nope, there is at most 50x50 map but I prefer to know a good approach in most cases. \$\endgroup\$ – John Smith Feb 23 '16 at 18:51
  • 1
    \$\begingroup\$ Which is off topic for the site. The question is too broad. \$\endgroup\$ – Almo Feb 23 '16 at 18:56
3
\$\begingroup\$

There is never one best method. Only the method which works best for you and your game.

But most tile-oriented game engines I know use a 2d array of integer values for each static map layer where each entry represents the tile-type at a given tile-coordinate pair. When your tiles have more properties than can be expressed with a single ID value, this 2d array can also hold a whole structure or object per tile with all the relevant information. This can also include a reference to the mobile object which occupies it (or the list of occupying entities if your game mechanics require that) .

If tiles do not manage their occupants, then mobile objects are usually stored in a simple list which is ordered by the desired drawing order. If you have a very (very!) large number of mobile objects, you might instead use a BSP tree so you can quickly get all objects on a point or in an area.

\$\endgroup\$
  • \$\begingroup\$ So cycling through all boxes to check if some is at specified tile or no is not so bad approach? \$\endgroup\$ – John Smith Feb 23 '16 at 18:53
  • \$\begingroup\$ @JohnSmith It is a possible approach. Whether it's a good or a bad approach depends on how many boxes you have and how often per game-tick you perform this check. \$\endgroup\$ – Philipp Feb 23 '16 at 19:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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