Skip to content

Commit

Permalink
move audio to the view, #51 #52
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jun 24, 2017
1 parent de7769c commit 56c3397
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
29 changes: 0 additions & 29 deletions js/ohms-law/model/OhmsLawModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,28 @@ define( function( require ) {
'use strict';

// modules
var BooleanProperty = require( 'AXON/BooleanProperty' );
var DerivedProperty = require( 'AXON/DerivedProperty' );
var inherit = require( 'PHET_CORE/inherit' );
var NumberProperty = require( 'AXON/NumberProperty' );
var OhmsLawConstants = require( 'OHMS_LAW/ohms-law/OhmsLawConstants' );
var ohmsLaw = require( 'OHMS_LAW/ohmsLaw' );
var Sound = require( 'VIBE/Sound' );

// audio
var addBatteryAudio = require( 'audio!OHMS_LAW/add-battery' );
var removeBatteryAudio = require( 'audio!OHMS_LAW/remove-battery' );

/**
* @constructor
*/
function OhmsLawModel() {

var self = this;

// @public {Property.<number>} in volts
this.voltageProperty = new NumberProperty( OhmsLawConstants.VOLTAGE_RANGE.getDefaultValue() );

// @public {Property.<number>} in Ohms
this.resistanceProperty = new NumberProperty( OhmsLawConstants.RESISTANCE_RANGE.getDefaultValue() );

// @public {Property.<boolean>}
this.soundActiveProperty = new BooleanProperty( true );

// @public {Property.<number>} create a derived property that tracks the current in milli amps
this.currentProperty = new DerivedProperty( [ this.voltageProperty, this.resistanceProperty ],
function( voltage, resistance ) {
return 1000 * voltage / resistance;
} );

// Hook up the sounds that are played when batteries are added or removed.
var addBatterySound = new Sound( addBatteryAudio );
var removeBatterySound = new Sound( removeBatteryAudio );

// play sounds when adding or removing a battery
this.voltageProperty.lazyLink( function( voltage, oldVoltage ) {
var newNumberBatteries = Math.floor( voltage / OhmsLawConstants.AA_VOLTAGE );
var oldNumberBatteries = Math.floor( oldVoltage / OhmsLawConstants.AA_VOLTAGE );
if ( self.soundActiveProperty.value ) {
if ( newNumberBatteries > oldNumberBatteries ) {
addBatterySound.play();
}
else if ( newNumberBatteries < oldNumberBatteries ) {
removeBatterySound.play();
}
}
} );
}

ohmsLaw.register( 'OhmsLawModel', OhmsLawModel );
Expand Down
30 changes: 28 additions & 2 deletions js/ohms-law/view/OhmsLawScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ define( function( require ) {
var ScreenView = require( 'JOIST/ScreenView' );
var SoundToggleButton = require( 'SCENERY_PHET/buttons/SoundToggleButton' );
var WireBox = require( 'OHMS_LAW/ohms-law/view/WireBox' );
var Sound = require( 'VIBE/Sound' );
var BooleanProperty = require( 'AXON/BooleanProperty' );
var OhmsLawConstants = require( 'OHMS_LAW/ohms-law/OhmsLawConstants' );

// audio
// The sounds themselves can be constants because there is only every one instance of OhmsLawScreenView.
var ADD_BATTERY_SOUND = new Sound( require( 'audio!OHMS_LAW/add-battery' ) );
var REMOVE_BATTERY_SOUND = new Sound( require( 'audio!OHMS_LAW/remove-battery' ) );

/**
* @param {OhmsLawModel} model
* @constructor
*/
function OhmsLawScreenView( model ) {

// {Property.<boolean>}
var soundActiveProperty = new BooleanProperty( true );

ScreenView.call( this );

// Circuit node with readout node
Expand All @@ -51,7 +61,7 @@ define( function( require ) {
var buttonCenterYOffset = 50; // empirically determined

// Sound on/off toggle button
var soundToggleButton = new SoundToggleButton( model.soundActiveProperty, {
var soundToggleButton = new SoundToggleButton( soundActiveProperty, {
scale: 1.15,
stroke: 'gray',
lineWidth: 0.5,
Expand All @@ -65,8 +75,24 @@ define( function( require ) {
radius: 30,
centerX: controlPanel.left + controlPanel.width * 0.27, // empirically determined
centerY: controlPanel.bottom + buttonCenterYOffset,
listener: function() { model.reset(); }
listener: function() {
model.reset();
}
} ) );

// Play sounds when adding or removing a battery
model.voltageProperty.lazyLink( function( voltage, oldVoltage ) {
var newNumberBatteries = Math.floor( voltage / OhmsLawConstants.AA_VOLTAGE );
var oldNumberBatteries = Math.floor( oldVoltage / OhmsLawConstants.AA_VOLTAGE );
if ( soundActiveProperty.value ) {
if ( newNumberBatteries > oldNumberBatteries ) {
ADD_BATTERY_SOUND.play();
}
else if ( newNumberBatteries < oldNumberBatteries ) {
REMOVE_BATTERY_SOUND.play();
}
}
} );
}

ohmsLaw.register( 'OhmsLawScreenView', OhmsLawScreenView );
Expand Down

0 comments on commit 56c3397

Please sign in to comment.