Skip to content

Commit 82d4f1c

Browse files
committed
Pausing sounds when show menu.
1 parent 4c64e02 commit 82d4f1c

File tree

5 files changed

+76
-30
lines changed

5 files changed

+76
-30
lines changed

blade-engine/src/com/bladecoder/engine/actions/SoundAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SoundAction implements Action {
2727
private String actor;
2828

2929
@ActionPropertyDescription("The actor 'soundId' to play. If empty the current sound will be stopped.")
30-
@ActionProperty(type = Type.SOUND)
30+
@ActionProperty(type = Type.STRING)
3131
private String play;
3232

3333
@Override

blade-engine/src/com/bladecoder/engine/model/InteractiveActor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void playSound(String id) {
162162
}
163163

164164
public void stopCurrentSound() {
165-
if (playingSound == null || sounds == null)
165+
if (playingSound == null)
166166
return;
167167

168168
SoundFX s = sounds.get(playingSound);
@@ -173,6 +173,10 @@ public void stopCurrentSound() {
173173

174174
playingSound = null;
175175
}
176+
177+
public String getPlayingSound() {
178+
return playingSound;
179+
}
176180

177181
@Override
178182
public String toString() {
@@ -226,6 +230,8 @@ public void retrieveAssets() {
226230

227231
if (playingSound != null && sounds.get(playingSound).getLoop() == true) {
228232
playSound(playingSound);
233+
} else {
234+
playingSound = null;
229235
}
230236
}
231237
}

blade-engine/src/com/bladecoder/engine/model/SoundFX.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public void stop() {
5050
s.stop();
5151
}
5252

53+
public void pause() {
54+
if(s==null)
55+
return;
56+
57+
s.pause();
58+
}
59+
60+
public void resume() {
61+
if(s==null)
62+
return;
63+
64+
s.resume();
65+
}
66+
5367
public boolean getLoop() {
5468
return loop;
5569
}
@@ -86,6 +100,7 @@ public void retrieveAssets() {
86100

87101
@Override
88102
public void dispose() {
103+
stop();
89104
EngineAssetManager.getInstance().disposeSound(getFilename());
90105
}
91106
}

blade-engine/src/com/bladecoder/engine/model/World.java

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static enum AssetState {
8282
private boolean paused;
8383
private boolean cutMode;
8484

85-
/** keep track of the time of game in ms.*/
85+
/** keep track of the time of game in ms. */
8686
private long timeOfGame;
8787

8888
/** for debug purposes, keep track of loading time */
@@ -114,7 +114,7 @@ public static enum AssetState {
114114
// We not dispose the last loaded scene.
115115
// Instead we cache it to improve performance when returning
116116
transient private Scene cachedScene;
117-
117+
118118
private String previousScene = null;
119119

120120
public static World getInstance() {
@@ -146,7 +146,7 @@ private void init() {
146146
paused = false;
147147

148148
disposed = false;
149-
149+
150150
previousScene = null;
151151
}
152152

@@ -164,7 +164,7 @@ public void setCustomProperty(String name, String value) {
164164
else
165165
customProperties.put(name, value);
166166
}
167-
167+
168168
public String getPreviousScene() {
169169
return previousScene;
170170
}
@@ -195,7 +195,7 @@ public void update(float delta) {
195195
&& !EngineAssetManager.getInstance().isLoading()) {
196196

197197
retrieveAssets();
198-
198+
199199
paused = false;
200200

201201
boolean initScene = (assetState == AssetState.LOADING_AND_INIT_SCENE);
@@ -291,7 +291,7 @@ public void setCurrentScene(Scene scene) {
291291
ActionCallbackQueue.clear();
292292

293293
if (cachedScene == scene) {
294-
assetState = AssetState.LOADING_AND_INIT_SCENE;
294+
assetState = AssetState.LOADING_AND_INIT_SCENE;
295295
} else {
296296
if (cachedScene != null) {
297297
cachedScene.dispose();
@@ -309,11 +309,15 @@ public void setCurrentScene(Scene scene) {
309309
currentDialog = null;
310310

311311
// Stop Sounds
312-
for(BaseActor a:currentScene.getActors().values()) {
313-
if(a instanceof InteractiveActor)
314-
((InteractiveActor) a).stopCurrentSound();
312+
for (BaseActor a : currentScene.getActors().values()) {
313+
if (a instanceof InteractiveActor) {
314+
String playingSound = ((InteractiveActor) a).getPlayingSound();
315+
316+
if (playingSound != null)
317+
((InteractiveActor) a).getSounds().get(playingSound).stop();
318+
}
315319
}
316-
320+
317321
previousScene = currentScene.getId();
318322

319323
if (CACHE_ENABLED)
@@ -495,20 +499,38 @@ public boolean inCutMode() {
495499
public void pause() {
496500
paused = true;
497501

498-
if (currentScene != null)
502+
if (currentScene != null) {
499503
currentScene.pauseMusic();
500504

501-
// TODO Pause all sounds
505+
// Pause all sounds
506+
for (BaseActor a : currentScene.getActors().values()) {
507+
if (a instanceof InteractiveActor) {
508+
String playingSound = ((InteractiveActor) a).getPlayingSound();
509+
510+
if (playingSound != null)
511+
((InteractiveActor) a).getSounds().get(playingSound).pause();
512+
}
513+
}
514+
}
502515
}
503516

504517
public void resume() {
505518
paused = false;
506519

507520
if (assetState == AssetState.LOADED) {
508-
if (currentScene != null)
521+
if (currentScene != null) {
509522
currentScene.resumeMusic();
510523

511-
// TODO Resume all sounds
524+
// Resume all sounds
525+
for (BaseActor a : currentScene.getActors().values()) {
526+
if (a instanceof InteractiveActor) {
527+
String playingSound = ((InteractiveActor) a).getPlayingSound();
528+
529+
if (playingSound != null)
530+
((InteractiveActor) a).getSounds().get(playingSound).resume();
531+
}
532+
}
533+
}
512534
}
513535
}
514536

@@ -643,9 +665,11 @@ public void loadChapter(String chapterName) throws IOException {
643665

644666
I18N.loadChapter(EngineAssetManager.MODEL_DIR + chapterName);
645667
} else {
646-
EngineLogger.error("ERROR LOADING CHAPTER: " + chapterName + EngineAssetManager.CHAPTER_EXT + " doesn't exists.");
668+
EngineLogger.error("ERROR LOADING CHAPTER: " + chapterName + EngineAssetManager.CHAPTER_EXT
669+
+ " doesn't exists.");
647670
dispose();
648-
throw new IOException("ERROR LOADING CHAPTER: " + chapterName + EngineAssetManager.CHAPTER_EXT + " doesn't exists.");
671+
throw new IOException("ERROR LOADING CHAPTER: " + chapterName + EngineAssetManager.CHAPTER_EXT
672+
+ " doesn't exists.");
649673
}
650674

651675
EngineLogger.debug("MODEL LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
@@ -667,8 +691,8 @@ public boolean savedGameExists() {
667691
}
668692

669693
public boolean savedGameExists(String filename) {
670-
return EngineAssetManager.getInstance().getUserFile(filename).exists() ||
671-
EngineAssetManager.getInstance().assetExists("tests/" + filename);
694+
return EngineAssetManager.getInstance().getUserFile(filename).exists()
695+
|| EngineAssetManager.getInstance().assetExists("tests/" + filename);
672696
}
673697

674698
public void loadGameState() throws IOException {
@@ -679,8 +703,8 @@ public void loadGameState() throws IOException {
679703

680704
public void loadGameState(String filename) throws IOException {
681705
FileHandle savedFile = null;
682-
683-
if(EngineAssetManager.getInstance().getUserFile(filename).exists())
706+
707+
if (EngineAssetManager.getInstance().getUserFile(filename).exists())
684708
savedFile = EngineAssetManager.getInstance().getUserFile(filename);
685709
else
686710
savedFile = EngineAssetManager.getInstance().getAsset("tests/" + filename);
@@ -700,12 +724,12 @@ public void loadGameState(FileHandle savedFile) throws IOException {
700724
SerializationHelper.getInstance().setMode(Mode.STATE);
701725

702726
JsonValue root = new JsonReader().parse(savedFile.reader("UTF-8"));
703-
727+
704728
Json json = new Json();
705729
json.setIgnoreUnknownFields(true);
706730

707731
read(json, root);
708-
732+
709733
assetState = AssetState.LOAD_ASSETS;
710734

711735
} else {
@@ -716,7 +740,7 @@ public void loadGameState(FileHandle savedFile) throws IOException {
716740
public void saveGameState() throws IOException {
717741
saveGameState(GAMESTATE_FILENAME);
718742
}
719-
743+
720744
public void removeGameState(String filename) throws IOException {
721745
EngineAssetManager.getInstance().getUserFile(filename).delete();
722746
EngineAssetManager.getInstance().getUserFile(filename + ".png").delete();
@@ -887,16 +911,16 @@ public void read(Json json, JsonValue jsonData) {
887911
SerializationHelper.getInstance().setMode(Mode.STATE);
888912

889913
currentScene = scenes.get(json.readValue("currentScene", String.class, jsonData));
890-
914+
891915
for (Scene s : scenes.values()) {
892916
JsonValue jsonValue = jsonData.get("scenes").get(s.getId());
893-
894-
if(jsonValue != null)
917+
918+
if (jsonValue != null)
895919
s.read(json, jsonValue);
896920
else
897921
EngineLogger.debug("LOAD WARNING: Scene not found in saved game: " + s.getId());
898922
}
899-
923+
900924
inventory = json.readValue("inventory", Inventory.class, jsonData);
901925

902926
timeOfGame = json.readValue("timeOfGame", long.class, 0L, jsonData);
@@ -908,7 +932,7 @@ public void read(Json json, JsonValue jsonData) {
908932

909933
textManager = json.readValue("textmanager", TextManager.class, jsonData);
910934
customProperties = json.readValue("customProperties", HashMap.class, String.class, jsonData);
911-
previousScene = json.readValue("previousScene", String.class, jsonData);
935+
previousScene = json.readValue("previousScene", String.class, jsonData);
912936

913937
String actorId = json.readValue("dialogActor", String.class, jsonData);
914938
String dialogId = json.readValue("currentDialog", String.class, jsonData);

blade-engine/src/com/bladecoder/engine/ui/defaults/DefaultSceneScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ public void runVerb(InteractiveActor a, String verb, String target) {
838838
}
839839

840840
private void showMenu() {
841+
pause();
841842
ui.setCurrentScreen(Screens.MENU_SCREEN);
842843
}
843844

0 commit comments

Comments
 (0)