Death screens for last attack animal - UQcsse3200/2024-studio-1 GitHub Wiki
Overview
The LoseScreenDisplay class is responsible for displaying the "Game Over" or "Losing" screen in the game when the player dies. This screen displays an image of the animal that killed the player and a taunting message from the animal, along with a button to return to the main menu. The menu is now enhanced with the background for each animal.
Example
- Death screen for Bat:
Implementation Logic
private void addActors() {
animals.add("Rat");
animals.add("Bear");
animals.add("Bat");
animals.add("Dog");
animals.add("Snake");
animals.add("Dino");
animals.add("Minotaur");
animals.add("Dragon");
animals.add("Werewolf");
animals.add("Cthulu");
animals.add("Kitsune");
animals.add("Birdman");
animalDeathScreens.add("You thought you were the predator, but turns out you are just prey!");
animalDeathScreens.add("Too slow, too soft. You didn't stand a chance against my claws!");
animalDeathScreens.add("Couldn't see in the dark, huh? My world, my rules. Better luck next time, human!");
animalDeathScreens.add("Fetch? More like fight! Too bad you didn't put up much of one.");
animalDeathScreens.add("Slither, slither... you never saw me coming, and now it is lights out for you!");
animalDeathScreens.add("Extinct? Not me! Guess it is you who is heading for the history books!");
animalDeathScreens.add("Lost in my labyrinth of power, and now you are just another trophy!");
animalDeathScreens.add("Foolish mortal, my fire has forged empires and destroyed heroes. What made you think you could survive?");
animalDeathScreens.add("Ah, the moon's glow reveals the truth: you were never a match for the beast within me!");
animalDeathScreens.add("You are but a fleeting whisper in the vast eternity of my dreams. Insignificant, erased!");
animalDeathScreens.add("Deceived by shadows and tricked by tales, your end came at the hands of my cunning!");
animalDeathScreens.add("From the skies I watched, and with swift wings I struck. Did you really think you could escape my gaze?");
LastAttackAnimal = readClass(String.class, "configs/LastAttack.json", FileLoader.Location.EXTERNAL);
if(!LastAttackAnimal.equals("Unknown")){
int firstSpaceIndex = LastAttackAnimal.indexOf(' ');
if (firstSpaceIndex != -1) { // Check if there is at least one space in the string
LastAttackAnimal = LastAttackAnimal.substring(0, firstSpaceIndex);
}
LastAttackAnimal = capitalizeFirstChar(LastAttackAnimal);
System.out.println(LastAttackAnimal);
}
Label youDied = new Label("You died...", skin, "cutscene");
Label animalName;
Image playerDead;
if(animals.contains(LastAttackAnimal)){
backgroundTexture = new Texture(Gdx.files.internal(backgroundPaths[animals.indexOf(LastAttackAnimal)]));
Image background = new Image(backgroundTexture);
background.setFillParent(true); // Make it fill the screen
stage.addActor(background);
table = new Table();
table.setFillParent(true);
playerDead = new Image(new Texture(Gdx.files.internal(animalImagePaths[animals.indexOf(LastAttackAnimal)])));
animalName = new Label(LastAttackAnimal+ ":", skin, "cutscene");
table.add(playerDead).padTop(Y_PADDING);
table.row();
table.add(animalName).padTop(Y_PADDING);
Label animalLabel = new Label(animalDeathScreens.get(animals.indexOf(LastAttackAnimal)), skin, "cutscene");
table.row();
table.add(animalLabel).padTop(30f);
}
else{
table = new Table();
table.setFillParent(true);
// See assets/images/player/player_asset_citation.txt
playerDead = new Image(
ServiceLocator.getResourceService().getAsset(PLAYER_DEAD, Texture.class)
);
animalName = new Label("", skin, "cutscene");
table.add(playerDead).padTop(Y_PADDING);
table.row();
table.add(animalName).padTop(Y_PADDING);
table.row();
table.add(youDied).padTop(30f);
}
Button exitBtn = new TextButton("Back to menu", skin);
exitBtn.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
logger.debug("Going back to menu from death screen");
game.setScreen(MAIN_MENU);
}
});
table.row();
table.add(exitBtn).padTop(Y_PADDING);
stage.addActor(table);
}
-
Animal Detection: The last attacking animal is read from a JSON file (configs/LastAttack.json) using the readClass() method. Depending on the value of LastAttackAnimal, the corresponding animal image and taunting message are displayed on the screen.
-
UI Elements: The death screen contains: An image of the animal responsible for the player's death and its background. A label showing the name of the animal. A specific taunting message from the animal to the player. A button to return to the main menu, which triggers a screen change to MAIN_MENU.
-
Event Handling: The "Back to menu" button listens for a click event and navigates back to the main menu when triggered. This is achieved through the game.setScreen(MAIN_MENU) call.
-
Resource Management: The dispose() method ensures the UI table is cleared and resources are properly managed when the component is no longer needed.