Skip to content

Commit

Permalink
Improve kick-scheduling logic in CAVModel step function - see #160
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-blackman committed Apr 28, 2023
1 parent 86205ff commit d682ac3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions js/common/model/CAVModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class CAVModel implements TModel {

private readonly nextBallToKickProperty: Property<CAVObject | null>; // Null if there is no more ball to kick
private readonly numberOfScheduledSoccerBallsToKickProperty: NumberProperty;
public readonly numberOfRemainingKickableSoccerBallsProperty: TReadOnlyProperty<number>;
public readonly numberOfUnkickedBallsProperty: TReadOnlyProperty<number>;
public readonly hasKickableSoccerBallsProperty: TReadOnlyProperty<boolean>;
private readonly timeWhenLastBallWasKickedProperty: NumberProperty;
protected readonly distributionProperty: Property<ReadonlyArray<number>>;
Expand Down Expand Up @@ -217,7 +217,7 @@ export default class CAVModel implements TModel {
phetioValueType: NullableIO( ReferenceIO( CAVObject.CAVObjectIO ) )
} );

this.numberOfRemainingKickableSoccerBallsProperty = DerivedProperty.deriveAny( [
this.numberOfUnkickedBallsProperty = DerivedProperty.deriveAny( [
this.numberOfScheduledSoccerBallsToKickProperty,
...this.soccerBallGroup.map( soccerBall => soccerBall.valueProperty ),
...this.soccerBallGroup.map( soccerBall => soccerBall.animationModeProperty ) ], () => {
Expand All @@ -232,8 +232,8 @@ export default class CAVModel implements TModel {
return value;
} );

this.hasKickableSoccerBallsProperty = new DerivedProperty( [ this.numberOfRemainingKickableSoccerBallsProperty ],
numberOfRemainingKickableObjects => numberOfRemainingKickableObjects > 0 );
this.hasKickableSoccerBallsProperty = new DerivedProperty( [ this.numberOfUnkickedBallsProperty ],
numberOfUnkickedBalls => numberOfUnkickedBalls > 0 );

this.distributionProperty = new Property( CAVModel.chooseDistribution(), {
tandem: options.tandem.createTandem( 'distributionProperty' ),
Expand All @@ -248,7 +248,7 @@ export default class CAVModel implements TModel {
// If the soccer player that kicked that ball was still in line when the ball lands, they can leave the line now.
this.advanceLine();

if ( this.numberOfRemainingKickableSoccerBallsProperty.value > 0 && this.nextBallToKickProperty.value === null ) {
if ( this.numberOfUnkickedBallsProperty.value > 0 && this.nextBallToKickProperty.value === null ) {
this.nextBallToKickProperty.value = this.getNextBallFromPool();
}
} );
Expand Down Expand Up @@ -391,7 +391,6 @@ export default class CAVModel implements TModel {
if ( frontPlayer ) {

if ( this.numberOfScheduledSoccerBallsToKickProperty.value > 0 &&
this.numberOfRemainingKickableSoccerBallsProperty.value > 0 &&
this.timeProperty.value >= this.timeWhenLastBallWasKickedProperty.value + TIME_BETWEEN_RAPID_KICKS ) {

if ( this.nextBallToKickProperty.value === null ) {
Expand Down Expand Up @@ -511,7 +510,7 @@ export default class CAVModel implements TModel {
* Adds the provided number of balls to the scheduled balls to kick
*/
public scheduleKicks( numberOfBallsToKick: number ): void {
this.numberOfScheduledSoccerBallsToKickProperty.value += Math.min( numberOfBallsToKick, this.numberOfRemainingKickableSoccerBallsProperty.value );
this.numberOfScheduledSoccerBallsToKickProperty.value += Math.min( numberOfBallsToKick, this.numberOfUnkickedBallsProperty.value );
}

/**
Expand Down Expand Up @@ -560,11 +559,11 @@ export default class CAVModel implements TModel {
}

private getNextBallFromPool(): CAVObject | null {
const newVar = this.soccerBallGroup.find( ball => !ball.isActiveProperty.value ) || null;
if ( newVar ) {
newVar.isActiveProperty.value = true;
const nextBallFromPool = this.soccerBallGroup.find( ball => !ball.isActiveProperty.value ) || null;
if ( nextBallFromPool ) {
nextBallFromPool.isActiveProperty.value = true;
}
return newVar;
return nextBallFromPool;
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/common/view/KickButtonGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class KickButtonGroup extends VBox {

const multiKickProperty = new NumberProperty( 5 );
const kick5PatternStringProperty = new PatternStringProperty( CenterAndVariabilityStrings.kickValuePatternStringProperty, { value: multiKickProperty } );
model.numberOfRemainingKickableSoccerBallsProperty.link( numberOfRemainingKickableObjects => {
model.numberOfUnkickedBallsProperty.link( numberOfRemainingKickableObjects => {
const value = Math.max( Math.min( numberOfRemainingKickableObjects, 5 ), 1 );
multiKickProperty.value = value;
} );
Expand Down

0 comments on commit d682ac3

Please sign in to comment.