Map Saving and Loading - UQcsse3200/2024-studio-1 GitHub Wiki
The Map Saving and Loading feature enables the game to maintain the state of the game world between sessions. This includes preserving the map layout, room configurations, player locations, and other critical game data. The system ensures that players can seamlessly resume gameplay without losing progress, no matter where they left off.
The following components play a crucial role in this feature:
-
MapLoadConfig
: Used to facilitate loading of Map structure data, and player location data, that would've been previously saved by player usingFileLoader
readClass method. It also saves shop room data such as items already bought. -
MainGameLevelFactory
: Creates and manages rooms during gameplay and exports the rooms that have been completed by the player as a list of strings as well as the seed number of the map to be saved in a JSON. Further more it also manages the loading of map data by bypassing the normal map creation method through setting of the seed and rooms completed before hand using the should load boolean overloaded into the class throughMainGameScreen
. -
MainGameExitDisplay
: Contains implementation for the save game button as a method, which contains the triggers for saving the current state of the game (map data and player locations). -
LoadGameScreen
: Maingate screen and loadGameScreen both inherit the game screen class, which is where all the initialisation for the startup of the game happens. If it's a main game screen (new game) you will have values provided from the player select display which will be used to initialise the game. When loading from a previously saved game, the game area and level factory are initialised from the map save json. All the player info is also initialised from the player save.
- public List savedMap : Contains a list of strings the first of which is the seed of the map that was saved and the rest are a list of completed room keys as a list.
- public List roomsCompleted : List of Room keys of the rooms that have been completed by the player in the round
- public List shopRoomItems : new ArrayList<>() : List of strings that contain the items that are currently spawned in shop room when the player saves the game.
- public String currentLevel : String that saves the current level that the player is in.
- public String currentRoom : The current room the player is in.
- public String seed : The current seed of the map.
- public int mapSize : The current size of the map. }
The saving system saves all of the above elements using the following trigger inside Maingameexitdisplay
//exports the rooms and map data into the filePath below after Save button is pressed
player.getEvents().trigger("saveMapData");
Once the map data is saved, The load button from the main menu triggers the loading process. Then the game process the saved files and uses them to initialise the game through the LoadGameScreen
class. Map load can be initialised in LoadGameScreen
in the following way.
//inside the constructor
MapLoadConfig mapLoadConfig = FileLoader.readClass(MapLoadConfig.class,
MAP_SAVE_PATH, FileLoader.Location.EXTERNAL);
LevelFactory levelFactory = new MainGameLevelFactory(true, mapLoadConfig);
new MainGameArea(levelFactory, player, true, mapLoadConfig);
}
Again based on whether load button is pressed the data is either loaded from the save file or makes a new game altogether.
- Start the game by pressing Easy:
- Play the Game and travel kill animals in each room and go through 7 rooms and then press ESC to pause menu and press the save button like so:
- After pressing the save button after going through 3 rooms including the shop room and killing the animals in the rooms the MapSave.json file will look like this:
{
roomsCompleted: [
0_0
2_0
2_1
]
shopRoomItems: [
buff:syringe:buyable
item:targetdummy:buyable
item:medkit:buyable
item:reroll:buyable
item:targetdummy:buyable
]
currentLevel: 0
currentRoom: 2_1
seed: seed
mapSize: 40
}
-
Start up the game and click the load button:
-
This should load up MapSave.json and the player will be inside the completed room that was saved as the current room number when saved (2_1), like so:
1. Use of JSON for Serialization
JSON is chosen as the serialization format due to its readability, ease of use, and widespread support. It allows for straightforward mapping between Java objects and a text-based format, facilitating easy debugging and potential manual editing if necessary.