Skip to content

Commit add1616

Browse files
committed
Comments + better method names
1 parent 69be34e commit add1616

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/lib/scene-manager.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ export class SceneManager {
6464
this.spawnSound();
6565
}
6666

67+
/**
68+
* Render the current frame.
69+
*/
6770
private animate(): void {
6871
const delta = this.clock.getDelta();
69-
this.animateGrid(delta);
70-
this.animateTitle(delta);
71-
this.animateModels(delta);
72+
7273
this.syncRendererSize();
74+
this.moveGridsInfinitely(delta);
75+
this.moveTitleUntilRest(delta);
76+
this.moveSpawnedModels(delta);
77+
7378
this.renderer.render(this.scene, this.camera);
7479
requestAnimationFrame(this.animate.bind(this));
7580
}
@@ -78,7 +83,7 @@ export class SceneManager {
7883
* Move grids closer to the camera. To make grids appear "infinite", reset
7984
* their position once they have travelled one grid row of distance.
8085
*/
81-
private animateGrid(delta: number): void {
86+
private moveGridsInfinitely(delta: number): void {
8287
this.gridTop.position.z = this.gridBottom.position.z =
8388
this.gridTop.position.z < GRID_SIZE / GRID_DIVISIONS
8489
? this.gridTop.position.z + 100 * delta
@@ -88,14 +93,14 @@ export class SceneManager {
8893
/**
8994
* Move the title away from the camera until it reaches its resting position.
9095
*/
91-
private animateTitle(delta: number): void {
96+
private moveTitleUntilRest(delta: number): void {
9297
this.title.position.z = Math.max(this.title.position.z - 350 * delta, 0);
9398
}
9499

95100
/**
96101
* Move models closer to the camera and destroy them when they travel past it.
97102
*/
98-
private animateModels(delta: number): void {
103+
private moveSpawnedModels(delta: number): void {
99104
if (this.title.position.z > 0) return;
100105

101106
this.spawnedModels.forEach((model) => {
@@ -113,6 +118,9 @@ export class SceneManager {
113118
}
114119
}
115120

121+
/**
122+
* Spawn a new model at a random position.
123+
*/
116124
private spawnModel(): void {
117125
const model = sample([...this.assetCache.models.values()])!.scene.clone();
118126
model.position.set(...this.computeModelSpawnPosition());
@@ -126,14 +134,17 @@ export class SceneManager {
126134
this.scene.add(model);
127135
}
128136

137+
/**
138+
* Spawn a new sound and start playing it. The audio may loop several times.
139+
*/
129140
private spawnSound(): void {
130141
const sound = sample(
131142
[...this.assetCache.sounds.values()].filter(
132143
(sound) => !this.spawnedSounds.has(sound)
133144
)
134145
)!;
135146

136-
const timeListener = () => {
147+
const timeListener = (): void => {
137148
if (sound.currentTime >= sound.duration * 0.65) {
138149
this.spawnSound();
139150
sound.removeEventListener('timeupdate', timeListener);
@@ -142,7 +153,7 @@ export class SceneManager {
142153
sound.addEventListener('timeupdate', timeListener);
143154

144155
let loops = random(0, 2);
145-
const endListener = () => {
156+
const endListener = (): void => {
146157
if (loops > 0) {
147158
void sound.play();
148159
loops--;

0 commit comments

Comments
 (0)