Skip to content

Commit

Permalink
Convert readonly properties to truly be readonly with IReadOnlyProper…
Browse files Browse the repository at this point in the history
…ty. See #75.
  • Loading branch information
Luisav1 committed Dec 13, 2021
1 parent fa95908 commit db39237
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
23 changes: 15 additions & 8 deletions js/game/model/CountingGameLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ import PlayObjectType from '../../../../counting-common/js/common/model/PlayObje
import dotRandom from '../../../../dot/js/dotRandom.js';
import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import numberPlayStrings from '../../numberPlayStrings.js';
import IReadOnlyProperty from '../../../../axon/js/IReadOnlyProperty.js';
import Enumeration from '../../../../phet-core/js/Enumeration.js';

// constants
const LEVEL_INPUT_RANGE = 10;

class CountingGameLevel extends NumberPlayGameLevel {

public readonly objectsPlayArea: OnesPlayArea;
public readonly playObjectTypeProperty: EnumerationProperty;
public readonly isObjectsRepresentationProperty: BooleanProperty;
private readonly _playObjectTypeProperty: EnumerationProperty;
public readonly playObjectTypeProperty: IReadOnlyProperty<Enumeration>;
private readonly _isObjectsRepresentationProperty: BooleanProperty;
public readonly isObjectsRepresentationProperty: IReadOnlyProperty<boolean>;
public readonly groupObjects: boolean;

constructor( levelNumber: number ) {
Expand All @@ -42,11 +46,14 @@ class CountingGameLevel extends NumberPlayGameLevel {
} );

// the object type of the current challenge
this.playObjectTypeProperty = new EnumerationProperty( PlayObjectType, CountingGameLevel.getRandomPlayObjectType() );
// TODO-TS: Use updated enumeration pattern for Property when PlayObjectType is converted. See https://github.com/phetsims/number-play/issues/80
this._playObjectTypeProperty = new EnumerationProperty( PlayObjectType, CountingGameLevel.getRandomPlayObjectType() );
this.playObjectTypeProperty = this._playObjectTypeProperty;

// whether the current representation of the challengeNumber are objects. Always use objects as the first representation
// of the current challenge
this.isObjectsRepresentationProperty = new BooleanProperty( true );
this._isObjectsRepresentationProperty = new BooleanProperty( true );
this.isObjectsRepresentationProperty = this._isObjectsRepresentationProperty;
}

