Character achievements - UQcsse3200/2024-studio-1 GitHub Wiki
Overview
The achievements feature allows players to earn certain "badges" when they meet certain requirements. Whether achievements have been won or not, is detected in the PlayerAchievementComponent
. If an achievement has been met, a small display will alert the player of the achievement won. This feature also keeps track of the achievements won by the player, and thus, the player cannot win the same achievement twice throughout the game, or even if they exit the game and return back in.
Key Features of the PlayerAchievementComponent
Saving achievements
In the initial initialization method, "achievements" is defined to be an array consisting of a HashMap<String, String>, which maps the name of the achievement to the icon associated with the badge. This HashMap is read in from the "config/achievements.json" which is stored in an external location, or if it does not exist externally, is read in from the local location. Any new achievements won are added to this new "achievements" array, which is then written to the external location to keep it up to date.
Event Handling
Listens for particular events that have occurred throughout the game and calls a method to determine if requirements for an achievement have been met. Some events that a listener has been added to include:
- "updateSpeedPercentage" (triggered whenever a new energy drink has been picked up)
- "defeatedEnemy" (triggered whenever an enemy has been killed)
- "addToInventory" (triggered whenever a collectible has been added to the inventory)
- "itemUsed" (triggered whenever an item from the inventory has been used)
- "updateCoins" (triggered whenever the player has scored additional coins, and the UI element of the coins is updated)
Determining achievements
-Some achievements that can be won include...
- Collecting all three types of energy drink items
- Defeating 1, 10, 100 and 1000 enemies (respectively)
- Scoring 100, 1000 and 10000 points (respectively)
- Obtaining a collectible item for the first time
- Using an item for the first time
Relevant Classes
- PlayerAchievementComponent.java: Responsible for handling the main functionality of this feature
- Achievement.Json: stores all the achievements gained.
Usage Instructions
Adding a new achievement
- A new achievement can be added using the
addAchievement()
method. - This automatically checks to see if the achievement has already been won, and if not, adds to the "achievements" list
- This method also handles updates to the external location, as well as the UI display
Class usage
Entity player = new Entity();
player.addComponent(new AchievementComponent);
UML
Sequence Diagram
Testing
This feature can be tested manually by observing the display shown when one of the listed achievements is won. To reset the list of achievements won (so that the same achievement can be tested more than once), use the resetAchievements()
method.