Loading State of the character - UQcsse3200/2024-studio-1 GitHub Wiki
Objective
The objective of this task is to implement a reliable system to load and restore the player's state from a saved JSON file. This ensures that players can resume their progress exactly where they left off, including their health, collected items, equipped weapons, and other relevant stats. The loading system is crucial for providing a seamless gaming experience, allowing for continuous gameplay across different sessions.
Player Factory class
package com.csse3200.game.entities.factories;
Overview
The loading system consists of key components that work together to reconstruct the player's state from the saved data:
PlayerConfig: The data structure that holds the player's saved state, which is deserialized from a JSON file.
PlayerFactory: Responsible for creating the player entity and initializing it with the loaded state.
FileLoader: Handles the reading and parsing of the JSON file to retrieve the player's saved data.
These components are designed to be modular, facilitating easy updates and maintenance as the game evolves.
Components Breakdown
1. PlayerConfig Class
Purpose: This class represents the player's state as stored in the JSON file. It includes attributes such as health, attack strength, collected items, and equipped weapons.
Key Attributes:
-
health (int): The player's health points.
-
baseAttack (int): The player's base attack power.
-
items (String[]): A list of items collected by the player.
-
melee (String): The currently equipped melee weapon.
-
ranged (String): The currently equipped ranged weapon.
-
equipped (String): The currently active weapon type ("melee" or "ranged").
2. PlayerFactory Class
Purpose: The PlayerFactory is responsible for creating a player entity and initializing it using the loaded state from the PlayerConfig object.
Functionality:
-
Load Assets: Ensures that all necessary assets (e.g., textures, animations) are loaded before the player entity is created. Initialize Player: Sets up the player entity with components such as health, inventory, and equipped weapons based on the loaded data. Error Handling: Handles cases where certain data might be missing or invalid, providing default values to ensure the player entity is always created successfully. Implementation Steps:
-
Load Config: Reads the PlayerConfig from the JSON file.
-
Initialize Inventory: Adds items to the player's inventory and equips weapons based on the loaded configuration.
-
Create Entity: Assembles the player entity, attaching all relevant components (e.g., physics, rendering, input).
-
Set Attributes: Applies the loaded health, attack, and other stats to the player entity.
3. FileLoader Utility
Purpose: Manages the reading of the player's saved state from the JSON file.
Functionality: Read JSON File: Deserializes the JSON data into a PlayerConfig object.
-
Handle File Path: Manages the file path where the player's saved state is stored (e.g., configs/player_save.json).
-
Error Handling: Includes checks to ensure the file is read successfully and handles scenarios where the file might be missing or corrupted.
Usage
Loading the Player's State To load the player's state during game initialization, the following steps are performed:
- Instantiate PlayerFactory: The PlayerFactory is used to create the player entity.
- Load Player State: The saved state is loaded from the JSON file using the FileLoader.
- Initialize Player: The PlayerFactory initializes the player entity with the loaded state.
Example:
Entity player = PlayerFactory.createPlayer();
This code will create a player entity that reflects the saved state stored in player_save.json.
UML Diagram
Testing and Validation
A testing planning is created to ensure the functionality of the loading system. Testing Planning for Loading system describes the steps taken and scenarios that were considered while testing for this component. Testing plan