Skip to content

Commit

Permalink
factor out function to avoide eslint disable, see #270 and #301
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Jun 18, 2024
1 parent 6f99f59 commit fd292b7
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions js/level-out/model/LevelOutModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,23 @@ export default class LevelOutModel extends PhetioObject implements TModel {
* @param waterDelta - the amount of water added or removed
*/
private distributeWaterRipple( connectedCups: Array<Cup>, targetCup: Cup, waterDelta: number ): void {

// Define the callback function outside of the loop
const updateNeighborWaterLevel = ( neighbor: Cup, fraction: number ) => {
waterDelta -= fraction;

const proposedValue = neighbor.waterLevelProperty.value + fraction;
neighbor.waterLevelProperty.value = Utils.clamp( proposedValue, 0, 1 );
};

// Loop through neighbors with target cup at center
for ( let i = 1; i < 7; i++ ) {
const neighbors = connectedCups.filter( cup => Math.abs( targetCup.linePlacement - cup.linePlacement ) === i );

// the larger the denominator the more subtle the ripple
const fraction = waterDelta / ( i * 5 );

// eslint-disable-next-line @typescript-eslint/no-loop-func
neighbors.forEach( neighbor => {
waterDelta -= fraction;

const proposedValue = neighbor.waterLevelProperty.value + fraction;
neighbor.waterLevelProperty.value = Utils.clamp( proposedValue, 0, 1 );
} );
neighbors.forEach( neighbor => updateNeighborWaterLevel( neighbor, fraction ) );
}
}

Expand Down

0 comments on commit fd292b7

Please sign in to comment.