Selection for Players - UQcsse3200/2024-studio-1 GitHub Wiki

Objective

This game has two main characters for players to choose from. Both have the same default configuration, different in animations and looks. When players start the game, it spawns a window to ask to select the main character that they want to play. Team 5 is responsible for creating a method, which can return two characters for Menu team to display on the window when users want to start the game. Below is the instruction how to use the class PlayerSelection.

PlayerSelection class

package com.csse3200.game.entities;

Overview

The PlayerSelection class is responsible for managing the creation and selection of multiple player entities within the game. It works by utilizing the PlayerFactory class to load and initialize players from different saved states (JSON files). The primary function of this class is to allow the game to create two distinct player entities from separate configurations, which players can select or use in multiplayer scenarios.

Components Breakdown

1. PlayerSelection Class

  • Purpose: This class facilitates the creation of multiple player entities based on configurations provided in JSON files. It coordinates the instantiation of two player entities by utilizing the PlayerFactory to load saved player states from different files. This functionality is especially useful in multiplayer setups where different players may have distinct configurations.
  • Key Functionality:
    • PlayerFactory Initialization: Initializes PlayerFactory instances to load the player state from JSON files.
    • Entity Creation: Creates two player entities from different saved states.
    • Player Management: Returns a list containing the two created player entities, which can be used for player selection, rendering, or interaction within the game.

2. PlayerFactory Class

  • The PlayerSelection class interacts with the PlayerFactory class to create player entities. Each PlayerFactory instance is associated with a different configuration file (JSON) that stores the saved state of a specific player.
  • Usage in PlayerSelection:
    • PlayerFactory is instantiated twice, once for each player.
    • The createPlayer() method is called on each factory to generate the player entities.

3. Key methods and attributes

Attributes:

  • PlayerFactory playerFactory1: The PlayerFactory responsible for loading and initializing the first player entity.
  • PlayerFactory playerFactory2: The PlayerFactory responsible for loading and initializing the second player entity.

Methods:

  • List<Entity> createTwoPlayers():
    • This method is responsible for creating two player entities by reading from two different JSON configuration files (configs/player.json and * - configs/player_2.json).
    • It initializes two instances of PlayerFactory, one for each player, and calls createPlayer() on both factories to generate the corresponding * - player entities.
    • It then adds both player entities to a list and returns this list, which can be used in the game to allow player selection or initialization.

#Example Usage

PlayerSelection playerSelection = new PlayerSelection;
List<Entity> players = playerSelection.createTwoPlayers();

This call create a list of two entities which are players for users to select.

Entity player = players.get(0) 

This line retrieves the first player from the players list using the get(0) method, which accesses the element at index 0 (lists in Java are 0-indexed). This means that player will now hold the reference to the first Entity in the list of players, which can then be used for further game logic or actions.

UML Diagram

LoadPlayer

Testing and Validation

Refer to Testing Plan for more information about how this class is going to be validated

Sequential Diagram

PlayerSelection_createTwoPlayers

⚠️ **GitHub.com Fallback** ⚠️