-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CASObject and SoccerBall/Node, see #6
- Loading branch information
Showing
6 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2022, University of Colorado Boulder | ||
|
||
/** | ||
* Base class for a manipulable data point which could be a soccer ball or, in the lab screen, a colored sphere. | ||
* | ||
* @author Chris Klusendorf (PhET Interactive Simulations) | ||
* @author Sam Reid (PhET Interactive Simulations) | ||
*/ | ||
|
||
import centerAndSpread from '../../centerAndSpread.js'; | ||
import Vector2Property from '../../../../dot/js/Vector2Property.js'; | ||
import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js'; | ||
import Tandem from '../../../../tandem/js/Tandem.js'; | ||
import Vector2 from '../../../../dot/js/Vector2.js'; | ||
import optionize from '../../../../phet-core/js/optionize.js'; | ||
|
||
type CASObjectSelfOptions = { | ||
initialPosition?: Vector2, | ||
radius?: number | ||
}; | ||
export type CASObjectOptions = | ||
CASObjectSelfOptions | ||
& PhetioObjectOptions | ||
& Required<Pick<PhetioObjectOptions, 'tandem'>>; | ||
|
||
class CASObject extends PhetioObject { | ||
readonly positionProperty: Vector2Property; // in model coordinates | ||
readonly radius: number; | ||
|
||
constructor( providedOptions: CASObjectOptions ) { | ||
|
||
const options = optionize<CASObjectOptions, CASObjectSelfOptions, PhetioObjectOptions>( { | ||
initialPosition: Vector2.ZERO, | ||
radius: 15, | ||
|
||
// phet-io options | ||
tandem: Tandem.REQUIRED | ||
}, providedOptions ); | ||
|
||
super( options ); | ||
|
||
this.radius = options.radius; | ||
|
||
this.positionProperty = new Vector2Property( options.initialPosition, { | ||
tandem: options.tandem.createTandem( 'positionProperty' ) | ||
} ); | ||
} | ||
|
||
reset() { | ||
this.positionProperty.reset(); | ||
} | ||
} | ||
|
||
centerAndSpread.register( 'CASObject', CASObject ); | ||
export default CASObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2022, University of Colorado Boulder | ||
|
||
/** | ||
* Soccer balls are animated and hence track their velocity. | ||
* | ||
* @author Chris Klusendorf (PhET Interactive Simulations) | ||
* @author Sam Reid (PhET Interactive Simulations) | ||
*/ | ||
|
||
import centerAndSpread from '../../centerAndSpread.js'; | ||
import Tandem from '../../../../tandem/js/Tandem.js'; | ||
import optionize from '../../../../phet-core/js/optionize.js'; | ||
import NumberProperty from '../../../../axon/js/NumberProperty.js'; | ||
import CASObject, { CASObjectOptions } from './CASObject.js'; | ||
|
||
type SoccerBallSelfOptions = {}; | ||
export type SoccerBallOptions = SoccerBallSelfOptions & CASObjectOptions; | ||
|
||
class SoccerBall extends CASObject { | ||
private readonly velocityProperty: NumberProperty; | ||
|
||
constructor( providedOptions: SoccerBallOptions ) { | ||
|
||
const options = optionize<SoccerBallOptions, SoccerBallSelfOptions>( { | ||
|
||
// phet-io options | ||
tandem: Tandem.REQUIRED | ||
}, providedOptions ); | ||
|
||
super( options ); | ||
|
||
this.velocityProperty = new NumberProperty( 0, { | ||
tandem: options.tandem.createTandem( 'velocityProperty' ) | ||
} ); | ||
} | ||
|
||
/** | ||
* Resets the model. | ||
*/ | ||
reset() { | ||
super.reset(); | ||
this.velocityProperty.reset(); | ||
} | ||
} | ||
|
||
centerAndSpread.register( 'SoccerBall', SoccerBall ); | ||
export default SoccerBall; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022, University of Colorado Boulder | ||
|
||
/** | ||
* Base class which renders a Node for the CASObject. | ||
* | ||
* @author Chris Klusendorf (PhET Interactive Simulations) | ||
* @author Sam Reid (PhET Interactive Simulations) | ||
*/ | ||
|
||
import optionize from '../../../../phet-core/js/optionize.js'; | ||
import centerAndSpread from '../../centerAndSpread.js'; | ||
import { Node, NodeOptions } from '../../../../scenery/js/imports.js'; | ||
import CASObject from '../model/CASObject.js'; | ||
import ModelViewTransform2 from '../../../../phetcommon/js/view/ModelViewTransform2.js'; | ||
|
||
type CASObjectNodeSelfOptions = { | ||
contentNode: Node | ||
}; | ||
export type CASObjectNodeOptions = CASObjectNodeSelfOptions & NodeOptions; | ||
|
||
class CASObjectNode extends Node { | ||
|
||
constructor( casObject: CASObject, modelViewTransform: ModelViewTransform2, providedOptions: CASObjectNodeOptions ) { | ||
const options = optionize<CASObjectNodeOptions>( {}, providedOptions ); | ||
super( options ); | ||
|
||
this.maxWidth = modelViewTransform.modelToViewDeltaX( casObject.radius * 2 ); | ||
this.maxHeight = this.maxWidth; | ||
this.addChild( options.contentNode ); | ||
|
||
casObject.positionProperty.link( position => { | ||
this.center = modelViewTransform.modelToViewPosition( position ); | ||
} ); | ||
} | ||
} | ||
|
||
centerAndSpread.register( 'CASObjectNode', CASObjectNode ); | ||
export default CASObjectNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2022, University of Colorado Boulder | ||
|
||
/** | ||
* Renders a soccer ball using a raster image. | ||
* | ||
* @author Chris Klusendorf (PhET Interactive Simulations) | ||
* @author Sam Reid (PhET Interactive Simulations) | ||
*/ | ||
|
||
import optionize from '../../../../phet-core/js/optionize.js'; | ||
import centerAndSpread from '../../centerAndSpread.js'; | ||
import { Image } from '../../../../scenery/js/imports.js'; | ||
import CASObject from '../model/CASObject.js'; | ||
import CASObjectNode, { CASObjectNodeOptions } from './CASObjectNode.js'; | ||
import ball_png from '../../../images/ball_png.js'; | ||
import ModelViewTransform2 from '../../../../phetcommon/js/view/ModelViewTransform2.js'; | ||
|
||
type SoccerBallNodeSelfOptions = {}; | ||
export type SoccerBallNodeOptions = SoccerBallNodeSelfOptions & Omit<CASObjectNodeOptions, 'contentNode'>; | ||
|
||
class SoccerBallNode extends CASObjectNode { | ||
constructor( casObject: CASObject, modelViewTransform: ModelViewTransform2, providedOptions: SoccerBallNodeOptions ) { | ||
|
||
// TODO: Why do we have to specify 'contentNode'? | ||
const options = optionize<SoccerBallNodeOptions, SoccerBallNodeSelfOptions, CASObjectNodeOptions, 'contentNode'>( { | ||
contentNode: new Image( ball_png ) | ||
}, providedOptions ); | ||
super( casObject, modelViewTransform, options ); | ||
} | ||
} | ||
|
||
centerAndSpread.register( 'SoccerBallNode', SoccerBallNode ); | ||
export default SoccerBallNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters