Skip to content

Commit

Permalink
Add model snapshots, see #96
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jan 18, 2020
1 parent d45139a commit 8a1da52
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions js/common/model/Plank.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define( require => {
const MassForceVector = require( 'BALANCING_ACT/common/model/MassForceVector' );
const Matrix3 = require( 'DOT/Matrix3' );
const NumberIO = require( 'TANDEM/types/NumberIO' );
const ObjectIO = require( 'TANDEM/types/ObjectIO' );
const ObservableArray = require( 'AXON/ObservableArray' );
const Property = require( 'AXON/Property' );
const Shape = require( 'KITE/Shape' );
Expand Down Expand Up @@ -67,15 +68,17 @@ define( require => {
tandem: tandem.createTandem( 'massDroppedOnPlankEmitter' ),
parameters: [
{ name: 'mass', phetioType: NumberIO },
{ name: 'position', phetioType: NumberIO } ]
{ name: 'position', phetioType: NumberIO },
{ name: 'fullState', phetioType: ObjectIO } ]
} );

// @private - signify in the data stream when masses are placed and removed
this.massRemovedFromPlankEmitter = new Emitter( {
tandem: tandem.createTandem( 'massRemovedFromPlankEmitter' ),
parameters: [
{ name: 'mass', phetioType: NumberIO },
{ name: 'position', phetioType: NumberIO } ]
{ name: 'position', phetioType: NumberIO },
{ name: 'fullState', phetioType: ObjectIO } ]
} );

// Variables that need to be retained for dynamic behavior, but are not
Expand Down Expand Up @@ -193,7 +196,13 @@ define( require => {
};
this.massDistancePairs.push( result );

this.massDroppedOnPlankEmitter.emit( mass.massValue, result.distance );
const fullState = this.massDistancePairs.map( massDistancePair => {
return {
mass: massDistancePair.mass.massValue,
distance: massDistancePair.distance
};
} );
this.massDroppedOnPlankEmitter.emit( mass.massValue, result.distance, fullState );

// Add the force vector for this mass.
this.forceVectors.push( new MassForceVector( mass ) );
Expand Down Expand Up @@ -261,11 +270,22 @@ define( require => {
// Remove the mass.
this.massesOnSurface.remove( mass );


// Remove the mass-distance pair for this mass.
for ( let i = 0; i < this.massDistancePairs.length; i++ ) {
if ( this.massDistancePairs[ i ].mass === mass ) {
this.massRemovedFromPlankEmitter.emit( mass.massValue, this.massDistancePairs[ i ].distance );

const distance = this.massDistancePairs[ i ].distance;
this.massDistancePairs.splice( i, 1 );

const fullState = this.massDistancePairs.map( massDistancePair => {
return {
mass: massDistancePair.mass.massValue,
distance: massDistancePair.distance
};
} );
this.massRemovedFromPlankEmitter.emit( mass.massValue, distance, fullState );

break;
}
}
Expand Down

0 comments on commit 8a1da52

Please sign in to comment.