I've got a 2D map, on which entities interact.
Should an entity's position be kept on the map object, or on the entity object itself, or both? Why? What does decide where a property is kept? What questions am I supposed to ask myself when deciding that?
Some use cases are:
- checking what entities are on a given tile (performant if the entities are kept inside, say, an array on the map object, unperformant if the position is kept on the entity)
- checking where a given entity is (easy if the position is on the entity instead - the above reversed)
My current thoughts are:
- the position 'logically' should be kept on the map, because a position is something that just exists within an entity. An entity needs to live in some kind of a spatial space to have a position, and map is such a space.
- there should be multiple data structures to ease various operations. For example, if entities are kept inside a 2D array, finding where a given entity is would be a linear operation (all cells must be iterated), and, say, a hashmap would bring it down to const time.
move
method (let's say, Entities have some 'abilities', and one of them is "jump 2 tiles forward"), as well as the Map might have amove
method to move Entities around if needed? This will require every Entity have a reference to the Map, and the Map to refence all the Entities somehow (to update the position in both places). Is this correct? Aren't cyclic references a bad pattern? – Sun Apr 09 '21 at 15:58move()
only, it doesn't matter on which one. If you absolutely must have it on both, that would mean they are actually both the same "unit". There are ways to do this too, like having the entity be an inner class of the map. – Robert Bräutigam Apr 10 '21 at 10:11