Skip to content

Commit 02a0178

Browse files
samreidjessegreenberg
authored andcommitted
Use PhetioGroup instead of ObservableArray of photons, see phetsims/molecules-and-light#353
1 parent 5f7ec84 commit 02a0178

File tree

1 file changed

+6
-62
lines changed

1 file changed

+6
-62
lines changed

js/photon-absorption/model/PhotonAbsorptionModel.js

+6-62
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import inherit from '../../../../phet-core/js/inherit.js';
2828
import TimeSpeed from '../../../../scenery-phet/js/TimeSpeed.js';
2929
import PhetioGroup from '../../../../tandem/js/PhetioGroup.js';
3030
import PhetioObject from '../../../../tandem/js/PhetioObject.js';
31-
import IOType from '../../../../tandem/js/types/IOType.js';
3231
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
3332
import moleculesAndLight from '../../moleculesAndLight.js';
3433
import Molecule from './Molecule.js';
@@ -128,12 +127,6 @@ function PhotonAbsorptionModel( initialPhotonTarget, tandem ) {
128127
// @public - convenience Property, indicating whether sim is running in slow motion
129128
this.slowMotionProperty = new DerivedProperty( [ this.timeSpeedProperty ], speed => speed === TimeSpeed.SLOW );
130129

131-
// @public
132-
this.photons = createObservableArray( {
133-
tandem: tandem.createTandem( 'photons' ),
134-
phetioType: createObservableArray.ObservableArrayIO( Photon.PhotonIO )
135-
} ); // Elements are of type Photon
136-
137130
this.activeMolecules = createObservableArray( {
138131
tandem: tandem.createTandem( 'molecules' ),
139132
phetioType: createObservableArray.ObservableArrayIO( Molecule.MoleculeIO )
@@ -162,11 +155,7 @@ function PhotonAbsorptionModel( initialPhotonTarget, tandem ) {
162155
this.photonEmissionCountdownTimer = Number.POSITIVE_INFINITY; // @private
163156
this.photonEmissionPeriodTarget = DEFAULT_PHOTON_EMISSION_PERIOD; // @private
164157

165-
PhetioObject.call( this, {
166-
tandem: tandem,
167-
phetioType: PhotonAbsorptionModel.PhotonAbsorptionModelIO,
168-
phetioState: false
169-
} );
158+
PhetioObject.call( this );
170159
}
171160

172161
moleculesAndLight.register( 'PhotonAbsorptionModel', PhotonAbsorptionModel );
@@ -206,9 +195,6 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
206195
*/
207196
resetPhotons() {
208197

209-
// Remove and dispose any photons that are currently in transit.
210-
this.photons.clear();
211-
212198
// If setting state, the state engine will do this step.
213199
if ( !phet.joist.sim.isSettingPhetioStateProperty.value ) {
214200
this.photonGroup.clear();
@@ -282,8 +268,8 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
282268
const photonsToRemove = [];
283269

284270
// check for possible interaction between each photon and molecule
285-
this.photons.forEach( function( photon ) {
286-
self.activeMolecules.forEach( function( molecule ) {
271+
this.photonGroup.forEach( photon => {
272+
self.activeMolecules.forEach( molecule => {
287273
if ( molecule.queryAbsorbPhoton( photon ) ) {
288274

289275
// the photon was absorbed, so put it on the removal list
@@ -294,15 +280,11 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
294280
} );
295281

296282
// Remove any photons that were marked for removal.
297-
this.photons.removeAll( photonsToRemove );
298-
for ( let i = 0; i < photonsToRemove.length; i++ ) {
299-
this.photonGroup.disposeElement( photonsToRemove[ i ] );
300-
}
283+
photonsToRemove.forEach( photon => this.photonGroup.disposeElement( photon ) );
301284
},
302285

303286
clearPhotons: function() {
304287
this.photonGroup.clear();
305-
this.photons.clear();
306288
},
307289

308290
/**
@@ -349,7 +331,6 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
349331
photon.positionProperty.set( new Vector2( PHOTON_EMISSION_POSITION.x + PHOTON_VELOCITY * advanceAmount, PHOTON_EMISSION_POSITION.y ) );
350332
const emissionAngle = 0; // Straight to the right.
351333
photon.setVelocity( PHOTON_VELOCITY * Math.cos( emissionAngle ), PHOTON_VELOCITY * Math.sin( emissionAngle ) );
352-
this.photons.add( photon );
353334

354335
// indicate that a photon has been emitted from the initial emission point
355336
this.photonEmittedEmitter.emit( photon );
@@ -390,7 +371,8 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
390371
// so that the user doesn't have to wait too long in order to see something come out, but only if there
391372
// are no other photons in the observation window so we don't emit unlimitted photons when turning
392373
// on/off rapidly
393-
if ( this.photonEmissionPeriodTarget === Number.POSITIVE_INFINITY && photonEmissionPeriod !== Number.POSITIVE_INFINITY && this.photons.length === 0 ) {
374+
if ( this.photonEmissionPeriodTarget === Number.POSITIVE_INFINITY && photonEmissionPeriod !== Number.POSITIVE_INFINITY
375+
&& this.photonGroup.count === 0 ) {
394376

395377
// only reset time on emission of first photon, there should still be a delay after subsequent photons
396378
this.setEmissionTimerToInitialCountdown();
@@ -444,11 +426,6 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
444426
// Set the photonGroup so that photons created by the molecule can be registered for PhET-iO
445427
newMolecule.photonGroup = this.photonGroup;
446428

447-
// Emit a new photon from this molecule after absorption.
448-
newMolecule.photonEmittedEmitter.addListener( function( photon ) {
449-
self.photons.add( photon );
450-
} );
451-
452429
// Break apart into constituent molecules.
453430
newMolecule.brokeApartEmitter.addListener( function( constituentMolecule1, constituentMolecule2 ) {
454431
// Remove the molecule from the photonAbsorptionModel's list of active molecules.
@@ -495,40 +472,7 @@ inherit( PhetioObject, PhotonAbsorptionModel, {
495472
}
496473
} );
497474

498-
499475
// @public {number} - horizontal velocity of photons when they leave the emitter, in picometers/second
500476
PhotonAbsorptionModel.PHOTON_VELOCITY = PHOTON_VELOCITY;
501477

502-
PhotonAbsorptionModel.PhotonAbsorptionModelIO = new IOType( 'PhotonAbsorptionModelIO', {
503-
valueType: PhotonAbsorptionModel,
504-
505-
/**
506-
* @public
507-
* @param photonAbsorptionModel
508-
* TODO: eliminate this legacy pattern, see https://github.com/phetsims/tandem/issues/87
509-
*/
510-
clearChildInstances( photonAbsorptionModel ) {
511-
photonAbsorptionModel.clearPhotons();
512-
// instance.chargedParticles.clear();
513-
// instance.electricFieldSensors.clear();
514-
},
515-
516-
/**
517-
* Create a dynamic particle as specified by the phetioID and state.
518-
* @public
519-
* @param {Object} photonAbsorptionModel
520-
* @param {Tandem} tandem
521-
* @param {Object} stateObject
522-
* @returns {ChargedParticle}
523-
*/
524-
addChildElementDeprecated( photonAbsorptionModel, tandem, stateObject ) {
525-
const value = Photon.PhotonIO.fromStateObject( stateObject );
526-
527-
const photon = new phet.moleculesAndLight.Photon( value.wavelength, tandem );
528-
photon.setVelocity( stateObject.vx, stateObject.vy );
529-
photonAbsorptionModel.photons.add( photon );
530-
return photon;
531-
}
532-
} );
533-
534478
export default PhotonAbsorptionModel;

0 commit comments

Comments
 (0)