Skip to content

Commit

Permalink
Factor out SoccerModel and improve tandem names, see #6
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Feb 4, 2022
1 parent 58a53e1 commit 3be0499
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 56 deletions.
7 changes: 5 additions & 2 deletions js/common/model/CASModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type CASModelSelfOptions = {};
export type CASModelOptions = CASModelSelfOptions & PhetioObjectOptions & Required<Pick<PhetioObjectOptions, 'tandem'>>; // TODO: Do we like the inline exports?

class CASModel {
objectGroup: PhetioGroup<CASObject>;
readonly objectGroup: PhetioGroup<CASObject>;
readonly objectType: CASObjectType;

constructor( objectType: CASObjectType, providedOptions: CASModelOptions ) {

Expand All @@ -29,6 +30,8 @@ class CASModel {
tandem: Tandem.REQUIRED
}, providedOptions );

this.objectType = objectType;

// @public {PhetioGroup}
this.objectGroup = new PhetioGroup( ( tandem, options ) => {

Expand All @@ -39,7 +42,7 @@ class CASModel {
return new CASObject( options );
}, [ { objectType: objectType } ], {
phetioType: PhetioGroup.PhetioGroupIO( CASObject.CASObjectIO ),
tandem: options.tandem.createTandem( 'objectGroup' )
tandem: options.tandem.createTandem( objectType === CASObjectType.SOCCER_BALL ? 'soccerBallGroup' : 'dataPointGroup' )
} );
}

Expand Down
47 changes: 47 additions & 0 deletions js/common/model/SoccerModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022, University of Colorado Boulder

/**
* TODO Describe this class and its responsibilities.
*
* @author Chris Klusendorf (PhET Interactive Simulations)
* @author Sam Reid (PhET Interactive Simulations)
*/

import optionize from '../../../../phet-core/js/optionize.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import centerAndSpread from '../../centerAndSpread.js';
import CASModel, { CASModelOptions } from '../../common/model/CASModel.js';
import CASObjectType from '../../common/model/CASObjectType.js';

type SoccerModelSelfOptions = {};
type SoccerModelOptions = SoccerModelSelfOptions & CASModelOptions;

class SoccerModel extends CASModel {

constructor( options: SoccerModelOptions ) {

options = optionize<SoccerModelOptions, SoccerModelSelfOptions, CASModelOptions>( {

// phet-io options
tandem: Tandem.REQUIRED
}, options );

super( CASObjectType.SOCCER_BALL, options );
}

/**
* Resets the model.
*/
reset() {
}

/**
* Steps the model.
* @param dt - time step, in seconds
*/
step( dt: number ) {
}
}

centerAndSpread.register( 'SoccerModel', SoccerModel );
export default SoccerModel;
8 changes: 6 additions & 2 deletions js/common/view/CASObjectNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import ball_png from '../../../images/ball_png.js';
import ShadedSphereNode from '../../../../scenery-phet/js/ShadedSphereNode.js';

type CASObjectNodeSelfOptions = {};
export type CASObjectNodeOptions = CASObjectNodeSelfOptions & NodeOptions;
export type CASObjectNodeOptions = CASObjectNodeSelfOptions & NodeOptions & Required<Pick<NodeOptions, 'tandem'>>;

class CASObjectNode extends Node {

constructor( casObject: CASObject, modelViewTransform: ModelViewTransform2, providedOptions?: CASObjectNodeOptions ) {
constructor( casObject: CASObject, modelViewTransform: ModelViewTransform2, providedOptions: CASObjectNodeOptions ) {
const options = optionize<CASObjectNodeOptions>( {
phetioDynamicElement: true
}, providedOptions );
Expand All @@ -32,6 +32,10 @@ class CASObjectNode extends Node {
this.addChild( casObject.objectType === CASObjectType.SOCCER_BALL ? new Image( ball_png ) :
new ShadedSphereNode( casObject.radius * 2 ) );

this.addLinkedElement( casObject, {
tandem: options.tandem.createTandem( casObject.objectType === CASObjectType.SOCCER_BALL ? 'soccerBall' : 'dataPoint' )
} );

casObject.positionProperty.link( position => {
this.center = modelViewTransform.modelToViewPosition( position );
} );
Expand Down
3 changes: 2 additions & 1 deletion js/common/view/CASScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ModelViewTransform2 from '../../../../phetcommon/js/view/ModelViewTransfo
import PhetioGroup from '../../../../tandem/js/PhetioGroup.js';
import CASObjectNode from './CASObjectNode.js';
import { Node } from '../../../../scenery/js/imports.js';
import CASObjectType from '../model/CASObjectType.js';

export type CASScreenViewOptions = ScreenViewOptions;

Expand All @@ -44,7 +45,7 @@ class CASScreenView extends ScreenView {
return new CASObjectNode( casObject, modelViewTransform, options );
}, [ model.objectGroup.archetype, modelViewTransform, {} ], {
phetioType: PhetioGroup.PhetioGroupIO( Node.NodeIO ),
tandem: options.tandem.createTandem( 'objectNodeGroup' ),
tandem: options.tandem.createTandem( model.objectType === CASObjectType.SOCCER_BALL ? 'soccerBallNodeGroup' : 'dataPointNodeGroup' ),
supportsDynamicState: false
} );

Expand Down
21 changes: 4 additions & 17 deletions js/mean-and-median/model/MeanAndMedianModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import optionize from '../../../../phet-core/js/optionize.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import centerAndSpread from '../../centerAndSpread.js';
import CASModel, { CASModelOptions } from '../../common/model/CASModel.js';
import CASObjectType from '../../common/model/CASObjectType.js';
import { CASModelOptions } from '../../common/model/CASModel.js';
import SoccerModel from '../../common/model/SoccerModel.js';

type MedianModelSelfOptions = {};
type MedianModelOptions = MedianModelSelfOptions & CASModelOptions;

class MeanAndMedianModel extends CASModel {
class MeanAndMedianModel extends SoccerModel {

constructor( options: MedianModelOptions ) {

Expand All @@ -26,20 +26,7 @@ class MeanAndMedianModel extends CASModel {
tandem: Tandem.REQUIRED
}, options );

super( CASObjectType.SOCCER_BALL, options );
}

/**
* Resets the model.
*/
reset() {
}

/**
* Steps the model.
* @param dt - time step, in seconds
*/
step( dt: number ) {
super( options );
}
}

Expand Down
21 changes: 4 additions & 17 deletions js/median/model/MedianModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import optionize from '../../../../phet-core/js/optionize.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import centerAndSpread from '../../centerAndSpread.js';
import CASModel, { CASModelOptions } from '../../common/model/CASModel.js';
import CASObjectType from '../../common/model/CASObjectType.js';
import { CASModelOptions } from '../../common/model/CASModel.js';
import SoccerModel from '../../common/model/SoccerModel.js';

type MedianModelSelfOptions = {};
type MedianModelOptions = MedianModelSelfOptions & CASModelOptions;

class MedianModel extends CASModel {
class MedianModel extends SoccerModel {

constructor( options: MedianModelOptions ) {

Expand All @@ -26,20 +26,7 @@ class MedianModel extends CASModel {
tandem: Tandem.REQUIRED
}, options );

super( CASObjectType.SOCCER_BALL, options );
}

/**
* Resets the model.
*/
reset() {
}

/**
* Steps the model.
* @param dt - time step, in seconds
*/
step( dt: number ) {
super( options );
}
}

Expand Down
21 changes: 4 additions & 17 deletions js/spread/model/SpreadModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import optionize from '../../../../phet-core/js/optionize.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import centerAndSpread from '../../centerAndSpread.js';
import CASModel, { CASModelOptions } from '../../common/model/CASModel.js';
import CASObjectType from '../../common/model/CASObjectType.js';
import { CASModelOptions } from '../../common/model/CASModel.js';
import SoccerModel from '../../common/model/SoccerModel.js';

type SpreadModelSelfOptions = {};
type SpreadModelOptions = SpreadModelSelfOptions & CASModelOptions;

class SpreadModel extends CASModel {
class SpreadModel extends SoccerModel {

constructor( options: SpreadModelOptions ) {

Expand All @@ -26,20 +26,7 @@ class SpreadModel extends CASModel {
tandem: Tandem.REQUIRED
}, options );

super( CASObjectType.SOCCER_BALL, options );
}

/**
* Resets the model.
*/
reset() {
}

/**
* Steps the model.
* @param dt - time step, in seconds
*/
step( dt: number ) {
super( options );
}
}

Expand Down

0 comments on commit 3be0499

Please sign in to comment.