Difficulty selection from main menu - UQcsse3200/2024-studio-1 GitHub Wiki
There are three buttons on the main menu screen to set a difficulty: Easy, Medium and Hard. Test is also given if you want to mess around in a TestGameArea
instead of a MainGameArea
.
Main menu buttons
The buttons that start the game (called "action"
buttons in the skin file) are coloured differently to the remaining buttons to make it clear that these buttons have a different function.
Setting the difficulty
The GameOptions
class will contain the current game options (what the player chooses before starting the game, i.e. difficulty, chosen player and whether to load from a save file or start afresh. Not to be confused with user settings e.g. volume).
public enum Difficulty {
EASY(1f), MEDIUM(0.75f), HARD(0.5f), TEST(1f);
Difficulty(float multiplier) {...}
private float multiplier;
public float getMultiplier() {...}
@Override
public String toString() {...}
}
multiplier
is a value associated with each difficulty that changes player attributes to make the game easier or harder (see below).
The game options are added as a field in GdxGame
.
Setting game options
When one of the difficulty buttons is clicked on, the "player_select"
event is triggered with difficulty
and shouldLoad
(whether to start the game by loading from a save file) as parameters.
public void changed(ChangeEvent event, Actor actor) {
logger.debug("{} difficulty button clicked", difficulty.toString());
boolean shouldLoad = canLoad && shouldLoadBtn.isChecked();
entity.getEvents().trigger(
"player_select", difficulty, shouldLoad);
}
In MainMenuActions
, the gameOptions
field of the game
is set to the new options, then the game begins.
private void onStart(GameOptions options) {
logger.info("Start game");
game.gameOptions = options;
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
}
The effect of difficulty
When the player is created in PlayerFactory
, the PlayerConfig
instance is adjusted based on the difficulty selection:
public Entity createPlayer(String player, Difficulty difficulty) {
LoadPlayer loader = new LoadPlayer();
PlayerConfig config = options.get(player);
config.adjustForDifficulty(difficulty);
return loader.createPlayer(config);
}
This has the effect of multiplying the player's health and speed by the multiplier
value, potentially decreasing their health and speed:
public void adjustForDifficulty(Difficulty difficulty) {
float multiplier = difficulty.getMultiplier();
health = (int) (health * multiplier);
speed.scl(multiplier); // multiply speed, a vector, by a scalar
}
The higher the difficulty, the lower the multiplier and the lower the resulting health and speed. This is how the game becomes harder.