From 9cbccee23a199378fa3a53aa0188f3fab1e4ebf4 Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Thu, 27 Apr 2023 19:26:49 -0600 Subject: [PATCH] Allow kicking while a ball is already in the air, see https://github.com/phetsims/center-and-variability/issues/160 --- js/common/model/CAVModel.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/js/common/model/CAVModel.ts b/js/common/model/CAVModel.ts index cc3e57cc..794b9d35 100644 --- a/js/common/model/CAVModel.ts +++ b/js/common/model/CAVModel.ts @@ -456,6 +456,8 @@ export default class CAVModel implements TModel { this.nextBallToKickProperty.value = this.getNextBallFromPool(); } + this.advanceLine(); + assert && assert( this.nextBallToKickProperty.value !== null, 'there was no ball to kick' ); if ( frontPlayer.poseProperty.value === Pose.STANDING ) { @@ -501,11 +503,17 @@ export default class CAVModel implements TModel { // When a ball lands, or when the next player is supposed to kick (before the ball lands), move the line forward private advanceLine(): void { - let nextIndex = this.activeKickerIndexProperty.value + 1; - if ( nextIndex > this.maxNumberOfObjects ) { - nextIndex = 0; + // Allow kicking another ball while one is already in the air. + // if the previous ball was still in the air, we need to move the line forward so the next player can kick + const kickers = this.soccerPlayers.filter( soccerPlayer => soccerPlayer.isActiveProperty.value && + soccerPlayer.poseProperty.value === Pose.KICKING ); + if ( kickers.length > 0 ) { + let nextIndex = this.activeKickerIndexProperty.value + 1; + if ( nextIndex > this.maxNumberOfObjects ) { + nextIndex = 0; + } + this.activeKickerIndexProperty.value = nextIndex; } - this.activeKickerIndexProperty.value = nextIndex; } private static chooseDistribution(): ReadonlyArray {