From 98a6ee5f78fbdb732f1aae5c7d5f6827b67b3977 Mon Sep 17 00:00:00 2001 From: jbphet Date: Tue, 5 Nov 2019 11:37:37 -0700 Subject: [PATCH] consolidated boundary sound generation, consolidated into a sound generator, see https://github.com/phetsims/gravity-force-lab/issues/181 --- .../view/BoundarySoundGenerator.js | 64 +++++++++++++++++++ .../view/GravityForceLabScreenView.js | 10 +++ 2 files changed, 74 insertions(+) create mode 100644 js/gravity-force-lab/view/BoundarySoundGenerator.js diff --git a/js/gravity-force-lab/view/BoundarySoundGenerator.js b/js/gravity-force-lab/view/BoundarySoundGenerator.js new file mode 100644 index 00000000..7f4063b9 --- /dev/null +++ b/js/gravity-force-lab/view/BoundarySoundGenerator.js @@ -0,0 +1,64 @@ +// Copyright 2019, University of Colorado Boulder + +/** + * BoundarySoundGenerator generates the sounds that indicate when the masses have reached their inner and outer motion + * limits. + * + * @author John Blanco (PhET Interactive Simulations) + */ +define( require => { + 'use strict'; + + // modules + const gravityForceLab = require( 'GRAVITY_FORCE_LAB/gravityForceLab' ); + const SoundClip = require( 'TAMBO/sound-generators/SoundClip' ); + const SoundGenerator = require( 'TAMBO/sound-generators/SoundGenerator' ); + + // sounds + const innerBoundarySoundInfo = require( 'sound!GRAVITY_FORCE_LAB/scrunched-mass-collision-sonic-womp.mp3' ); + const outerBoundarySoundInfo = require( 'sound!TAMBO/boundary-reached.mp3' ); + + class BoundarySoundGenerator extends SoundGenerator { + + /** + * @param {ISLCObject} movableObject + * @param {ISLCModel} model + * @param {Object} [options] + */ + constructor( movableObject, model, options ) { + + super( options ); + + const innerBoundarySoundClip = new SoundClip( innerBoundarySoundInfo ); + innerBoundarySoundClip.connect( this.masterGainNode ); + const outerBoundarySoundClip = new SoundClip( outerBoundarySoundInfo ); + outerBoundarySoundClip.connect( this.masterGainNode ); + + // function for starting the force sound or adjusting the volume + const positionListener = position => { + if ( position === model.getObjectMinPosition( movableObject ) ) { + innerBoundarySoundClip.play(); + } + else if ( position === model.getObjectMaxPosition( movableObject ) ) { + outerBoundarySoundClip.play(); + } + }; + movableObject.positionProperty.link( positionListener ); + + // @private {function} + this.disposeBoundarySoundGenerator = () => { movableObject.positionProperty.unlink( positionListener ); }; + } + + /** + * @public + */ + dispose() { + this.disposeBoundarySoundGenerator(); + super.dispose(); + } + } + + gravityForceLab.register( 'BoundarySoundGenerator', BoundarySoundGenerator ); + + return BoundarySoundGenerator; +} ); \ No newline at end of file diff --git a/js/gravity-force-lab/view/GravityForceLabScreenView.js b/js/gravity-force-lab/view/GravityForceLabScreenView.js index b6f7c2b8..ad9fa320 100644 --- a/js/gravity-force-lab/view/GravityForceLabScreenView.js +++ b/js/gravity-force-lab/view/GravityForceLabScreenView.js @@ -12,6 +12,7 @@ define( require => { // modules const AccessiblePeer = require( 'SCENERY/accessibility/AccessiblePeer' ); + const BoundarySoundGenerator = require( 'GRAVITY_FORCE_LAB/gravity-force-lab/view/BoundarySoundGenerator' ); const Bounds2 = require( 'DOT/Bounds2' ); const DefaultDirection = require( 'INVERSE_SQUARE_LAW_COMMON/view/DefaultDirection' ); const ForceSoundGenerator = require( 'GRAVITY_FORCE_LAB/gravity-force-lab/view/ForceSoundGenerator' ); @@ -68,6 +69,7 @@ define( require => { const OBJECT_ONE = ISLCObjectEnum.OBJECT_ONE; const OBJECT_TWO = ISLCObjectEnum.OBJECT_TWO; const CHECKBOX_TEXT_SIZE = 15; + const BOUNDARY_SOUNDS_LEVEL = 1; function GravityForceLabScreenView( model, tandem ) { @@ -348,6 +350,14 @@ define( require => { resetAllButton.buttonModel.isFiringProperty, { initialOutputLevel: 0.7, lockoutTime: 0.2 } ) ); + + // sound generation for masses reaching the inner or outer motion boundaries + soundManager.addSoundGenerator( new BoundarySoundGenerator( model.object1, model, { + initialOutputLevel: BOUNDARY_SOUNDS_LEVEL + } ) ); + soundManager.addSoundGenerator( new BoundarySoundGenerator( model.object2, model, { + initialOutputLevel: BOUNDARY_SOUNDS_LEVEL + } ) ); } gravityForceLab.register( 'GravityForceLabScreenView', GravityForceLabScreenView );