I am developing a 2D Metroidvania which consists of a series of interconnected levels that can be revisited.
Each level is represented by a Tiled TMX file in which I have specified where various objects of different sprite classes spawn (for example, enemies, pickups, levers, etc.). When starting a new game, loading a saved game, or changing a level, my game loop runs through the appropriate TMX file and generates all the objects in that level.
I handle level changes in the following way: If the
Player object intersects a
Portal object, a
change_map() method is called which loads a new map (the one associated with the intersected portal) and positions the player at the appropriate position on the new map.
Some of my objects have states which I would like to be persistent through level changes and saving and quitting the game. For example, if a player unlocks a door and the state attribute of the door is set to "open," I would like the door to be open when the player returns. I want something similar for levers, which can be set to left or right, and various other objects. Further, the player will sometimes have collected items which I do not want to respawn when the player revisits the area.
My question is thus how can I handle this kind of persistence?
I am working in Python, although I think you can abstract away from that.