Skip to content

Commit

Permalink
replace GamePhaseProperty with setGamePhase, #109
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jan 21, 2019
1 parent a5965d3 commit a996b40
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 62 deletions.
98 changes: 39 additions & 59 deletions js/linegame/model/BaseGameModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define( function( require ) {
var StringProperty = require( 'AXON/StringProperty' );

// constants
var INITIAL_GAME_PHASE = GamePhase.SETTINGS;
var CHALLENGES_PER_GAME = 6;
var DUMMY_CHALLENGE = new GraphTheLine( '', Line.createSlopeIntercept( 1, 1, 1 ),
EquationForm.SLOPE_INTERCEPT, ManipulationMode.SLOPE, GLConstants.X_AXIS_RANGE, GLConstants.Y_AXIS_RANGE );
Expand Down Expand Up @@ -81,31 +82,8 @@ define( function( require ) {
this.bestTimeProperties.push( new Property( null ) ); // null if a level has no best time yet
}

// @public
this.gamePhaseProperty = new GamePhaseProperty( GamePhase.SETTINGS,
/*
* This function will be called prior to setting the Property value.
* Updates fields so that they are accurate before Property listeners are notified.
*/
function( gamePhase ) {
if ( gamePhase === GamePhase.SETTINGS ) {
self.playStateProperty.set( PlayState.NONE );
self.timer.stop();
}
else if ( gamePhase === GamePhase.PLAY ) {
self.initChallenges();
self.playStateProperty.set( PlayState.FIRST_CHECK );
self.scoreProperty.set( 0 );
self.timer.start();
}
else if ( gamePhase === GamePhase.RESULTS ) {
self.playStateProperty.set( PlayState.NONE );
self.updateBestTime();
}
else {
throw new Error( 'unsupported game phase: ' + gamePhase );
}
} );
// @public (read-only) {GamePhase} set this using setGamePhase
this.gamePhaseProperty = new Property( INITIAL_GAME_PHASE );

this.initChallenges();

Expand All @@ -128,7 +106,7 @@ define( function( require ) {

if ( isLastChallenge ) {
// game has been completed
self.gamePhaseProperty.set( GamePhase.RESULTS );
self.setGamePhase( GamePhase.RESULTS );
if ( score > self.bestScoreProperties[ level ].get() ) {
self.bestScoreProperties[ level ].set( score );
}
Expand All @@ -152,7 +130,40 @@ define( function( require ) {

graphingLines.register( 'BaseGameModel', BaseGameModel );

inherit( Object, BaseGameModel, {
return inherit( Object, BaseGameModel, {

/**
* Sets the game phase. Call this instead of setting gamePhaseProperty directly,
* because there are tasks that needs to be done before listeners are notified.
* @param {GamePhase} gamePhase
* @public
*/
setGamePhase: function( gamePhase ) {
if ( gamePhase !== this.gamePhaseProperty.get() ) {

// Do tasks that need to be done before notifying listeners.
if ( gamePhase === GamePhase.SETTINGS ) {
this.playStateProperty.set( PlayState.NONE );
this.timer.stop();
}
else if ( gamePhase === GamePhase.PLAY ) {
this.initChallenges();
this.playStateProperty.set( PlayState.FIRST_CHECK );
this.scoreProperty.set( 0 );
this.timer.start();
}
else if ( gamePhase === GamePhase.RESULTS ) {
this.playStateProperty.set( PlayState.NONE );
this.updateBestTime();
}
else {
throw new Error( 'unsupported game phase: ' + gamePhase );
}

// Change the Property, which notifies listeners
this.gamePhaseProperty.set( gamePhase );
}
},

// @override @public
reset: function() {
Expand All @@ -166,7 +177,7 @@ define( function( require ) {
this.challengesPerGameProperty.reset();
this.playStateProperty.reset();

this.gamePhaseProperty.reset();
this.setGamePhase( INITIAL_GAME_PHASE );
this.resetBestScores();
this.resetBestTimes();

Expand Down Expand Up @@ -278,35 +289,4 @@ define( function( require ) {
console.log( 'end: verify creation of challenges' );
}
} );

/**
* Property used for the game phase.
* It has a 'hook' function that is called before the value is changed.
* This is useful for setting the various state parameters of the game before
* notifying observes that the game phase has changed.
* @param {GamePhase} value
* @param {function} hook function with one parameter of type {GamePhase}
* @constructor
*/
class GamePhaseProperty extends Property {
constructor( value, hook ) {
super( value );
this.hook = hook; // @private
}

/**
* @param value
* @returns {Property}
* @public
* @override
*/
set( value ) {
this.hook( value );
return super.set( value );
}
}

graphingLines.register( 'BaseGameModel.GamePhaseProperty', GamePhaseProperty );

return BaseGameModel;
} );
2 changes: 1 addition & 1 deletion js/linegame/view/PlayNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ define( function( require ) {
xMargin: 10,
yMargin: 5,
listener: function() {
model.gamePhaseProperty.set( GamePhase.SETTINGS );
model.setGamePhase( GamePhase.SETTINGS );
}
}
} );
Expand Down
2 changes: 1 addition & 1 deletion js/linegame/view/ResultsNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ define( function( require ) {
model.bestTimeProperties[ model.levelProperty.get() ].get(),
model.isNewBestTime,
function() {
model.gamePhaseProperty.set( GamePhase.SETTINGS );
model.setGamePhase( GamePhase.SETTINGS );
}, {
starDiameter: 45,
buttonFill: LineGameConstants.BUTTON_COLOR,
Expand Down
2 changes: 1 addition & 1 deletion js/linegame/view/SettingsNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ define( function( require ) {
},
listener: function() {
model.levelProperty.set( level );
model.gamePhaseProperty.set( GamePhase.PLAY );
model.setGamePhase( GamePhase.PLAY );
}
} );
};
Expand Down

0 comments on commit a996b40

Please sign in to comment.