Skip to content

Commit 26a0ddd

Browse files
samreidjessegreenberg
authored andcommitted
Use PhetioGroup instead of ObservableArray of photons, see phetsims/molecules-and-light#353
1 parent 93dc5da commit 26a0ddd

4 files changed

+13
-20
lines changed

js/moleculesandlight/view/MoleculesAndLightScreenView.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class MoleculesAndLightScreenView extends ScreenView {
257257
) );
258258

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

262262
// We may want to disable the PlayPauseButton when the light source is off, but we aren't sure yet. This will let
263263
// us test behavior and make sure it doesn't have other UX impacts

js/moleculesandlight/view/ObservationWindow.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function ObservationWindow( photonAbsorptionModel, modelViewTransform, tandem )
171171
photonAbsorptionModel.activeMolecules.addItemAddedListener( addMoleculeToWindow );
172172

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

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

184-
photonAbsorptionModel.photons.addItemRemovedListener( function removalListener( removedPhoton ) {
184+
photonAbsorptionModel.photonGroup.elementDisposedEmitter.addListener( function removalListener( removedPhoton ) {
185185
if ( removedPhoton === addedPhoton ) {
186186
addedPhoton.positionProperty.hasListener( photonPositionObserver ) && addedPhoton.positionProperty.unlink( photonPositionObserver );
187187
photonLayer.removeChild( photonNode );
188-
photonAbsorptionModel.photons.removeItemRemovedListener( removalListener );
188+
photonAbsorptionModel.photonGroup.elementDisposedEmitter.removeListener( removalListener );
189189
}
190190
} );
191191
} );
@@ -282,18 +282,11 @@ inherit( Rectangle, ObservationWindow, {
282282
*/
283283
photonCheckBounds: function() {
284284

285-
const photonsToRemove = [];
286-
for ( let photon = 0; photon < this.photonAbsorptionModel.photons.length; photon++ ) {
287-
if ( !this.particleRemovalBounds.containsPoint( this.modelViewTransform.modelToViewPosition( this.photonAbsorptionModel.photons.get( photon ).positionProperty.get() ) ) ) {
288-
photonsToRemove.push( this.photonAbsorptionModel.photons.get( photon ) );
289-
}
290-
}
291-
this.photonAbsorptionModel.photons.removeAll( photonsToRemove );
292-
293-
// dispose all photons that leave the observation window
294-
for ( let i = 0; i < photonsToRemove.length; i++ ) {
295-
this.photonAbsorptionModel.photonGroup.disposeElement( photonsToRemove[ i ] );
296-
}
285+
const photonsToRemove = this.photonAbsorptionModel.photonGroup.filter( photon => {
286+
const position = this.modelViewTransform.modelToViewPosition( photon.positionProperty.get() );
287+
return !this.particleRemovalBounds.containsPoint( position );
288+
} );
289+
photonsToRemove.forEach( photon => this.photonAbsorptionModel.photonGroup.disposeElement( photon ) );
297290
},
298291

299292
/**

js/moleculesandlight/view/ObservationWindowAlertManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class ObservationWindowAlertManager {
205205
let alert = null;
206206

207207
const emitterOn = model.photonEmitterOnProperty.get();
208-
const hasPhotons = model.photons.length > 0;
208+
const hasPhotons = model.photonGroup.count > 0;
209209
const targetMolecule = model.targetMolecule;
210210

211211
if ( targetMolecule ) {

js/moleculesandlight/view/PhotonEmissionSoundGenerator.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ const PLAY_MOLECULE_EMISSION_X_POSITION = 0;
3838
class PhotonEmissionSoundGenerator extends SoundGenerator {
3939

4040
/**
41-
* @param {ObservableArrayDef<Photon>} photons
41+
* @param {PhetioGroup} photonGroup
4242
* @param {Object} [options]
4343
*/
44-
constructor( photons, options ) {
44+
constructor( photonGroup, options ) {
4545

4646
options = merge( {}, options );
4747
super( options );
@@ -96,7 +96,7 @@ class PhotonEmissionSoundGenerator extends SoundGenerator {
9696
} );
9797

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

102102
if ( photonXPosition === PLAY_MOLECULE_EMISSION_X_POSITION ) {

0 commit comments

Comments
 (0)