From ea664b67e789c56a7885874d1244cc8f93ba37f1 Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Fri, 18 Aug 2023 10:51:37 -0600 Subject: [PATCH] move user control logic into the model, fixes https://github.com/phetsims/build-a-nucleus/issues/133 --- js/common/model/BANModel.ts | 23 +++++++++++++++++++++++ js/common/view/BANScreenView.ts | 20 -------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/js/common/model/BANModel.ts b/js/common/model/BANModel.ts index 588613b..7dd4e11 100644 --- a/js/common/model/BANModel.ts +++ b/js/common/model/BANModel.ts @@ -105,6 +105,29 @@ class BANModel { ( 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() ); } diff --git a/js/common/view/BANScreenView.ts b/js/common/view/BANScreenView.ts index 55b4450..f2837c3 100644 --- a/js/common/view/BANScreenView.ts +++ b/js/common/view/BANScreenView.ts @@ -454,24 +454,6 @@ abstract class BANScreenView> } ); 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 ) => { @@ -488,8 +470,6 @@ abstract class BANScreenView> this.checkIfCreatorNodeShouldBeInvisible( particleType ); } - particle.userControlledProperty.link( isUserControlled => userControlledListener( isUserControlled, particle ) ); - particle.disposeEmitter.addListener( () => { delete this.particleViewMap[ particle.id ];