I'm writing a game in Flash (player 10) and need to come up with a good way to manage the list of objects/entities/actors in the game (player character, obstacles, enemies, etc.). It has these requirements:
- Iterable
- Objects addable and removable while iterating.
- Argument to remove() function would be the object to remove, not an index.
- [optional] Objects can be assigned a name, and retrieved using that.
- [optional] Sortable, so some objects get updated earlier than others.
How would I implement this?
I was thinking along the lines of an in-memory database table which would contain:
- An array with the objects (records).
- A hashmap (index) which links names to array indices.
- Another hashmap to link objects to their indices (for removing by passing the object to remove).
Removing an entity would make the corresponding record null. Once in a while the array needs to be compacted (empty spaces removed) or it will grow too big. This requires rebuilding the hashmap to point to the correct records (but can that be done efficiently).
Thoughts? Would it perform well? And how to make the sortable and add/remove while iterating parts?