Skip to content

Commit

Permalink
Update PhET-iO serialization for Tracks, #385
Browse files Browse the repository at this point in the history
Also:
* Update Spline Typescript Type
* Remove unused "parents" for Track
  • Loading branch information
zepumph committed Jan 14, 2025
1 parent edfe5be commit c965ff2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 128 deletions.
4 changes: 3 additions & 1 deletion js/common/SplineEvaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

import energySkatePark from '../energySkatePark.js';

type Spline = {
// https://github.com/sloisel/numeric/blob/master/src/numeric.js#L2423
export type Spline = {
x: number[];
yl: number[];
yr: number[];
kl: number[];
kr: number[];
diff(): Spline;
};

// The most important function for this sim in numeric.js is just too slow because it uses tensor versions of all functions.
Expand Down
2 changes: 1 addition & 1 deletion js/common/model/DebugTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class DebugTracks {
public static init( model: EnergySkateParkModel ): void {

const createPoint = ( x: number, y: number ) => model.controlPointGroup.createNextElement( x, y, {} );
const createTrack = ( controlPoints: ControlPoint[], options: TrackOptions = {} ) => model.trackGroup.createNextElement( controlPoints, [], options );
const createTrack = ( controlPoints: ControlPoint[], options: TrackOptions = {} ) => model.trackGroup.createNextElement( controlPoints, options );
// Tracks to help demonstrate issues

let controlPoints = null;
Expand Down
16 changes: 8 additions & 8 deletions js/common/model/EnergySkateParkModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class EnergySkateParkModel extends PhetioObject {
// TODO: https://github.com/phetsims/energy-skate-park/issues/123 the control point group doesn't have enough archetypes to
// TODO: create an archetype track, https://github.com/phetsims/energy-skate-park/issues/123
// group of tracks
public readonly trackGroup: PhetioGroup<Track, [ ControlPoint[], Track[], TrackOptions ]>;
public readonly trackGroup: PhetioGroup<Track, [ ControlPoint[], TrackOptions ]>;

// Temporary flag that keeps track of whether the track was changed in the step before the physics
// update. True if the skater's track is being dragged by the user, so that energy conservation no longer applies.
Expand Down Expand Up @@ -229,13 +229,13 @@ export default class EnergySkateParkModel extends PhetioObject {
phetioDynamicElementName: 'controlPoint'
} );

this.trackGroup = new PhetioGroup<Track, [ ControlPoint[], Track[], TrackOptions ]>( ( tandem, controlPoints, parents, options ) => {
this.trackGroup = new PhetioGroup<Track, [ ControlPoint[], TrackOptions ]>( ( tandem, controlPoints, options ) => {
assert && options && assert( !options.hasOwnProperty( 'tandem' ), 'tandem is managed by the PhetioGroup' );
return new Track( this, controlPoints, parents, merge( {}, options, {
return new Track( this, controlPoints, merge( {}, options, {
tandem: tandem,
phetioDynamicElement: true
} ) );
}, [ _.range( 20 ).map( n => this.controlPointGroup.createNextElement( n * 100, 0, {} ) ), [], {
}, [ _.range( 20 ).map( n => this.controlPointGroup.createNextElement( n * 100, 0, {} ) ), {
draggable: true,
configurable: true
} ], {
Expand Down Expand Up @@ -1550,7 +1550,7 @@ export default class EnergySkateParkModel extends PhetioObject {
const controlPointToDelete = track.controlPoints[ controlPointIndex ];
const points = _.without( track.controlPoints, controlPointToDelete );
this.controlPointGroup.disposeElement( controlPointToDelete );
const newTrack = this.trackGroup.createNextElement( points, track.getParentsOrSelf(), Track.FULLY_INTERACTIVE_OPTIONS );
const newTrack = this.trackGroup.createNextElement( points, Track.FULLY_INTERACTIVE_OPTIONS );
newTrack.physicalProperty.value = true;
newTrack.droppedProperty.value = true;

Expand Down Expand Up @@ -1605,10 +1605,10 @@ export default class EnergySkateParkModel extends PhetioObject {
points1.push( newPoint1 );
points2.unshift( newPoint2 );

const newTrack1 = this.trackGroup.createNextElement( points1, track.getParentsOrSelf(), Track.FULLY_INTERACTIVE_OPTIONS );
const newTrack1 = this.trackGroup.createNextElement( points1, Track.FULLY_INTERACTIVE_OPTIONS );
newTrack1.physicalProperty.value = true;
newTrack1.droppedProperty.value = true;
const newTrack2 = this.trackGroup.createNextElement( points2, track.getParentsOrSelf(), Track.FULLY_INTERACTIVE_OPTIONS );
const newTrack2 = this.trackGroup.createNextElement( points2, Track.FULLY_INTERACTIVE_OPTIONS );
newTrack2.physicalProperty.value = true;
newTrack2.droppedProperty.value = true;

Expand Down Expand Up @@ -1699,7 +1699,7 @@ export default class EnergySkateParkModel extends PhetioObject {
secondTrackBackward();
}

const newTrack = this.trackGroup.createNextElement( points, a.getParentsOrSelf().concat( b.getParentsOrSelf() ), Track.FULLY_INTERACTIVE_OPTIONS );
const newTrack = this.trackGroup.createNextElement( points, Track.FULLY_INTERACTIVE_OPTIONS );
newTrack.physicalProperty.value = true;
newTrack.droppedProperty.value = true;

Expand Down
5 changes: 1 addition & 4 deletions js/common/model/PremadeTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import ControlPoint from './ControlPoint.js';
import EnergySkateParkModel from './EnergySkateParkModel.js';
import Track from './Track.js';

// constants
const PARENT_TRACKS: Track[] = [];

// limiting bounds for dragging control points
const END_BOUNDS_WIDTH = 2.5;
const END_BOUNDS_HEIGHT = 4;
Expand Down Expand Up @@ -298,7 +295,7 @@ const PremadeTracks = {
* Create a track from the provided control points.
*/
createTrack( model: EnergySkateParkModel, controlPoints: ControlPoint[], options: IntentionalAny ): Track {
return new Track( model, controlPoints, PARENT_TRACKS, options );
return new Track( model, controlPoints, options );
},

TrackType: TrackType as IntentionalAny
Expand Down
Loading

0 comments on commit c965ff2

Please sign in to comment.