Scoring System - UQcsse3200/2024-studio-1 GitHub Wiki

Overview

The CoinsComponent is designed to manage the coin collection and spending mechanics in the game. It tracks of player's attacks and when player has successfully killed an NPC, it adds their strength as coins which can be be used later in the game. Moreover, this system also updates the coins on UI accordingly.

Rationale

The implementation of the CoinsComponent follows a component-based architecture, allowing for modular and reusable components across different entities. This design enables easier maintenance and scalability of the game code. By decoupling the coin management from the player entity, it is convenient to extend or modify the coin system without affecting other parts of the codebase.

Key Features

  • Coin Component: Tracks the number of coins owned by the player, allowing for adding and spending of coins.
  • Event Handling: Listens for coin collection events triggered by other components, particularly NPCs, to update the player's coin count in real time.
  • Integration with Inventory: Links with the player's inventory to ensure seamless updates and interactions.

Relevant Classes

  • CoinsComponent.java: Responsible for handling the coins, this includes adding coins, setting coins to a value and spending coins

  • PlayerCoinDisplay: This UI component displays the current coin count on the screen. It listens out for any change in coin amount and updates the label whenever it changes, ensuring players can see their current currency at a glance.

  • NPCDeathHandler: This component triggers coin collection events when an NPC dies based on the NPC's strength, which directly affects how many coins are awarded to the player.

  • NPCConfigs: This file was modified to store the strength of different NPCs. This is important as NPCs with different strength will award different scores.

  • InventoryComponent: The CoinsComponent relies on the InventoryComponent to access the player's events and trigger updates related to coin management.

Usage Instructions

Adding CoinsComponent to an Entity

To use the CoinsComponent in your game, you must add it to the player entity during its creation. Here's an example:

import com.csse3200.game.components.player.inventory.CoinsComponent;
import com.csse3200.game.components.player.InventoryComponent;

// Inside your player creation method
public Entity createPlayer(PlayerConfig config) {
    Entity player = new Entity();
    InventoryComponent inventoryComponent = new InventoryComponent();
    player.addComponent(inventoryComponent);
    
    CoinsComponent coinsComponent = new CoinsComponent(inventoryComponent.getInventory());
    player.addComponent(coinsComponent); // adding scoring system
    player.addComponent(new PlayerCoinDisplay(coinsComponent)); // displaying the coins on UI
    // Other player setup...

    return player;
}

Accessing Coin Data

To access or modify the player's coin count, you can use the following methods provided by the CoinsComponent:

  • Get Coins: Retrieve the current coin count.

    int currentCoins = coinsComponent.getCoins();
    
  • Add Coins: Add a specified amount of coins.

    coinsComponent.addCoins(5); // Adds 5 coins
    
  • Spend Coins: Spend a specified amount of coins, ensuring the player has enough.

    coinsComponent.spend(3); // Attempts to spend 3 coins
    
  • Set Coins: Directly set the coin count, ensuring it does not go below zero.

    coinsComponent.setCoins(10); // Sets coins to 10
    

On Display

the coins appear on the top of the screen like: image

Reference for the coins image: https://pearlyarts.com/product/coin-clipart-a/

UML Diagram

image

Sequence Diagram

Testing

Please refer to the following link to view the test plans for Scoring System: Test Plans for Scoring System

Conclusion

The CoinsComponent is a crucial part of the game's economy system, enabling players to collect, spend, and manage their currency effectively. By leveraging event-driven programming and a component-based architecture, it provides a flexible and maintainable approach to coin management in your game.