I’m writing a game where a large number of objects will have “area effects” over a region of a tiled 2D map.
Required features:
- Several of these area effects may overlap and affect the same tile
- It must be possible to very efficiently access the list of effects for any given tile
- The area effects can have arbitrary shapes but will usually be of the form “up to X tiles distance from the object causing the effect” where X is a small integer, typically 1-10
- The area effects will change frequently, e.g. as objects are moved to different locations on the map
- Maps could be potentially large (e.g. 1000*1000 tiles)
What data structure would work best for this?
Providing you really do have a lot of area effects happening simultaneously, and that they will have arbitrary shapes, I’d do it this way:
stored in a global list of effects
(not necessarily a global variable,
just something that applies to the
whole game or the current game-map)
it affects, and stores a list of those tiles against the effect
notified of the new effect, and
stores a reference back to it in a
per-tile list (in C++ I’d use a
std::vector for this, something with
contiguous storage, not a linked
list)
the interested tiles and removing references to it, before destroying it
the references as above, performing the change calculations,
then re-attaching references in the tiles now affected
your entire map and verifies that the list of tiles in the effect
exactly matches the tiles in the map that reference it.