Skip to content

Commit

Permalink
Link to numberOfPeopleProperty, see: #52
Browse files Browse the repository at this point in the history
  • Loading branch information
marlitas committed Aug 30, 2022
1 parent 83050c8 commit f08f5a9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
14 changes: 13 additions & 1 deletion js/leveling-out/model/Chocolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,34 @@ import Range from '../../../../dot/js/Range.js';
import meanShareAndBalance from '../../meanShareAndBalance.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import Property from '../../../../axon/js/Property.js';

type SelfOptions = {
position: Vector2;
isActive: boolean;
};

type ChocolateOptions = SelfOptions & PhetioObjectOptions & PickRequired<PhetioObjectOptions, 'tandem'>;

export default class Chocolate {

public readonly position: Vector2;
public readonly chocolateBarsNumberProperty: NumberProperty;
public readonly chocolateBarsNumberProperty: Property<number>;
public readonly isActiveProperty: Property<boolean>;


public constructor( providedOptions: ChocolateOptions ) {
const options = optionize<ChocolateOptions, EmptySelfOptions, PhetioObjectOptions>()( {}, providedOptions );

this.isActiveProperty = new BooleanProperty( options.isActive, {
// phet-io
tandem: options.tandem.createTandem( 'isActiveProperty' ),

// Takes its value from LevelingOutModel.numberOfPeopleProperty
phetioReadOnly: true
} );

this.position = options.position;

this.chocolateBarsNumberProperty = new NumberProperty( 1, {
Expand Down
10 changes: 10 additions & 0 deletions js/leveling-out/model/LevelingOutModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class LevelingOutModel extends MeanShareAndBalanceModel {
for ( let i = 0; i < MAX_PEOPLE; i++ ) {
const x = i * MeanShareAndBalanceConstants.PERSON_WIDTH;
const chocolate = new Chocolate( {
isActive: i < this.numberOfPeopleProperty.value,
position: new Vector2( x, MeanShareAndBalanceConstants.PLATE_CHOCOLATE_CENTER_Y ),
tandem: options.tandem.createTandem( `plateChocolate${i}` )
} );
Expand All @@ -69,6 +70,15 @@ export default class LevelingOutModel extends MeanShareAndBalanceModel {
} );
this.peopleArray.push( person );
}

this.numberOfPeopleProperty.link( numberOfPeople => {
this.plateChocolateArray.forEach( ( plateChocolate, i ) => {
plateChocolate.isActiveProperty.value = i < numberOfPeople;
} );
this.peopleArray.forEach( ( person, i ) => {
person.isActiveProperty.value = i < numberOfPeople;
} );
} );
}

public override reset(): void {
Expand Down
11 changes: 6 additions & 5 deletions js/leveling-out/view/ChocolateBarsContainerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import chocolateBar_png from '../../../images/chocolateBar_png.js';
type ChocolateBarsContainerNodeOptions = StrictOmit<NodeOptions, keyof NodeTranslationOptions>;

export default class ChocolateBarsContainerNode extends Node {
public constructor( chocolateModel: Chocolate, providedOptions?: ChocolateBarsContainerNodeOptions ) {
public constructor( chocolate: Chocolate, providedOptions?: ChocolateBarsContainerNodeOptions ) {
const options = optionize<ChocolateBarsContainerNodeOptions, EmptySelfOptions, NodeOptions>()( {
x: chocolateModel.position.x,
y: chocolateModel.position.y,
scale: 0.2
x: chocolate.position.x,
y: chocolate.position.y,
scale: 0.1,
visibleProperty: chocolate.isActiveProperty
}, providedOptions );

const chocolateBars = [];
for ( let i = 0; i < chocolateModel.chocolateBarsNumberProperty.value; i++ ) {
for ( let i = 0; i < chocolate.chocolateBarsNumberProperty.value; i++ ) {
const chocolateBar = new Image( chocolateBar_png );
chocolateBar.y = ( MeanShareAndBalanceConstants.CHOCOLATE_HEIGHT + 5 ) * i;
chocolateBars.push( chocolateBar );
Expand Down
3 changes: 2 additions & 1 deletion js/leveling-out/view/PersonNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default class PersonNode extends GridBox {
super( {
children: [ personImage, chocolatesNode, numberSpinnerAlignBox ],
x: person.position.x,
centerY: person.position.y
centerY: person.position.y,
visibleProperty: person.isActiveProperty
} );
}
}
Expand Down

0 comments on commit f08f5a9

Please sign in to comment.