Skip to content

Commit 7afb761

Browse files
Merge pull request #108 from SpringRoll/feature/187532394-springroll-listeners
Feature/187532394 - Pixi template Springroll listeners
2 parents 5672c3d + 88974df commit 7afb761

File tree

2 files changed

+61
-49
lines changed

2 files changed

+61
-49
lines changed

src/game.js

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,75 @@ export class Game
1414
this.height = height;
1515

1616
this.pixi = new PIXI.Application();
17-
(async () => {
18-
await this.pixi.init({
17+
(async () =>
18+
{
19+
await this.pixi.init(
20+
{
1921
width,
2022
height,
2123
autoResize: true
2224
});
2325

2426
document.getElementById('content').appendChild(this.pixi.canvas);
2527

26-
this.app = new Application(
28+
this.application = new Application(
29+
{
30+
// This feature list matches the Springroll states subscribed to below
31+
// Note: features will only work if the Container environment also supports controls for that feature.
32+
features:
2733
{
28-
// This feature list matches the Springroll states subscribed to below
29-
// Note: features will only work if the Container environment also supports controls for that feature.
30-
features:
31-
{
32-
sfx: true,
33-
sound: true,
34-
music: true,
35-
vo: true,
36-
sfxVolume: true,
37-
soundVolume: true,
38-
musicVolume: true,
39-
voVolume: true,
40-
captionsMuted: true,
41-
}
42-
});
34+
captions: true,
35+
sound: true,
36+
soundVolume: true,
37+
vo: true,
38+
voVolume: true,
39+
music: true,
40+
musicVolume: true,
41+
sfx: true,
42+
sfxVolume: true,
43+
}
44+
});
4345

4446
this.resize = this.resize.bind(this);
4547
this.scaleManager = new SafeScaleManager({width, height, safeWidth: 1024, safeHeight: 660, callback: this.resize});
46-
47-
// Subscribe to required springroll States.
48-
this.app.state.pause.subscribe((value) =>
48+
49+
// Listen for container events from the application.
50+
this.application.state.ready.subscribe(this.onApplicationReady.bind(this));
51+
this.application.state.pause.subscribe(this.onApplicationPause.bind(this));
52+
53+
this.application.state.captionsMuted.subscribe(result =>
4954
{
50-
this.isPaused = value;
55+
console.log('captionsMuted: ', result);
5156
});
5257

53-
this.app.state.soundVolume.subscribe((value) =>
58+
this.application.state.soundVolume.subscribe(this.onMainVolumeChange.bind(this));
59+
this.application.state.voVolume.subscribe(result =>
5460
{
55-
sound.volumeAll = value;
61+
console.log('voVolume: ', result);
5662
});
57-
58-
this.app.state.sfxVolume.subscribe((value) =>
63+
this.application.state.musicVolume.subscribe(result =>
64+
{
65+
console.log('musicVolume: ', result);
66+
});
67+
this.application.state.sfxVolume.subscribe((value) =>
5968
{
6069
//Check to see if sound exists in the cache before changing its volume
61-
if(PIXI.Assets.cache.has('bounce')){
70+
if(PIXI.Assets.cache.has('bounce'))
71+
{
6272
PIXI.Assets.get('bounce').sound.volume = value;
6373
}
6474

6575
});
66-
67-
this.app.state.musicVolume.subscribe(result =>
68-
{
69-
console.log('musicVolume: ', result);
70-
});
71-
72-
this.app.state.voVolume.subscribe(result =>
73-
{
74-
console.log('voVolume: ', result);
75-
});
76-
77-
this.app.state.captionsMuted.subscribe(result =>
78-
{
79-
console.log('captionsMuted: ', result);
80-
});
81-
76+
8277
// add a extra state property for storying the current scene. Whenever the scene is changed, this class
8378
// will swap out the container attached to the stage
84-
this.app.state.scene = new Property(null);
85-
this.app.state.scene.subscribe(this.onChangeScene.bind(this));
79+
this.application.state.scene = new Property(null);
80+
this.application.state.scene.subscribe(this.onChangeScene.bind(this));
8681

8782
// wait for the app to be ready, then set the new scene
88-
this.app.state.ready.subscribe(() =>
83+
this.application.state.ready.subscribe(() =>
8984
{
90-
this.app.state.scene.value = new TitleScene(this);
85+
this.application.state.scene.value = new TitleScene(this);
9186
});
9287

9388
// Dispatch a ready event after initializing the app
@@ -101,6 +96,23 @@ export class Game
10196
this.pixi.ticker.add(this.update.bind(this));
10297
}
10398

99+
onApplicationReady()
100+
{
101+
console.log('The app is ready. All plugins have finished their setup and preload calls');
102+
}
103+
104+
onApplicationPause(isPaused)
105+
{
106+
console.log('Is the game paused?', isPaused);
107+
this.isPaused = isPaused;
108+
}
109+
110+
onMainVolumeChange(value)
111+
{
112+
sound.volumeAll = value;
113+
}
114+
115+
104116
onChangeScene(newScene, oldScene)
105117
{
106118
this.currentScene = null;
@@ -119,7 +131,7 @@ export class Game
119131
update(deltaTime)
120132
{
121133
// if the game is paused, or there isn't a scene, we can skip rendering/updates
122-
if (this.isPaused || this.app.state.scene.value === null)
134+
if (this.isPaused || this.application.state.scene.value === null)
123135
{
124136
return;
125137
}

src/scenes/title.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class TitleScene extends PIXI.Container
3737
{
3838
// when the label is clicked, preload the game scene and then tell the app to switch scenes
3939
const nextScene = new GameScene(this.game);
40-
this.game.app.state.scene.value = nextScene;
40+
this.game.application.state.scene.value = nextScene;
4141
});
4242

4343
this.game.scaleManager.addEntity(new Anchor(

0 commit comments

Comments
 (0)