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

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

View File

@@ -39,9 +39,6 @@ public class DefaultGameEngine implements GameEngine {
@Getter
private boolean running = false;
@Getter
private boolean paused = true;
private void run() {
try {
init();
@@ -76,13 +73,9 @@ public class DefaultGameEngine implements GameEngine {
input();
if (!paused) {
while (accumulator >= step) {
update(dt);
accumulator -= step;
}
} else {
accumulator = 0;
while (accumulator >= step) {
update(dt);
accumulator -= step;
}
render();
@@ -129,27 +122,6 @@ public class DefaultGameEngine implements GameEngine {
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
// It is supposed to be moved to the Context so that
// user will be able to choose default window size,

View File

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