/**
Expand All @@ -60,8 +67,8 @@ class CountingGameLevel extends NumberPlayGameLevel {

public reset(): void {
super.reset();
this.playObjectTypeProperty.reset();
this.isObjectsRepresentationProperty.reset();
this._playObjectTypeProperty.reset();
this._isObjectsRepresentationProperty.reset();
}

public step( dt: number ): void {
Expand All @@ -72,8 +79,8 @@ class CountingGameLevel extends NumberPlayGameLevel {
*/
public newChallenge(): void {
super.newChallenge();
this.playObjectTypeProperty.value = CountingGameLevel.getRandomPlayObjectType();
this.isObjectsRepresentationProperty.value = !this.isObjectsRepresentationProperty.value;
this._playObjectTypeProperty.value = CountingGameLevel.getRandomPlayObjectType();
this._isObjectsRepresentationProperty.value = !this._isObjectsRepresentationProperty.value;
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/game/model/NumberPlayGameLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class NumberPlayGameLevel {
public readonly scoreProperty: NumberProperty;
public readonly isChallengeSolvedProperty: BooleanProperty;
public readonly challengeRange: Range;
public readonly challengeNumberProperty: NumberProperty;
public readonly challengeNumberProperty: NumberProperty; // TODO-TS: This should be IReadOnlyProperty. See https://github.com/phetsims/number-play/issues/81.
private oldChallengeNumberOne: number;
private oldChallengeNumberTwo: number;
public readonly numberOfAnswerButtonPressesProperty: NumberProperty;
Expand Down
28 changes: 17 additions & 11 deletions js/game/model/Subitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,21 @@ class Subitizer {
private readonly challengeNumberProperty: NumberProperty;
private readonly isChallengeSolvedProperty: BooleanProperty;
public readonly isShapeVisibleProperty: BooleanProperty;
public readonly pointsProperty: Property<Vector2[]>;
private readonly _pointsProperty: Property<Vector2[]>;
public readonly pointsProperty: IReadOnlyProperty<Vector2[]>;
private readonly randomOrPredetermined: boolean;
private timeSinceShapeVisible: number;
public readonly objectSize: number;
public readonly isInputEnabledProperty: BooleanProperty;
private timeToShowShapeProperty: IReadOnlyProperty<number>;
public readonly objectTypeProperty: Property<SubitizeObjectTypeEnum>;
private readonly _objectTypeProperty: Property<SubitizeObjectTypeEnum>;
public readonly objectTypeProperty: IReadOnlyProperty<SubitizeObjectTypeEnum>;
private isDelayStarted: boolean;
private timeSinceDelayStarted: number;
public readonly isLoadingBarAnimatingProperty: BooleanProperty;
public static SUBITIZER_BOUNDS: Bounds2;
public readonly isPlayButtonVisibleProperty: BooleanProperty;
private readonly _isPlayButtonVisibleProperty: BooleanProperty;
public readonly isPlayButtonVisibleProperty: IReadOnlyProperty<boolean>;

constructor( challengeNumberProperty: NumberProperty,
isChallengeSolvedProperty: BooleanProperty,
Expand All @@ -148,7 +151,8 @@ class Subitizer {
this.isChallengeSolvedProperty = isChallengeSolvedProperty;

// whether the play button is visible
this.isPlayButtonVisibleProperty = new BooleanProperty( true );
this._isPlayButtonVisibleProperty = new BooleanProperty( true );
this.isPlayButtonVisibleProperty = this._isPlayButtonVisibleProperty;

// whether the loading bar is animating. This can also be used to stop an existing animation.
this.isLoadingBarAnimatingProperty = new BooleanProperty( false );
Expand All @@ -157,10 +161,11 @@ class Subitizer {
this.isShapeVisibleProperty = new BooleanProperty( false );

// the points of the current shape
this.pointsProperty = new Property( [ Vector2.ZERO ], {
this._pointsProperty = new Property( [ Vector2.ZERO ], {
valueType: Array,
arrayElementType: Vector2
} );
this.pointsProperty = this._pointsProperty;

// if true, make random or predetermined shapes. if false, only make arranged shapes.
this.randomOrPredetermined = randomOrPredetermined;
Expand All @@ -183,7 +188,8 @@ class Subitizer {
this.isInputEnabledProperty = new BooleanProperty( false );

// the object type of the current shape
this.objectTypeProperty = new StringEnumerationProperty( SubitizeObjectTypeValues, 'dog' );
this._objectTypeProperty = new StringEnumerationProperty( SubitizeObjectTypeValues, 'dog' );
this.objectTypeProperty = this._objectTypeProperty;

// how long the shape is visible when shown, in seconds. This is a derived Property instead of a constant because
// the time that the shape is shown is increased if the user gets the answer wrong multiple times.
Expand Down Expand Up @@ -256,7 +262,7 @@ class Subitizer {
this.isLoadingBarAnimatingProperty.reset();
this.resetDelay();
this.resetShapeVisible();
this.isPlayButtonVisibleProperty.reset();
this._isPlayButtonVisibleProperty.reset();
this.isInputEnabledProperty.reset();
}
}
Expand Down Expand Up @@ -298,8 +304,8 @@ class Subitizer {
`${challengeNumber}: ${points.length}` );

// two of the same shapes in a row are not allowed
if ( !Subitizer.arePointsEqual( this.pointsProperty.value, points ) ) {
this.pointsProperty.value = points;
if ( !Subitizer.arePointsEqual( this._pointsProperty.value, points ) ) {
this._pointsProperty.value = points;
}
else {
this.setNewPoints();
Expand All @@ -310,13 +316,13 @@ class Subitizer {
* Sets this.objectTypeProperty with a new object type for the current challenge.
*/
private setRandomPlayObjectType(): void {
this.objectTypeProperty.value = dotRandom.sample( SubitizeObjectTypeValues.slice() );
this._objectTypeProperty.value = dotRandom.sample( SubitizeObjectTypeValues.slice() );
}

public reset(): void {
this.isShapeVisibleProperty.reset();
this.isInputEnabledProperty.reset();
this.isPlayButtonVisibleProperty.reset();
this._isPlayButtonVisibleProperty.reset();
}

/**
Expand Down

0 comments on commit db39237

Please sign in to comment.