Skip to content

Commit

Permalink
add lab screen (duplicated of application screen right now), phetsims…
Browse files Browse the repository at this point in the history
…/buoyancy#113

Signed-off-by: Michael Kauzmann <[email protected]>
  • Loading branch information
zepumph committed Mar 5, 2024
1 parent 9a917f6 commit 30235bf
Show file tree
Hide file tree
Showing 2 changed files with 507 additions and 0 deletions.
135 changes: 135 additions & 0 deletions js/buoyancy/model/BuoyancyLabModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2019-2024, University of Colorado Boulder

/**
* The main model for the Lab screen of the Buoyancy simulation.
*
* @author Michael Kauzmann (PhET Interactive Simulations)
* @author Jonathan Olson <[email protected]>
*/

import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import EnumerationProperty from '../../../../axon/js/EnumerationProperty.js';
import Property from '../../../../axon/js/Property.js';
import Matrix3 from '../../../../dot/js/Matrix3.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Cube from '../../common/model/Cube.js';
import DensityBuoyancyModel, { DensityBuoyancyModelOptions } from '../../common/model/DensityBuoyancyModel.js';
import Material from '../../common/model/Material.js';
import Scale, { DisplayType } from '../../common/model/Scale.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import Boat from './Boat.js';
import Bottle from './Bottle.js';
import { Scene } from './BuoyancyApplicationsModel.js';

export type BuoyancyLabModelOptions = DensityBuoyancyModelOptions;

export default class BuoyancyLabModel extends DensityBuoyancyModel {

public readonly sceneProperty: Property<Scene>;
public readonly densityExpandedProperty: Property<boolean>;

public readonly bottle: Bottle;
public readonly block: Cube;
public override boat: Boat;
public readonly scale1: Scale; // Scale sitting to the right of the pool

public constructor( options: BuoyancyLabModelOptions ) {

const tandem = options.tandem;

super( options );

this.sceneProperty = new EnumerationProperty( Scene.BOTTLE, {
tandem: options.tandem.createTandem( 'sceneProperty' )
} );
this.densityExpandedProperty = new BooleanProperty( false, {
tandem: tandem.createTandem( 'densityExpandedProperty' )
} );

this.bottle = new Bottle( this.engine, {
matrix: Matrix3.translation( 0, 0 ),
tandem: tandem.createTandem( 'bottle' ),
visible: true
} );
this.availableMasses.push( this.bottle );

this.block = Cube.createWithVolume( this.engine, Material.BRICK, new Vector2( 0.5, 0.5 ), 0.001, {
visible: false,
tandem: tandem.createTandem( 'block' )
} );
this.availableMasses.push( this.block );

// DerivedProperty doesn't need disposal, since everything here lives for the lifetime of the simulation
this.boat = new Boat( this.engine, new DerivedProperty( [ this.block.sizeProperty ], size => size.depth ), this.liquidMaterialProperty, {
matrix: Matrix3.translation( 0, -0.1 ),
tandem: tandem.createTandem( 'boat' ),
visible: false
} );
this.availableMasses.push( this.boat );

this.scale1 = new Scale( this.engine, this.gravityProperty, {
matrix: Matrix3.translation( 0.77, -Scale.SCALE_BASE_BOUNDS.minY ),
displayType: DisplayType.NEWTONS,
tandem: tandem.createTandem( 'scale1' ),
canMove: false,
inputEnabledPropertyOptions: {
phetioReadOnly: false
}
} );
this.availableMasses.push( this.scale1 );

// Adjust pool volume so that it's at the desired value WITH the pool scale inside.
this.pool.liquidVolumeProperty.setInitialValue( this.pool.liquidVolumeProperty.value );

// This instance lives for the lifetime of the simulation, so we don't need to remove this listener
this.sceneProperty.link( scene => {
this.bottle.internalVisibleProperty.value = scene === Scene.BOTTLE;
this.boat.internalVisibleProperty.value = scene === Scene.BOAT;
this.block.internalVisibleProperty.value = scene === Scene.BOAT;

assert && assert( !this.boat.visibleProperty.value || !this.bottle.visibleProperty.value,
'Boat and bottle should not be visible at the same time' );
} );
}

public override step( dt: number ): void {
assert && assert( !this.boat.visibleProperty.value || !this.bottle.visibleProperty.value,
'Boat and bottle should not be visible at the same time' );

super.step( dt );
}

/**
* Moves the boat and block to their initial locations (see https://github.com/phetsims/buoyancy/issues/25)
*/
public resetBoatScene(): void {
// Reset the basin levels (clear the liquid out of the boat)
this.boat.basin.reset();
this.pool.reset();

// Move things to the initial position
this.boat.resetPosition();
this.block.resetPosition();
}

/**
* Resets things to their original values.
*/
public override reset(): void {
this.densityExpandedProperty.reset();

this.bottle.reset();
this.block.reset();
this.boat.reset();

super.reset();

this.sceneProperty.reset();

assert && assert( !this.boat.visibleProperty.value || !this.bottle.visibleProperty.value,
'Boat and bottle should not be visible at the same time' );
}
}

densityBuoyancyCommon.register( 'BuoyancyLabModel', BuoyancyLabModel );
Loading

0 comments on commit 30235bf

Please sign in to comment.