Skip to content

Commit

Permalink
Merge pull request #26 from C-m8/master
Browse files Browse the repository at this point in the history
Added typewriter sound effects
  • Loading branch information
Nightspeller committed May 31, 2021
2 parents b5bf661 + 35386f0 commit b2645a8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 1 deletion.
Binary file added src/assets/audio/ui/typewriter-end.mp3
Binary file not shown.
Binary file added src/assets/audio/ui/typewriter-end.ogg
Binary file not shown.
Binary file added src/assets/audio/ui/typewriter-long.mp3
Binary file not shown.
Binary file added src/assets/audio/ui/typewriter-long.ogg
Binary file not shown.
Binary file added src/assets/audio/ui/typewriter-short.mp3
Binary file not shown.
Binary file added src/assets/audio/ui/typewriter-short.ogg
Binary file not shown.
37 changes: 36 additions & 1 deletion src/scenes/overlays/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default class DialogScene extends GeneralOverlayScene {
declare public opts: DialogOptions;
private speakerName: string;

private typewriterLongSound: Phaser.Sound.BaseSound | Phaser.Sound.HTML5AudioSound | Phaser.Sound.WebAudioSound;
private typewriterShortSound: Phaser.Sound.BaseSound | Phaser.Sound.HTML5AudioSound | Phaser.Sound.WebAudioSound;
private typewriterEndSound: Phaser.Sound.BaseSound | Phaser.Sound.HTML5AudioSound | Phaser.Sound.WebAudioSound;

constructor() {
super({ key: 'Dialog' });
}
Expand Down Expand Up @@ -48,7 +52,7 @@ export default class DialogScene extends GeneralOverlayScene {
// closeButtonHoverColor: 'red',

textColor: 'black',
letterAppearanceDelay: 10,
letterAppearanceDelay: 15,
};
this.speakerName = speakerName;
this.dialogTree = dialogTree;
Expand All @@ -64,6 +68,9 @@ export default class DialogScene extends GeneralOverlayScene {

public create() {
super.create(this.parentSceneKey, this.opts);
this.typewriterLongSound = this.sound.add('typewriter-long');
this.typewriterShortSound = this.sound.add('typewriter-short');
this.typewriterEndSound = this.sound.add('typewriter-end');
this.dialogDisplayGroup = this.add.group();
this._showDialog();
// @ts-ignore
Expand Down Expand Up @@ -211,7 +218,9 @@ export default class DialogScene extends GeneralOverlayScene {
delay: this.opts.letterAppearanceDelay,
callback: () => {
textGameObject.setText(text.slice(0, shownLettersCounter));
this.playTypewriterSound(shownLettersCounter, text[shownLettersCounter]);
if (text.length === shownLettersCounter) {
this.playTypewriterSound();
this.timedEvent.remove();
zone.destroy();
this.input.keyboard.off('keydown-SPACE');
Expand All @@ -224,6 +233,9 @@ export default class DialogScene extends GeneralOverlayScene {
});

zone.once('pointerdown', () => {
this.typewriterEndSound.play({
volume: 0.5,
});
zone.destroy();
this.input.keyboard.off('keydown-SPACE');
this.timedEvent.remove();
Expand All @@ -244,6 +256,29 @@ export default class DialogScene extends GeneralOverlayScene {
});
}

private playTypewriterSound(letterCounter?: number, currentLetter?: string) {
if (currentLetter === '—') return;

if (letterCounter === undefined) {
this.typewriterEndSound.play({
volume: 0.5,
});
return;
}

if (letterCounter % 7 === 0) {
if (Phaser.Math.Between(0, 1) === 1) {
this.typewriterLongSound.play({
volume: 0.5,
});
} else {
this.typewriterShortSound.play({
volume: 0.5,
});
}
}
}

private _showName() {
if (this.speakerName) {
const name = this.add.text(this.opts.windowX + 5, this.opts.windowY - 26, this.speakerName, { font: '20px monospace' })
Expand Down
3 changes: 3 additions & 0 deletions src/scenes/preload/preloadAudioAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default function preloadAudioAssets(preloadScene: Scene) {
preloadScene.load.audio('text-button-hover', ['assets/audio/ui/text-button-hover.ogg', 'assets/audio/ui/text-button-hover.mp3']);
preloadScene.load.audio('text-button-select', ['assets/audio/ui/text-button-select.ogg', 'assets/audio/ui/text-button-select.mp3']);
preloadScene.load.audio('checkbox-checked', ['assets/audio/ui/checkbox-checked.ogg', 'assets/audio/ui/checkbox-checked.mp3']);
preloadScene.load.audio('typewriter-long', ['assets/audio/ui/typewriter-long.ogg', 'assets/audio/ui/typewriter-long.mp3']);
preloadScene.load.audio('typewriter-short', ['assets/audio/ui/typewriter-short.ogg', 'assets/audio/ui/typewriter-short.mp3']);
preloadScene.load.audio('typewriter-end', ['assets/audio/ui/typewriter-end.ogg', 'assets/audio/ui/typewriter-end.mp3']);
preloadScene.load.audio('main-menu-start-game', ['assets/audio/ui/main-menu-start-game.ogg', 'assets/audio/ui/main-menu-start-game.mp3']);

preloadScene.load.audio('intro', ['assets/audio/intro.ogg', 'assets/audio/intro.mp3']);
Expand Down

0 comments on commit b2645a8

Please sign in to comment.