From 413060def47e8f9bd9f92b30438c73ce5d057035 Mon Sep 17 00:00:00 2001 From: jbphet Date: Wed, 15 May 2024 16:32:54 -0600 Subject: [PATCH] reduce amount of apple movement for collect->share, see https://github.com/phetsims/mean-share-and-balance/issues/219 --- js/common/model/SharingModel.ts | 2 ++ js/fair-share/model/FairShareModel.ts | 45 ++++++++++++++++----------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/js/common/model/SharingModel.ts b/js/common/model/SharingModel.ts index d22bd32..d51c9cd 100644 --- a/js/common/model/SharingModel.ts +++ b/js/common/model/SharingModel.ts @@ -282,6 +282,8 @@ export default class SharingModel implements TModel { // Reset the active plates, whereupon they will grab and position snacks to match their initial table snack number. this.plates.forEach( plate => plate.isActiveProperty.value && plate.reset() ); } + + public static readonly INTER_PLATE_DISTANCE = INTER_PLATE_DISTANCE; } meanShareAndBalance.register( 'SharingModel', SharingModel ); \ No newline at end of file diff --git a/js/fair-share/model/FairShareModel.ts b/js/fair-share/model/FairShareModel.ts index ab22ed6..b84d035 100644 --- a/js/fair-share/model/FairShareModel.ts +++ b/js/fair-share/model/FairShareModel.ts @@ -46,6 +46,9 @@ const INTER_STACKED_GROUP_SPACING = 20; const COLLECTION_BOTTOM_Y = 10; // in screen coords, empirically determined const APPLE_FRACTION_DISTRIBUTION_DELAY = 0.75; // in seconds const INITIAL_PLATE_VALUES = [ 2, 1, 6, 2, 10, 5, 8 ]; +const PLATE_MIN_X_POSITION = -SharingModel.INTER_PLATE_DISTANCE * + MeanShareAndBalanceConstants.MAXIMUM_NUMBER_OF_DATA_SETS / 2; +const SORT_REFERENCE_POINT = new Vector2( PLATE_MIN_X_POSITION, 0 ); // Size of the collection area, empirically determined. Could be derived from other constants, but didn't seem worth it. const COLLECTION_AREA_SIZE = new Dimension2( 410, 120 ); @@ -202,7 +205,6 @@ export default class FairShareModel extends SharingModel { else if ( previousDistributionMode === DistributionMode.COLLECT && appleDistributionMode === DistributionMode.SHARE ) { this.animateAddedSnacks = true; const numberOfWholeApplesPerActivePlate = Math.floor( this.meanProperty.value ); - const activePlates = this.getActivePlates(); const applesAtTop: Apple[] = []; // We want to identify the index of the middle apple traveling to the top of the collection area so that @@ -211,28 +213,33 @@ export default class FairShareModel extends SharingModel { ( numberOfWholeApplesPerActivePlate * this.numberOfPlatesProperty.value ); const middleAppleIndex = Math.floor( numberOfRemainingApples / 2 ); - // Iterate over the apples in the collection, moving them to the appropriate plates or to the top of the notepad. - this.appleCollection.forEach( ( apple, i ) => { + // Sort the apples by their distance from the left side to minimize the distance they need to travel. + this.appleCollection.sort( ( a1, a2 ) => + a2.positionProperty.value.distance( SORT_REFERENCE_POINT ) - + a1.positionProperty.value.distance( SORT_REFERENCE_POINT ) + ); - // Move the whole apples to the appropriate plates. - if ( i < numberOfWholeApplesPerActivePlate * activePlates.length ) { - const plate = activePlates[ i % activePlates.length ]; - plate.addSnackToTop( apple ); - } - else { + // Move the whole apples to the active plates. + this.getActivePlates().forEach( plate => { + _.times( numberOfWholeApplesPerActivePlate, () => { + const apple = this.appleCollection.pop(); + assert && assert( apple, 'There should be enough apples to add the wholes ones to each plate.' ); + plate.addSnackToTop( apple! ); + } ); + } ); - // The remaining apples are moved in an animation to the top of the notepad. - applesAtTop.push( apple ); - apple.moveTo( new Vector2( - ( applesAtTop.length - middleAppleIndex - 1 ) * ( MeanShareAndBalanceConstants.APPLE_GRAPHIC_RADIUS * 2 + HORIZONTAL_SPACE_BETWEEN_APPLES_IN_COLLECTION ), - -( COLLECTION_AREA_SIZE.height + COLLECTION_BOTTOM_Y ) - ), - true - ); - } + // The remaining apples are moved in an animation to the top of the notepad. + this.appleCollection.forEach( apple => { + applesAtTop.push( apple ); + apple.moveTo( new Vector2( + ( applesAtTop.length - middleAppleIndex - 1 ) * ( MeanShareAndBalanceConstants.APPLE_GRAPHIC_RADIUS * 2 + HORIZONTAL_SPACE_BETWEEN_APPLES_IN_COLLECTION ), + -( COLLECTION_AREA_SIZE.height + COLLECTION_BOTTOM_Y ) + ), + true + ); } ); - // Mark the collection as empty now that the apples have all been moved to plates. + // Mark the collection as empty now that the apples have all been moved out of the collection. this.appleCollection.length = 0; // Any apples that were moved to the top are subsequently turned into fractional representations and distributed