Skip to content

Commit

Permalink
move user control logic into the model, fixes #133
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Aug 18, 2023
1 parent fb9418f commit ea664b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
23 changes: 23 additions & 0 deletions js/common/model/BANModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ class BANModel<T extends ParticleAtom> {
( protonNumber: number, neutronNumber: number ) => AtomIdentifier.doesExist( protonNumber, neutronNumber )
);

const userControlledListener = ( isUserControlled: boolean, particle: Particle ) => {
if ( isUserControlled && this.particleAtom.containsParticle( particle ) ) {
this.particleAtom.removeParticle( particle );
}

if ( isUserControlled && particle.type === ParticleType.PROTON.particleTypeString && !this.userControlledProtons.includes( particle ) ) {
this.userControlledProtons.add( particle );
}
else if ( !isUserControlled && particle.type === ParticleType.PROTON.particleTypeString && this.userControlledProtons.includes( particle ) ) {
this.userControlledProtons.remove( particle );
}
else if ( isUserControlled && particle.type === ParticleType.NEUTRON.particleTypeString && !this.userControlledNeutrons.includes( particle ) ) {
this.userControlledNeutrons.add( particle );
}
else if ( !isUserControlled && particle.type === ParticleType.NEUTRON.particleTypeString && this.userControlledNeutrons.includes( particle ) ) {
this.userControlledNeutrons.remove( particle );
}
};

this.particles.addItemAddedListener( particle => {
particle.userControlledProperty.link( isUserControlled => userControlledListener( isUserControlled, particle ) );
} );

// reconfigure the nucleus when the massNumber changes
this.particleAtom.massNumberProperty.link( () => this.particleAtom.reconfigureNucleus() );
}
Expand Down
20 changes: 0 additions & 20 deletions js/common/view/BANScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,24 +454,6 @@ abstract class BANScreenView<M extends BANModel<ParticleAtom | ParticleNucleus>>
} );
this.addChild( this.resetAllButton );

const userControlledListener = ( isUserControlled: boolean, particle: Particle ) => {
if ( isUserControlled && this.model.particleAtom.containsParticle( particle ) ) {
this.model.particleAtom.removeParticle( particle );
}

if ( isUserControlled && particle.type === ParticleType.PROTON.particleTypeString && !this.model.userControlledProtons.includes( particle ) ) {
this.model.userControlledProtons.add( particle );
}
else if ( !isUserControlled && particle.type === ParticleType.PROTON.particleTypeString && this.model.userControlledProtons.includes( particle ) ) {
this.model.userControlledProtons.remove( particle );
}
else if ( isUserControlled && particle.type === ParticleType.NEUTRON.particleTypeString && !this.model.userControlledNeutrons.includes( particle ) ) {
this.model.userControlledNeutrons.add( particle );
}
else if ( !isUserControlled && particle.type === ParticleType.NEUTRON.particleTypeString && this.model.userControlledNeutrons.includes( particle ) ) {
this.model.userControlledNeutrons.remove( particle );
}
};

// add ParticleView's to match the model
this.model.particles.addItemAddedListener( ( particle: Particle ) => {
Expand All @@ -488,8 +470,6 @@ abstract class BANScreenView<M extends BANModel<ParticleAtom | ParticleNucleus>>
this.checkIfCreatorNodeShouldBeInvisible( particleType );
}

particle.userControlledProperty.link( isUserControlled => userControlledListener( isUserControlled, particle ) );

particle.disposeEmitter.addListener( () => {
delete this.particleViewMap[ particle.id ];

Expand Down

0 comments on commit ea664b6

Please sign in to comment.