Move game pause logic from GameEngine to Context

This commit is contained in:
2021-04-08 22:18:58 +02:00
parent 028faae564
commit 808155533d
4 changed files with 29 additions and 50 deletions

View File

@@ -62,6 +62,8 @@ public interface Context extends Updatable, Renderable, Disposable {
boolean isPaused(); boolean isPaused();
void setPaused(boolean paused);
void pause(); void pause();
void resume(); void resume();

View File

@@ -98,6 +98,9 @@ public class DefaultContext implements Context {
private final List<Sound> sounds = new LinkedList<>(); private final List<Sound> sounds = new LinkedList<>();
@Getter
private boolean paused;
@SneakyThrows @SneakyThrows
@Override @Override
public void init(@NonNull Screen screen, @NonNull Input input, @NonNull Camera camera) { public void init(@NonNull Screen screen, @NonNull Input input, @NonNull Camera camera) {
@@ -220,23 +223,31 @@ public class DefaultContext implements Context {
} }
@Override @Override
public boolean isPaused() { public void setPaused(boolean paused) {
return engine.isPaused(); this.paused = paused;
sounds.forEach(this.paused ? Sound::pause : Sound::play);
} }
@Override @Override
public void pause() { public void pause() {
engine.pause(); this.paused = true;
sounds.forEach(Sound::pause);
} }
@Override @Override
public void resume() { public void resume() {
engine.resume(); this.paused = false;
sounds.forEach(Sound::play);
} }
@Override @Override
public boolean togglePause() { public boolean togglePause() {
return engine.togglePaused(); this.paused = !this.paused;
sounds.forEach(this.paused ? Sound::pause : Sound::play);
return this.paused;
} }
@Override @Override
@@ -252,17 +263,19 @@ public class DefaultContext implements Context {
public void update(float dt) { public void update(float dt) {
gameRunner.update(dt); gameRunner.update(dt);
if (mapHandler != null) { if (!paused) {
mapHandler.update(this, map, dt); if (mapHandler != null) {
} mapHandler.update(this, map, dt);
}
if (map != null) { if (map != null) {
map.update(dt); map.update(dt);
}
} }
for (var iterator = sounds.iterator(); iterator.hasNext(); ) { for (var iterator = sounds.iterator(); iterator.hasNext(); ) {
var sound = iterator.next(); var sound = iterator.next();
if (!sound.isPlaying()) { if (sound.isStopped()) {
iterator.remove(); iterator.remove();
soundManager.disposeSound(sound); soundManager.disposeSound(sound);
} }

View File

@@ -39,9 +39,6 @@ public class DefaultGameEngine implements GameEngine {
@Getter @Getter
private boolean running = false; private boolean running = false;
@Getter
private boolean paused = true;
private void run() { private void run() {
try { try {
init(); init();
@@ -76,13 +73,9 @@ public class DefaultGameEngine implements GameEngine {
input(); input();
if (!paused) { while (accumulator >= step) {
while (accumulator >= step) { update(dt);
update(dt); accumulator -= step;
accumulator -= step;
}
} else {
accumulator = 0;
} }
render(); render();
@@ -129,27 +122,6 @@ public class DefaultGameEngine implements GameEngine {
running = false; running = false;
} }
@Override
public void pause() {
log.info("Pausing the engine");
paused = true;
}
@Override
public void resume() {
log.info("Resuming the engine");
paused = false;
}
@Override
public boolean togglePaused() {
paused = !paused;
log.info("{} the engine", paused ? "Pausing" : "Resuming");
return paused;
}
// TODO // TODO
// It is supposed to be moved to the Context so that // It is supposed to be moved to the Context so that
// user will be able to choose default window size, // user will be able to choose default window size,

View File

@@ -7,13 +7,5 @@ public interface GameEngine {
void stop(); void stop();
void pause();
void resume();
boolean isRunning(); boolean isRunning();
boolean isPaused();
boolean togglePaused();
} }