From cd45ac293c7dd813c59026b92336c1e78570c768 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 12 Sep 2020 15:52:40 -0500 Subject: [PATCH] Add ability to resolve absolute URLs; trigger fire, hit, miss sounds (#6) --- index.html | 7 +++++++ src/components/TopLevel.component.js | 8 ++++++-- src/module/sounds.js | 20 ++++++++++++++------ src/module/util.js | 10 ++++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 4a69e10..39be12b 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,13 @@ + diff --git a/src/components/TopLevel.component.js b/src/components/TopLevel.component.js index 0ea2448..1d8b7c2 100644 --- a/src/components/TopLevel.component.js +++ b/src/components/TopLevel.component.js @@ -198,9 +198,13 @@ export default class TopLevelComponent extends Component { * @param {number} row_index * @param {number} column_index */ - on_missile_fired([row_index, column_index]) { + async on_missile_fired([row_index, column_index]) { if ( this.player_is_firing_missiles ) { - game_service.attempt_missile_fire([row_index, column_index]) + await GameSounds.Fire.play() + const success = game_service.attempt_missile_fire([row_index, column_index]) + + if ( success ) await GameSounds.Hit.play() + else await GameSounds.Miss.play() // Give the user time to see whether they hit or not setTimeout(() => { diff --git a/src/module/sounds.js b/src/module/sounds.js index ad8282b..a094de7 100644 --- a/src/module/sounds.js +++ b/src/module/sounds.js @@ -1,3 +1,5 @@ +import { appUrl } from './util.js' + /** * A thin wrapper for sound effects. */ @@ -23,8 +25,13 @@ class Sound { /** * Start playing the sound. */ - play() { - this.sound.play() + async play() { + const duration = this.sound.duration + + await this.sound.play() + await new Promise(res => { + setTimeout(res, duration * 1000) + }) } /** @@ -36,11 +43,12 @@ class Sound { } const GameSounds = { - Victory: new Sound('/lib/sounds/cartoon_success_fanfair.mp3'), - Fire: new Sound('/lib/sounds/zapsplat_warfare_mortar_projectile_launch_002_25232.mp3'), - Hit: new Sound('/lib/sounds/zapsplat_warfare_bomb_whizz_in_hit_close_by_explosion_med_003_48060.mp3'), - Miss: new Sound('/lib/sounds/zapsplat_nature_water_pour_medium_amount_deep_sudden_fast_002_52765.mp3'), + Victory: new Sound(appUrl('/lib/sounds/cartoon_success_fanfair.mp3')), + Fire: new Sound(appUrl('/lib/sounds/zapsplat_warfare_mortar_projectile_launch_002_25232.mp3')), + Hit: new Sound(appUrl('/lib/sounds/zapsplat_warfare_bomb_whizz_in_hit_close_by_explosion_med_003_48060.mp3')), + Miss: new Sound(appUrl('/lib/sounds/zapsplat_nature_water_pour_medium_amount_deep_sudden_fast_002_52765.mp3')), } +console.log(GameSounds) export { GameSounds } diff --git a/src/module/util.js b/src/module/util.js index cf2d52e..bd6ffe5 100644 --- a/src/module/util.js +++ b/src/module/util.js @@ -112,3 +112,13 @@ export function clone(obj) { } return copy } + +/** + * Generate an absolute URL to a file w/in the project directory. + * @param {string} path + * @return {string} + */ +export function appUrl(path) { + if ( path.startsWith('/') ) path = path.slice(1) + return `${APP_BASE_PATH}${path}` +}