Skip to content

Commit

Permalink
add photonAddedEmitter and photonRemovedEmitter to PhotonSystem, #47
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jan 18, 2025
1 parent 9ef98d5 commit 1f7f872
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
34 changes: 23 additions & 11 deletions js/common/model/PhotonSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import HydrogenAtom from './HydrogenAtom.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import { Color } from '../../../../scenery/js/imports.js';
import Emitter from '../../../../axon/js/Emitter.js';

export default class PhotonSystem extends PhetioObject {

Expand All @@ -28,6 +29,10 @@ export default class PhotonSystem extends PhetioObject {
// the collection of photons that appear in the zoomed-in box
private readonly photons: ObservableArray<Photon>;

// notify when a photon is added or removed
public readonly photonAddedEmitter: Emitter<[ Photon ]>;
public readonly photonRemovedEmitter: Emitter<[ Photon ]>;

public constructor( zoomedInBox: ZoomedInBox, hydrogenAtomProperty: TReadOnlyProperty<HydrogenAtom>, tandem: Tandem ) {

super( {
Expand All @@ -41,6 +46,13 @@ export default class PhotonSystem extends PhetioObject {

//TODO https://github.com/phetsims/models-of-the-hydrogen-atom/issues/47 replace ObservableArray
this.photons = createObservableArray<Photon>();

this.photonAddedEmitter = new Emitter<[ Photon ]>( {
parameters: [ { name: 'photon', valueType: Photon } ]
} );
this.photonRemovedEmitter = new Emitter<[ Photon ]>( {
parameters: [ { name: 'photon', valueType: Photon } ]
} );
}

public reset(): void {
Expand All @@ -51,7 +63,7 @@ export default class PhotonSystem extends PhetioObject {
* Emits a photon from the light source.
*/
public emitPhotonFromLight( wavelength: number, position: Vector2, direction: number ): void {
this.photons.add( new Photon( {
this.addPhoton( new Photon( {
wavelength: wavelength,
position: position,
direction: direction
Expand All @@ -62,7 +74,7 @@ export default class PhotonSystem extends PhetioObject {
* Emits a photon from the hydrogen atom.
*/
public emitPhotonFromAtom( wavelength: number, position: Vector2, direction: number, debugHaloColor: Color ): void {
this.photons.add( new Photon( {
this.addPhoton( new Photon( {
wavelength: wavelength,
position: position,
direction: direction,
Expand All @@ -71,26 +83,26 @@ export default class PhotonSystem extends PhetioObject {
} ) );
}

private addPhoton( photon: Photon ): void {
this.photons.add( photon );
this.photonAddedEmitter.emit( photon );
}

public removePhoton( photon: Photon ): void {
assert && assert( this.photons.includes( photon ), 'Attempted to remove a photon that does not exist.' );
this.photons.remove( photon );
photon.dispose();
this.photonRemovedEmitter.emit( photon );
}

public removeAllPhotons(): void {
while ( this.photons.length > 0 ) {
this.photons.pop()!.dispose();
const photon = this.photons.pop()!;
photon.dispose();
this.photonRemovedEmitter.emit( photon );
}
}

public addPhotonAddedListener( listener: ( photon: Photon ) => void ): void {
this.photons.addItemAddedListener( listener );
}

public addPhotonRemovedListener( listener: ( photon: Photon ) => void ): void {
this.photons.addItemRemovedListener( listener );
}

/**
* Advances the state of the photons.
* @param dt - the time step, in seconds
Expand Down
4 changes: 2 additions & 2 deletions js/common/view/ZoomedInBoxNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export default class ZoomedInBoxNode extends Node {
const photonNodes: PhotonNode[] = [];

// Add the PhotonNode for a Photon.
photonSystem.addPhotonAddedListener( photon => {
photonSystem.photonAddedEmitter.addListener( photon => {
const photonNode = new PhotonNode( photon, modelViewTransform );
photonNodes.push( photonNode );
photonsLayer.addChild( photonNode );
} );

// Remove the PhotonNode for a Photon.
photonSystem.addPhotonRemovedListener( photon => {
photonSystem.photonRemovedEmitter.addListener( photon => {
const photonNode = _.find( photonNodes, photonNode => ( photonNode.photon === photon ) )!;
assert && assert( photonNode );
photonNodes.splice( photonNodes.indexOf( photonNode ), 1 );
Expand Down

0 comments on commit 1f7f872

Please sign in to comment.