Skip to content

Commit

Permalink
Added lose button and implemented lose screen
Browse files Browse the repository at this point in the history
Implemented LosingScreen class and a lose button on MainGameScreen for demonstration purposes until lose game state is implemented
  • Loading branch information
SonjaMcNeilly committed Sep 10, 2023
1 parent 46a3c0a commit 4a95dfe
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 4 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/core/assets/images/lose-screen/lose-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion source/core/src/main/com/csse3200/game/GdxGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.files.UserSettings;
import com.csse3200.game.screens.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -74,13 +75,15 @@ private Screen newScreen(ScreenType screenType) {
return new StoryScreen(this);
case LEVEL_SELECT:
return new LevelSelectScreen(this);
case LOSING_SCREEN:
return new LosingScreen(this);
default:
return null;
}
}

public enum ScreenType {
MAIN_MENU, MAIN_GAME, SETTINGS, STORY_SCREEN, LEVEL_SELECT
MAIN_MENU, MAIN_GAME, SETTINGS, STORY_SCREEN, LEVEL_SELECT, LOSING_SCREEN
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public MainGameActions(GdxGame game) {
@Override
public void create() {
entity.getEvents().addListener("exit", this::onExit);
entity.getEvents().addListener("lose", this::onLose);
}

/**
Expand All @@ -29,4 +30,8 @@ private void onExit() {
logger.info("Exiting main game screen");
game.setScreen(GdxGame.ScreenType.MAIN_MENU);
}

private void onLose() {
game.setScreen(GdxGame.ScreenType.LOSING_SCREEN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.csse3200.game.components.maingame;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.csse3200.game.ui.UIComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Displays a button to exit the Main Game screen to the Main Menu screen.
*/
public class MainGameLoseDisplay extends UIComponent {
private static final Logger logger = LoggerFactory.getLogger(MainGameExitDisplay.class);
private static final float Z_INDEX = 2f;
private Table table;

@Override
public void create() {
super.create();
addActors();
}

private void addActors() {
table = new Table();
table.top().right();
table.setFillParent(true);

TextButton mainMenuBtn = new TextButton("Lose", skin);

// Triggers an event when the button is pressed.
mainMenuBtn.addListener(
new ChangeListener() {
@Override
public void changed(ChangeEvent changeEvent, Actor actor) {
logger.debug("Quit button clicked");
entity.getEvents().trigger("lose");
}
});

table.add(mainMenuBtn).padTop(-100).padBottom(-500);

stage.addActor(table);
}

@Override
public void draw(SpriteBatch batch) {
// draw is handled by the stage
}

@Override
public float getZIndex() {
return Z_INDEX;
}

@Override
public void dispose() {
table.clear();
super.dispose();
}
}

109 changes: 109 additions & 0 deletions source/core/src/main/com/csse3200/game/screens/LosingScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.csse3200.game.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.csse3200.game.GdxGame;
import com.csse3200.game.screens.text.AnimatedText;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LosingScreen extends ScreenAdapter {
private final GdxGame game;
private SpriteBatch batch;
private Texture introImage;
private Sprite introSprite;

private static final String TEXTURE = "planets/background.png";
private static final String INTRO_TEXT = """
The aliens gained control. You lose!
""";

private BitmapFont font;
private AnimatedText text;
private Stage stage;
private TextButton exitButton;
private TextButton mainMenuButton;
private TextButton playAgainButton;

public LosingScreen(GdxGame game) {
this.game = game;
font = new BitmapFont();
text = new AnimatedText(INTRO_TEXT, font, 0.05f);
font.getData().setScale(2, 2);
}

@Override
public void show() {
batch = new SpriteBatch();
introImage = new Texture(TEXTURE);
introSprite = new Sprite(introImage);
introSprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);

Skin skin = new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json"));
exitButton = new TextButton("Exit Game", skin);
exitButton.addListener(new ClickListener(){
public void clicked(InputEvent even, float x, float y) {
game.exit();
}
});
mainMenuButton = new TextButton("Back to Main Menu", skin);
mainMenuButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(GdxGame.ScreenType.MAIN_MENU);
}

});

playAgainButton = new TextButton("Play Again", skin);
playAgainButton.addListener(new ClickListener() {
public void clicked(InputEvent even, float x, float y) {
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
}
});

Table table = new Table();
table.setFillParent(true);
table.add(exitButton).padTop(-100).row();
table.add(mainMenuButton).padTop(-200).row();
table.add(playAgainButton).padTop(-300).row();
stage.addActor(table);
}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin();
introSprite.draw(batch);
text.update();
text.draw(batch, 730, 800); // Adjust the position
batch.end();

stage.draw();
}

@Override
public void dispose() {
batch.dispose();
introImage.dispose();
stage.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.csse3200.game.areas.ForestGameArea;
import com.csse3200.game.areas.terrain.TerrainFactory;
import com.csse3200.game.components.maingame.MainGameActions;
import com.csse3200.game.components.maingame.MainGameLoseDisplay;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.entities.factories.PlayerFactory;
Expand Down Expand Up @@ -67,7 +68,6 @@ public class MainGameScreen extends ScreenAdapter {
public static int viewportHeight= screenHeight;



private OrthographicCamera camera;
private SpriteBatch batch;

Expand Down Expand Up @@ -117,7 +117,6 @@ public MainGameScreen(GdxGame game) {
ForestGameArea forestGameArea = new ForestGameArea(terrainFactory);
forestGameArea.create();
}

@Override
public void render(float delta) {
physicsEngine.update();
Expand All @@ -128,7 +127,6 @@ public void render(float delta) {
batch.draw(backgroundTexture, 0, 0, viewportWidth, viewportHeight);
batch.end();


renderer.render();
stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
stage.draw();
Expand Down Expand Up @@ -196,6 +194,7 @@ private void createUI() {
.addComponent(new PerformanceDisplay())
.addComponent(new MainGameActions(this.game))
.addComponent(new MainGameExitDisplay())
.addComponent(new MainGameLoseDisplay())
.addComponent(new Terminal())
.addComponent(inputComponent)
.addComponent(new TerminalDisplay());
Expand Down

0 comments on commit 4a95dfe

Please sign in to comment.