Skip to content

Commit

Permalink
Convert method to emitter, see #45
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Mar 4, 2022
1 parent edca9c5 commit 103b83e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
15 changes: 9 additions & 6 deletions js/common/model/CASModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class CASModel {
private lastHighlightAnimationStepTime: number;
readonly isMedianAnimationCompleteProperty: BooleanProperty;

// TODO: See if TypeScript 4.6 will let us initialize more things here
protected readonly objectValueBecameNonNullEmitter: Emitter<[ CASObject ]>;

constructor( objectType: CASObjectType, maxNumberOfObjects: number, providedOptions: CASModelOptions ) {

const options = optionize<CASModelOptions, CASModelSelfOptions, {}>( {
Expand Down Expand Up @@ -176,7 +179,7 @@ class CASModel {
if ( options.includeCards ) {
this.cardModelGroup.createNextElement( casObject );
}
this.objectValueBecameNonNull( casObject );
this.objectValueBecameNonNullEmitter.emit( casObject );
casObject.valueProperty.unlink( listener ); // Only create the card once, then no need to listen further
}
};
Expand Down Expand Up @@ -214,6 +217,11 @@ class CASModel {
this.isMedianAnimationCompleteProperty.value = false;
}
} );

this.objectValueBecameNonNullEmitter = new Emitter<[ CASObject ]>( {
parameters: [ { valueType: CASObject } ]
} );
this.objectValueBecameNonNullEmitter.addListener( () => this.updateAnimation() );
}

updateMeanAndMedian(): void {
Expand Down Expand Up @@ -336,11 +344,6 @@ class CASModel {
} );
}

// TODO: Should this be an emitter? We say yes.
protected objectValueBecameNonNull( casObject: CASObject ): void {
this.updateAnimation();
}

/**
* Clears out the data and the cards
*/
Expand Down
25 changes: 12 additions & 13 deletions js/common/model/SoccerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ class SoccerModel extends CASModel {
numberOfRemainingKickableObjects => numberOfRemainingKickableObjects > 0 );

this.currentDistribution = SoccerModel.chooseDistribution();

this.objectValueBecameNonNullEmitter.addListener( casObject => {

// If the soccer player that kicked that ball was still in line when the ball lands, they can leave the line now.
if ( this.soccerPlayerGroup.includes( this.ballPlayerMap.get( casObject )! ) ) {
this.advanceLine();
}

if ( this.numberOfRemainingObjectsProperty.value > 0 && this.nextBallToKickProperty.value === null ) {
this.nextBallToKickProperty.value = this.createBall();
}
} );
}

static chooseDistribution(): number[] {
Expand Down Expand Up @@ -267,19 +279,6 @@ class SoccerModel extends CASModel {
}
}

protected objectValueBecameNonNull( casObject: CASObject ): void {
super.objectValueBecameNonNull( casObject );

// If the soccer player that kicked that ball was still in line when the ball lands, they can leave the line now.
if ( this.soccerPlayerGroup.includes( this.ballPlayerMap.get( casObject )! ) ) {
this.advanceLine();
}

if ( this.numberOfRemainingObjectsProperty.value > 0 && this.nextBallToKickProperty.value === null ) {
this.nextBallToKickProperty.value = this.createBall();
}
}

// When a ball lands, or when the next player is supposed to kick (before the ball lands), move the line forward
private advanceLine(): void {

Expand Down

0 comments on commit 103b83e

Please sign in to comment.