Saving Loading Feature - UQcsse3200/2024-studio-1 GitHub Wiki
Introduction
The Saving-Loading feature provides user the ability to save the current game process and continue playing at another time in the future.
Overview
The "Save" button and the game saving functionality can be found in the pause menu:
The "Load" button can be found in main menu:
Technical Information
The "Save" button and save game functionality are stored in MainGameExitDisplay.java
.
The saveGame() method aims to save the maps and current state (e.g. Health) of the main player entity in the game. It retrieves the player's entity, triggers events to save the player's location and map data (from team 6 - maps), and uses a service to persist the player's state (from team 5 - player). This function primarily interacts with the game's entity system and event system to manage and store the player's state into JSON files.
public void saveGame() {
Array<EntityCoordinates> entities = new Array<>();
for (Entity entity : ServiceLocator.getEntityService().getEntities()) {
if (entity.getName().equals("Main Player")) {
player = entity;
SavePlayerService savePlayerService = new SavePlayerService();
savePlayerService.savePlayerState(player);
player.getEvents().trigger("saveMapLocation");
player.getEvents().trigger("saveMapData");
System.out.println("Saved Successfully");
}
}
}
The Load button in the MainMenuDisplay component provides the player with the option to load a previously saved game state. This button is displayed as a checkbox labeled "Load from save file" and allows the player to decide whether they want to continue from where they left off (using a saved game state) or start a new game session.
-
Load Button Functionality Load Button (CheckBox shouldLoadBtn) - The load button is implemented as a checkbox in the main menu, allowing players to toggle whether they wish to load a previous save file or start a new game. This checkbox is only displayed if a valid save file is detected on the player's local system.
-
loadFilesExist() Method - This method checks whether the necessary save files for the game exist in the expected file paths. It iterates over the SAVE_PATHS and checks if the local files corresponding to the player's saved state are present.
-
Loading a Game - When the player selects a difficulty option in the main menu, the game checks if the "Load from save file" checkbox is selected. If it is checked, the game loads the player's previously saved progress.