Skip to content

Commit

Permalink
Implemented the wave timer pausing when the pause menu is opened. ISS…
Browse files Browse the repository at this point in the history
…UE: The wave timer entity isn't present in MainGameScreen, so it can't be passed to the pause menu at present. Fixing this is the next objective for this task.
  • Loading branch information
AlasdairS4698737 committed Sep 27, 2023
1 parent 42179a4 commit e095797
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public class MainGamePauseDisplay extends UIComponent {
private static final Logger logger = LoggerFactory.getLogger(MainGamePauseDisplay.class);
private static final float Z_INDEX = 2f;
private Table table;

private GdxGame game;
private Entity waveSpawner;

public MainGamePauseDisplay(GdxGame screenSwitchHandle) {
public MainGamePauseDisplay(GdxGame screenSwitchHandle, Entity waveSpawner) {
game = screenSwitchHandle;
this.waveSpawner = waveSpawner;
}

@Override
Expand All @@ -45,7 +46,7 @@ private void addActors() {
@Override
public void changed(ChangeEvent changeEvent, Actor actor) {
logger.debug("Pause button clicked");
PauseMenuFactory.createPauseMenu(game);
PauseMenuFactory.createPauseMenu(game, waveSpawner);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
public class PauseMenuTimeStopComponent extends Component {

private Array<Entity> freezeList;
public PauseMenuTimeStopComponent() {
private Entity waveSpawner;

/**
* Initialises the component.
* @param waveSpawner The entity that controls the wave spawning timer.
*/
public PauseMenuTimeStopComponent(Entity waveSpawner) {
this.waveSpawner = waveSpawner;
}

/**
* Handles the pausing of the game entities when the pause menu is made.
*/
@Override
public void create() {
waveSpawner.getEvents().trigger("toggleWaveTimer");
freezeList = ServiceLocator.getEntityService().getEntities();
for (Entity pauseTarget : freezeList) {
if (pauseTarget.getId() != getEntity().getId()) {
Expand All @@ -33,6 +41,7 @@ public void create() {
*/
@Override
public void dispose() {
waveSpawner.getEvents().trigger("toggleWaveTimer");
for (Entity pauseTarget : freezeList) {
pauseTarget.setEnabled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class SpawnWaveTask extends DefaultTask implements PriorityTask {
private final GameTime globalTime;
private long endTime = 0;
private final int SPAWNING_INTERVAL = 10;
private boolean wavesPaused = false;
private long pauseTime = 0;
public SpawnWaveTask() {
this.globalTime = ServiceLocator.getTimeSource();
}
Expand All @@ -25,13 +27,30 @@ public int getPriority() {
public void start() {
super.start();
endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000);
this.owner.getEntity().getEvents().addListener("toggleWaveTimer", this::toggleWaveTimer);
}

@Override
public void update() {
if (globalTime.getTime() >= endTime) {
this.owner.getEntity().getEvents().trigger("spawnWave");
endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000L); // reset end time
if (!wavesPaused) {
if (globalTime.getTime() >= endTime) {
this.owner.getEntity().getEvents().trigger("spawnWave");
endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000L); // reset end time
}
}
}

/**
* Function for pausing the wave timer when the pause menu is opened, and resuming it when the pause menu is closed.
*/
private void toggleWaveTimer() {
if (!wavesPaused) {
pauseTime = globalTime.getTime();
wavesPaused = true;
} else {
// Offsets the next wave spawn by however long the game was paused.
endTime += globalTime.getTime() - pauseTime;
wavesPaused = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public class PauseMenuFactory {

/**
* Creates the pause menu
* @param game The Gdx game instance that handles screen changes.
* @param waveSpawner The entity that handles the wave spawn timer
* @return entity
*/
public static Entity createPauseMenu(GdxGame game) {
public static Entity createPauseMenu(GdxGame game, Entity waveSpawner) {
Entity pauseMenu = new Entity()
.addComponent(new PauseMenuTimeStopComponent())
.addComponent(new PauseMenuTimeStopComponent(waveSpawner))
.addComponent(new PauseMenuRenderComponent())
.addComponent(new PauseMenuContinueButton())
.addComponent(new PauseMenuSettingsButton(game))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ private void createUI() {
.addComponent(new MainGameActions(this.game))
.addComponent(new MainGameExitDisplay())
.addComponent(new MainGameLoseDisplay())
.addComponent(new MainGamePauseDisplay(this.game))
// WHERE'S THE WAVE SPAWNER, HOW IS THIS GAME FUNCTIONING WHEN IT DOESN'T EVEN MAKE A WAVE SPAWNER
// I'm told there's a new entity for the SpawnWaveTask, maybe it'll actually be accessible in this class.
// fyi there REALLY shouldn't be a null arg, but I need to push this before I can fix the issue.
.addComponent(new MainGamePauseDisplay(this.game, null))
.addComponent(new Terminal())
.addComponent(inputComponent)
.addComponent(new TerminalDisplay());
Expand Down

0 comments on commit e095797

Please sign in to comment.