Skip to content

Commit

Permalink
Use PhetioGroup instead of ObservableArray of photons, see phetsims/m…
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid authored and jessegreenberg committed Apr 21, 2021
1 parent 93dc5da commit 26a0ddd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion js/moleculesandlight/view/MoleculesAndLightScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class MoleculesAndLightScreenView extends ScreenView {
) );

// add the sound generator that will produce the sounds when photons are emitted by the lamps or the active molecule
soundManager.addSoundGenerator( new PhotonEmissionSoundGenerator( photonAbsorptionModel.photons ) );
soundManager.addSoundGenerator( new PhotonEmissionSoundGenerator( photonAbsorptionModel.photonGroup ) );

// We may want to disable the PlayPauseButton when the light source is off, but we aren't sure yet. This will let
// us test behavior and make sure it doesn't have other UX impacts
Expand Down
23 changes: 8 additions & 15 deletions js/moleculesandlight/view/ObservationWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function ObservationWindow( photonAbsorptionModel, modelViewTransform, tandem )
photonAbsorptionModel.activeMolecules.addItemAddedListener( addMoleculeToWindow );

// Set up the event listeners for adding and removing photons.
photonAbsorptionModel.photons.addItemAddedListener( function( addedPhoton ) {
photonAbsorptionModel.photonGroup.elementCreatedEmitter.addListener( function( addedPhoton ) {
const photonNode = new PhotonNode( addedPhoton, self.modelViewTransform );
photonLayer.addChild( photonNode );

Expand All @@ -181,11 +181,11 @@ function ObservationWindow( photonAbsorptionModel, modelViewTransform, tandem )
};
addedPhoton.positionProperty.link( photonPositionObserver );

photonAbsorptionModel.photons.addItemRemovedListener( function removalListener( removedPhoton ) {
photonAbsorptionModel.photonGroup.elementDisposedEmitter.addListener( function removalListener( removedPhoton ) {
if ( removedPhoton === addedPhoton ) {
addedPhoton.positionProperty.hasListener( photonPositionObserver ) && addedPhoton.positionProperty.unlink( photonPositionObserver );
photonLayer.removeChild( photonNode );
photonAbsorptionModel.photons.removeItemRemovedListener( removalListener );
photonAbsorptionModel.photonGroup.elementDisposedEmitter.removeListener( removalListener );
}
} );
} );
Expand Down Expand Up @@ -282,18 +282,11 @@ inherit( Rectangle, ObservationWindow, {
*/
photonCheckBounds: function() {

const photonsToRemove = [];
for ( let photon = 0; photon < this.photonAbsorptionModel.photons.length; photon++ ) {
if ( !this.particleRemovalBounds.containsPoint( this.modelViewTransform.modelToViewPosition( this.photonAbsorptionModel.photons.get( photon ).positionProperty.get() ) ) ) {
photonsToRemove.push( this.photonAbsorptionModel.photons.get( photon ) );
}
}
this.photonAbsorptionModel.photons.removeAll( photonsToRemove );

// dispose all photons that leave the observation window
for ( let i = 0; i < photonsToRemove.length; i++ ) {
this.photonAbsorptionModel.photonGroup.disposeElement( photonsToRemove[ i ] );
}
const photonsToRemove = this.photonAbsorptionModel.photonGroup.filter( photon => {
const position = this.modelViewTransform.modelToViewPosition( photon.positionProperty.get() );
return !this.particleRemovalBounds.containsPoint( position );
} );
photonsToRemove.forEach( photon => this.photonAbsorptionModel.photonGroup.disposeElement( photon ) );
},

/**
Expand Down
2 changes: 1 addition & 1 deletion js/moleculesandlight/view/ObservationWindowAlertManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class ObservationWindowAlertManager {
let alert = null;

const emitterOn = model.photonEmitterOnProperty.get();
const hasPhotons = model.photons.length > 0;
const hasPhotons = model.photonGroup.count > 0;
const targetMolecule = model.targetMolecule;

if ( targetMolecule ) {
Expand Down
6 changes: 3 additions & 3 deletions js/moleculesandlight/view/PhotonEmissionSoundGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const PLAY_MOLECULE_EMISSION_X_POSITION = 0;
class PhotonEmissionSoundGenerator extends SoundGenerator {

/**
* @param {ObservableArrayDef<Photon>} photons
* @param {PhetioGroup} photonGroup
* @param {Object} [options]
*/
constructor( photons, options ) {
constructor( photonGroup, options ) {

options = merge( {}, options );
super( options );
Expand Down Expand Up @@ -96,7 +96,7 @@ class PhotonEmissionSoundGenerator extends SoundGenerator {
} );

// listen for new photons and play sounds or set them up to be played later when appropriate
photons.addItemAddedListener( photon => {
photonGroup.elementCreatedEmitter.addListener( photon => {
const photonXPosition = photon.positionProperty.value.x;

if ( photonXPosition === PLAY_MOLECULE_EMISSION_X_POSITION ) {
Expand Down

0 comments on commit 26a0ddd

Please sign in to comment.