Skip to content

Commit

Permalink
passed tandem to Molecule and subtypes, #151
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 6, 2017
1 parent 0ff9725 commit f9ad4ae
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
1 change: 1 addition & 0 deletions js/moleculesandlight/view/ObservationWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ define( function( require ) {
photonAbsorptionModel.restoreActiveMolecule();
self.returnMoleculeButtonVisibleProperty.set( false );
self.moleculeCheckBounds();

// a11y
// move focus to the emission control slider only when the button is clicked
// retain focus on other elements if they return the molecule
Expand Down
29 changes: 18 additions & 11 deletions js/photon-absorption/model/Molecule.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ define( function( require ) {
var NullPhotonAbsorptionStrategy = require( 'MOLECULES_AND_LIGHT/photon-absorption/model/NullPhotonAbsorptionStrategy' );
var Photon = require( 'MOLECULES_AND_LIGHT/photon-absorption/model/Photon' );
var Property = require( 'AXON/Property' );
var Tandem = require( 'TANDEM/Tandem' );
var TBoolean = require( 'ifphetio!PHET_IO/types/TBoolean' );
var Vector2 = require( 'DOT/Vector2' );

// constants
Expand Down Expand Up @@ -61,7 +63,8 @@ define( function( require ) {
function Molecule( options ) {

options = _.extend( {
initialPosition: Vector2.ZERO
initialPosition: Vector2.ZERO,
tandem: Tandem.tandemOptional()
}, options );

this.highElectronicEnergyStateProperty = new BooleanProperty( false );
Expand Down Expand Up @@ -115,8 +118,8 @@ define( function( require ) {

// Boolean values that track whether the molecule is vibrating or rotating.
// @public
this.vibrating = false;
this.rotating = false;
this.vibratingProperty = new Property( false, { tandem: options.tandem.createTandem( 'vibratingProperty'), phetioValueType: TBoolean});
this.rotatingProperty = new Property( false, { tandem: options.tandem.createTandem( 'rotatingProperty'), phetioValueType: TBoolean});
this.rotationDirectionClockwise = true; // Controls the direction of rotation.

// @public, set by PhotonAbsorptionModel
Expand All @@ -141,13 +144,21 @@ define( function( require ) {
this.activePhotonAbsorptionStrategy.reset();
this.activePhotonAbsorptionStrategy = new NullPhotonAbsorptionStrategy( this );
this.absorptionHysteresisCountdownTime = 0;
this.vibrating = false;
this.rotating = false;
this.vibratingProperty.reset();
this.rotatingProperty.reset();
this.setRotation( 0 );
this.setVibration( 0 );

},

/**
* @public
*/
dispose: function(){
this.vibratingProperty.dispose();
this.rotatingProperty.dispose();
},

/**
* Set the photon absorption strategy for this molecule for a given photon wavelength.
*
Expand Down Expand Up @@ -221,11 +232,11 @@ define( function( require ) {
this.absorptionHysteresisCountdownTime -= dt;
}

if ( this.vibrating ) {
if ( this.vibratingProperty.get() ) {
this.advanceVibration( dt * VIBRATION_FREQUENCY * 2 * Math.PI );
}

if ( this.rotating ) {
if ( this.rotatingProperty.get() ) {
var directionMultiplier = this.rotationDirectionClockwise ? -1 : 1;
this.rotate( dt * ROTATION_RATE * 2 * Math.PI * directionMultiplier );
}
Expand Down Expand Up @@ -477,8 +488,6 @@ define( function( require ) {
absorptionHysteresisCountdownTime: this.absorptionHysteresisCountdownTime,
currentVibrationRadians: this.currentVibrationRadians,
currentRotationRadians: this.currentRotationRadians,
vibrating: this.vibrating,
rotating: this.rotating,
rotationDirectionClockwise: this.rotationDirectionClockwise
};
}
Expand All @@ -497,8 +506,6 @@ define( function( require ) {
molecule.absorptionHysteresisCountdownTime = stateObject.absorptionHysteresisCountdownTime;
molecule.currentVibrationRadians = stateObject.currentVibrationRadians;
molecule.currentRotationRadians = stateObject.currentRotationRadians;
molecule.vibrating = stateObject.vibrating;
molecule.rotating = stateObject.rotating;
molecule.rotationDirectionClockwise = stateObject.rotationDirectionClockwise;

// add the atoms
Expand Down
27 changes: 17 additions & 10 deletions js/photon-absorption/model/PhotonAbsorptionModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ define( function( require ) {

var self = this;

this.tandem = tandem; // @private

// @private
this.photonGroupTandem = tandem.createGroupTandem( 'photons' );

Expand Down Expand Up @@ -118,7 +120,7 @@ define( function( require ) {
// Link the model's active molecule to the photon target property. Note that this wiring must be done after the
// listeners for the activeMolecules observable array have been implemented.
self.photonTargetProperty.link( function() {
self.updateActiveMolecule( self.photonTargetProperty.get() );
self.updateActiveMolecule( self.photonTargetProperty.get(), self.tandem );
} );

// Set the photon emission period from the emission frequency.
Expand Down Expand Up @@ -362,49 +364,52 @@ define( function( require ) {
* the molecule should emit a photon or break apart into constituents.
*
* @param {string} photonTarget - The string constant which represents the desired photon target.
* @param {Tandem} tandem
*/
updateActiveMolecule: function( photonTarget ) {
updateActiveMolecule: function( photonTarget, tandem ) {

var self = this;

this.activeMolecules.forEach( function( molecule ) { molecule.dispose(); } );

// Remove the old photon target(s).
this.activeMolecules.clear(); // Clear the old active molecules array

// Add the new photon target(s).
var newMolecule;
switch( photonTarget ) {
case PhotonTarget.SINGLE_CO_MOLECULE:
newMolecule = new CO();
newMolecule = new CO( { tandem: tandem.createTandem( 'CO' ) } );
this.activeMolecules.add( newMolecule );
break;

case PhotonTarget.SINGLE_CO2_MOLECULE:
newMolecule = new CO2();
newMolecule = new CO2( { tandem: tandem.createTandem( 'CO2' ) } );
this.activeMolecules.add( newMolecule );
break;

case PhotonTarget.SINGLE_H2O_MOLECULE:
newMolecule = new H2O();
newMolecule = new H2O( { tandem: tandem.createTandem( 'H2O' ) } );
this.activeMolecules.add( newMolecule );
break;

case PhotonTarget.SINGLE_N2_MOLECULE:
newMolecule = new N2();
newMolecule = new N2( { tandem: tandem.createTandem( 'N2' ) } );
this.activeMolecules.add( newMolecule );
break;

case PhotonTarget.SINGLE_O2_MOLECULE:
newMolecule = new O2();
newMolecule = new O2( { tandem: tandem.createTandem( 'O2' ) } );
this.activeMolecules.add( newMolecule );
break;

case PhotonTarget.SINGLE_O3_MOLECULE:
newMolecule = new O3();
newMolecule = new O3( { tandem: tandem.createTandem( 'O3' ) } );
this.activeMolecules.add( newMolecule );
break;

case PhotonTarget.SINGLE_NO2_MOLECULE:
newMolecule = new NO2();
newMolecule = new NO2( { tandem: tandem.createTandem( 'NO2' ) } );
this.activeMolecules.add( newMolecule );
break;

Expand All @@ -423,6 +428,8 @@ define( function( require ) {
// Break apart into constituent molecules.
newMolecule.brokeApartEmitter.addListener( function( constituentMolecule1, constituentMolecule2 ) {
// Remove the molecule from the photonAbsorptionModel's list of active molecules.

newMolecule.dispose();
self.activeMolecules.remove( newMolecule );
// Add the constituent molecules to the photonAbsorptionModel.
self.activeMolecules.add( constituentMolecule1 );
Expand All @@ -446,7 +453,7 @@ define( function( require ) {
*/
restoreActiveMolecule: function() {
var currentTarget = this.photonTargetProperty.get();
this.updateActiveMolecule( currentTarget );
this.updateActiveMolecule( currentTarget, this.tandem );
}

} );
Expand Down
4 changes: 2 additions & 2 deletions js/photon-absorption/model/RotationStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ define( function( require ) {
photonAbsorbed: function() {

this.molecule.rotationDirectionClockwise = RAND.nextBoolean();
this.molecule.rotating = true;
this.molecule.rotatingProperty.value = true;

},

Expand All @@ -54,7 +54,7 @@ define( function( require ) {
reemitPhoton: function() {

PhotonHoldStrategy.prototype.reemitPhoton.call( this );
this.molecule.rotating = false;
this.molecule.rotatingProperty.value = false;

}

Expand Down
4 changes: 2 additions & 2 deletions js/photon-absorption/model/VibrationStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ define( function( require ) {
* Set this molecule to a vibrating state when a photon is absorbed.
*/
photonAbsorbed: function() {
this.molecule.vibrating = true;
this.molecule.vibratingProperty.value = true;
},

/**
Expand All @@ -45,7 +45,7 @@ define( function( require ) {
reemitPhoton: function() {

PhotonHoldStrategy.prototype.reemitPhoton.call( this );
this.molecule.vibrating = false;
this.molecule.vibratingProperty.value = false;
this.molecule.setVibration( 0 );
}

Expand Down

0 comments on commit f9ad4ae

Please sign in to comment.