Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonLi8 committed Aug 26, 2020
1 parent 6bc615c commit a7ee218
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 2 additions & 3 deletions js/common/model/BallState.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
* - Pauses the sim.
* - Sets the elapsed time to 0.
* - Sets the Balls' position, mass, and velocity to their most recent saved BallState. Their restart BallState
* is saved when the user finishes controlling one of the Balls.
* - Only balls currently in the BallSystem are restarted. Only balls in the BallSystem that are fully inside
* the PlayArea's bounds have their states saved.
* is saved when the user finishes controlling one of the Balls. However, if any of the balls are outside
* the PlayArea's bounds, the states are not saved. See https://github.com/phetsims/collision-lab/issues/163.
*
* @author Brandon Li
*/
Expand Down
8 changes: 4 additions & 4 deletions js/common/model/BallSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,20 @@ class BallSystem {
// Observe when Balls are added to the system and save the states of all balls in the system. Listener lasts for the
// life-time of the simulation since BallSystems are never disposed.
this.balls.elementAddedEmitter.addListener( ball => {
this.balls.forEach( ball => ball.insidePlayAreaProperty.value && ball.saveState() );
this.balls.every( ball => ball.insidePlayAreaProperty.value ) && this.balls.forEach( ball => ball.saveState() );
} );

// Observe when the user is done controlling any of the Balls to:
// 1. Save the states of all Balls that are both in the system and fully inside the PlayArea's bounds.
// 1. Save the states of all Balls if every ball is inside the PlayArea's bounds.
// 2. Clear the trailing Paths of all Balls and the Path of the CenterOfMass.
// 3. Reset the rotation of Balls relative to their centers.
//
// Link lasts for the life-time of the sim as BallSystems are never disposed.
this.ballSystemUserControlledProperty.lazyLink( ballSystemUserControlled => {
if ( !ballSystemUserControlled ) {
if ( !ballSystemUserControlled && this.balls.every( ball => ball.insidePlayAreaProperty.value ) ) {
this.balls.forEach( ball => {

// Save the state of each Ball in the BallSystem that is fully inside the PlayArea.
// Save the state of each Ball.
ball.insidePlayAreaProperty.value && ball.saveState();
ball.path.clear();
ball.rotationProperty.reset();
Expand Down

0 comments on commit a7ee218

Please sign in to comment.