Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added source/core/assets/images/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions source/core/src/main/com/csse3200/game/GdxGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.csse3200.game.screens.MainMenuScreen;
import com.csse3200.game.screens.SettingsScreen;
import com.csse3200.game.screens.SaveSelectionScreen;
import com.csse3200.game.services.GameStateService;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,6 +31,9 @@ public void create() {
// Sets background to light yellow
Gdx.gl.glClearColor(248f/255f, 249/255f, 178/255f, 1);

// instantiate game state
ServiceLocator.registerGameStateService(new GameStateService());

setScreen(ScreenType.MAIN_MENU);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void create() {
entity.getEvents().addListener("resume", this::onResume);
entity.getEvents().addListener("openSettings", this::onOpenSettings);
entity.getEvents().addListener("quitToMenu", this::onQuitToMenu);
entity.getEvents().addListener("awardStars", this::awardStars);
}

private boolean isPaused = false;
Expand Down Expand Up @@ -101,4 +102,16 @@ private void onSave() {
logger.error("Error during manual save", e);
}
}

/**
* Awards stars when won
*/
private void awardStars(int amount) {
if ((ServiceLocator.getGameStateService()) == null) {
logger.error("GameStateService is missing; register in Gdx.game");
return;
}
ServiceLocator.getGameStateService().updateStars(amount);
logger.info("Awarded {} star. Total = {}", amount, ServiceLocator.getGameStateService().getStars());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void addActors() {
@Override
public void changed(ChangeEvent changeEvent, Actor actor) {
logger.debug("Win button clicked");
entity.getEvents().trigger("awardStars", 1);
entity.getEvents().trigger("gamewin");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.csse3200.game.areas.ForestGameArea;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.csse3200.game.services.GameStateService;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.ui.UIComponent;
import org.slf4j.Logger;
Expand Down Expand Up @@ -52,7 +51,20 @@ private void addActors() {
TextButton loadBtn = new TextButton("Continue", customButtonStyle);
TextButton settingsBtn = new TextButton("Settings", customButtonStyle);
TextButton exitBtn = new TextButton("Exit", customButtonStyle);


// stars display
Image starImage = new Image(
ServiceLocator.getResourceService().getAsset(
"images/star.png",
Texture.class
)
);
Label starsLabel = new Label(
Integer.toString(ServiceLocator.getGameStateService().getStars()),
skin,
"large"
);

// 设置按钮大小
float buttonWidth = 200f;
float buttonHeight = 50f;
Expand All @@ -68,6 +80,7 @@ private void addActors() {
@Override
public void changed(ChangeEvent changeEvent, Actor actor) {
logger.debug("Start button clicked");
ServiceLocator.registerGameStateService(new GameStateService());
entity.getEvents().trigger("start");
}
});
Expand Down Expand Up @@ -101,7 +114,13 @@ public void changed(ChangeEvent changeEvent, Actor actor) {
});


table.add().expandY().row();
table.add().expandY().row();
HorizontalGroup group = new HorizontalGroup();
group.space(5);
group.addActor(starImage);
group.addActor(starsLabel);
table.add(group);
table.row();
table.add(startBtn).size(buttonWidth, buttonHeight).padTop(50f);
table.row();
table.add(loadBtn).size(buttonWidth, buttonHeight).padTop(20f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.csse3200.game.ui.terminal.TerminalDisplay;
import com.csse3200.game.components.maingame.MainGameExitDisplay;
import com.csse3200.game.components.maingame.MainGameOver;
import com.csse3200.game.components.maingame.MainGameWin;
import com.csse3200.game.components.gamearea.PerformanceDisplay;
import com.csse3200.game.services.SaveGameService;
import com.csse3200.game.components.maingame.PauseMenuDisplay;
Expand Down Expand Up @@ -201,6 +202,7 @@ private Entity createUI() {
.addComponent(new PauseInputComponent())
.addComponent(new MainGameExitDisplay())
.addComponent(new MainGameOver())
.addComponent(new MainGameWin())
.addComponent(new Terminal())
.addComponent(inputComponent)
.addComponent(new TerminalDisplay());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class MainMenuScreen extends ScreenAdapter {
private final Renderer renderer;
private static final String[] mainMenuTextures = {
"images/main_menu_background.png",
"images/Main_Menu_Button_Background.png"
"images/Main_Menu_Button_Background.png",
"images/star.png"
};


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.csse3200.game.services;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Service that handles persistent state within the game runtime
* Stores persistent data such as stars, unlocks, and level progress
*/
public class GameStateService {
private static final Logger logger = LoggerFactory.getLogger(GameStateService.class);

private int stars;

public GameStateService() {
// should load from save file later
logger.info("Loading GameStateService");
stars = 0;
}

/**
* Gets the current number of stars
* @return current number of stars
*/
public int getStars() {
return stars;
}

/**
* Sets the current number of stars to the given number
* @param newStars new number of stars
*/
public void setStars(int newStars) {
stars = newStars;
}

/**
* Increments the current number of stars by the given number
* @param increment the number of stars to increment by
*/
public void updateStars(int increment) {
stars += increment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ServiceLocator {
private static InputService inputService;
private static ResourceService resourceService;
private static GdxGame gameService;
private static GameStateService gameStateService;

private static com.csse3200.game.services.leaderboard.LeaderboardService leaderboardService;

Expand Down Expand Up @@ -56,6 +57,8 @@ public static GdxGame getGameService() {
return gameService;
}

public static GameStateService getGameStateService() { return gameStateService; }

public static void registerEntityService(EntityService service) {
logger.debug("Registering entity service {}", service);
entityService = service;
Expand Down Expand Up @@ -101,6 +104,11 @@ public static void registerGameService(GdxGame source) {
gameService = source;
}

public static void registerGameStateService(GameStateService source) {
logger.debug("Registering game service {}", source);
gameStateService = source;
}

public static void clear() {
entityService = null;
renderService = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.csse3200.game.services;

import com.csse3200.game.extensions.GameExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(GameExtension.class)
class GameStateServiceTest {
GameStateService gameState;

@BeforeEach
void beforeAll() {
gameState = new GameStateService();
}

@Test
void shouldGet() {
assert(gameState.getStars() == 0);
}

@Test
void shouldSet() {
gameState.setStars(345);
assert(gameState.getStars() == 345);
}

@Test
void shouldIncrement() {
gameState.updateStars(5);
assert(gameState.getStars() == 5);
}

@Test
void multipleIncrement() {
for (int i = 0; i < 10; i++) {
gameState.updateStars(1);
assert(gameState.getStars() == i+1);
}
}
}