I achieved a big step in my game yesterday! I am now able to change the current map in the game. Before doing the real code, i have modified the MapContext object with all the initialization process inside one method. The RuntimeContext object can call this method during map switch. It is now possible also to reload a MapContext, updating only what is necessary when the map is used as the current one. The RuntimeContext object is able to load multiple MapContexts and switch between them when the player move to another room.
The current result is pretty rough but it is working !
To implement the code, I have defined in each maps some zones tracked by the game. When the hero is entering one, the game detect that the player wants to change the current room. The hero sprite will be “teleported” in the zone linked to the incoming room from the another map. I have called this zone in my code a “map change”. This could be a door, a passage or some stairs. In the map editor, those areas are invisible rectangular object defined in the “other” layer. Each zone has a name and some attributes to handle the logic.
The same name should be used in the two maps and should be unique for the current map otherwise when entering the zone, the game will not be able to know which zone with the name to use to teleport the hero.
In the MapChange object definition, I must define the path to the file holding the next room. I need also another parameter “direction”. Why? When the player is moving his hero on this zone, it will be teleported in the linked zone on the other map. If i just let the sprite there, the game will detect a “map change” zone and will teleport the sprite again on the other map, with an infinite loop… The solution (i have found) is to make the hero not controllable and move automatically the sprite from the exit zone to the playable zone on a direction. The exit zone detection is disabled during automatic moves and the infinite loop disappears.
As you can see in this definition, when entering the zone from r1r2 area, the sprite will be move automatically down inside the playable area.
On the second map, i have defined another MapChange area with the same name.
This time, when the hero is coming from r1r2, the sprite will be move automatically up into the playable zone.
What need to be done to polish this? A visual transition when changing room and all the UI stuff like a door, or a stair. Some sounds could be good too. But i am postponing this work for later.