Skip to content

Commit

Permalink
IOTypes for Particle subclasses, #231
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed May 3, 2024
1 parent 9aa02a7 commit 52f604e
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 20 deletions.
39 changes: 36 additions & 3 deletions js/common/model/HeavyParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,53 @@
import gasProperties from '../../gasProperties.js';
import GasPropertiesColors from '../GasPropertiesColors.js';
import GasPropertiesConstants from '../GasPropertiesConstants.js';
import Particle from './Particle.js';
import Particle, { ParticleOptions, ParticleStateObject } from './Particle.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';

type SelfOptions = EmptySelfOptions;

type HeavyParticleOptions = SelfOptions &
StrictOmit<ParticleOptions, 'mass' | 'radius' | 'colorProperty' | 'highlightColorProperty'>;

export type HeavyParticleStateObject = ParticleStateObject;

export default class HeavyParticle extends Particle {

public constructor() {
super( {
public constructor( providedOptions?: HeavyParticleOptions ) {

const options = optionize<HeavyParticleOptions, SelfOptions, ParticleOptions>()( {

// ParticleOptions
mass: GasPropertiesConstants.HEAVY_PARTICLES_MASS,
radius: GasPropertiesConstants.HEAVY_PARTICLES_RADIUS,
colorProperty: GasPropertiesColors.heavyParticleColorProperty,
highlightColorProperty: GasPropertiesColors.heavyParticleHighlightColorProperty
}, providedOptions );

super( options );
}

/**
* Deserializes an instance of HeavyParticle.
*/
private static fromStateObject( stateObject: HeavyParticleStateObject ): HeavyParticle {
return new HeavyParticle( {
x: stateObject.x,
y: stateObject.y,
previousX: stateObject.previousX,
previousY: stateObject.previousY,
vx: stateObject.vx,
vy: stateObject.vy
} );
}

public static readonly HeavyParticleIO = new IOType<HeavyParticle, HeavyParticleStateObject>( 'HeavyParticleIO', {
valueType: Particle,
stateSchema: Particle.STATE_SCHEMA,
fromStateObject: HeavyParticle.fromStateObject
} );
}

gasProperties.register( 'HeavyParticle', HeavyParticle );
39 changes: 36 additions & 3 deletions js/common/model/LightParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,53 @@
import gasProperties from '../../gasProperties.js';
import GasPropertiesColors from '../GasPropertiesColors.js';
import GasPropertiesConstants from '../GasPropertiesConstants.js';
import Particle from './Particle.js';
import Particle, { ParticleOptions, ParticleStateObject } from './Particle.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import IOType from '../../../../tandem/js/types/IOType.js';

type SelfOptions = EmptySelfOptions;

type LightParticleOptions = SelfOptions &
StrictOmit<ParticleOptions, 'mass' | 'radius' | 'colorProperty' | 'highlightColorProperty'>;

export type LightParticleStateObject = ParticleStateObject;

export default class LightParticle extends Particle {

public constructor() {
super( {
public constructor( providedOptions?: LightParticleOptions ) {

const options = optionize<LightParticleOptions, SelfOptions, ParticleOptions>()( {

// ParticleOptions
mass: GasPropertiesConstants.LIGHT_PARTICLES_MASS,
radius: GasPropertiesConstants.LIGHT_PARTICLES_RADIUS,
colorProperty: GasPropertiesColors.lightParticleColorProperty,
highlightColorProperty: GasPropertiesColors.lightParticleHighlightColorProperty
}, providedOptions );

super( options );
}

/**
* Deserializes an instance of HeavyParticle.
*/
private static fromStateObject( stateObject: LightParticleStateObject ): LightParticle {
return new LightParticle( {
x: stateObject.x,
y: stateObject.y,
previousX: stateObject.previousX,
previousY: stateObject.previousY,
vx: stateObject.vx,
vy: stateObject.vy
} );
}

public static readonly LightParticleIO = new IOType<LightParticle, LightParticleStateObject>( 'LightParticleIO', {
valueType: Particle,
stateSchema: Particle.STATE_SCHEMA,
fromStateObject: LightParticle.fromStateObject
} );
}

gasProperties.register( 'LightParticle', LightParticle );
71 changes: 57 additions & 14 deletions js/common/model/Particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,42 @@ import Bounds2 from '../../../../dot/js/Bounds2.js';
import { ProfileColorProperty } from '../../../../scenery/js/imports.js';
import gasProperties from '../../gasProperties.js';
import GasPropertiesUtils from '../GasPropertiesUtils.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import optionize from '../../../../phet-core/js/optionize.js';

type SelfOptions = {
mass: number; // AMU
radius: number; // pm
x?: number;
y?: number;
previousX?: number;
previousY?: number;
vx?: number;
vy?: number;
colorProperty: ProfileColorProperty;
highlightColorProperty: ProfileColorProperty; // color for specular highlight
};

export type ParticleOptions = SelfOptions;

// This should match Particle.STATE_SCHEMA, but with JavaScript types.
export type ParticleStateObject = {
mass: number;
radius: number;
x: number;
y: number;
previousX: number;
previousY: number;
vx: number;
vy: number;
};

export default class Particle {

// These are settable in the Diffusion screen
protected _mass: number; // AMU
protected _radius: number; // pm

//TODO https://github.com/phetsims/gas-properties/issues/218 Can these be moved elsewhere?
public readonly colorProperty: ProfileColorProperty;
public readonly highlightColorProperty: ProfileColorProperty;

// (x,y) position of the particle, at the center of the particle
private _x: number;
private _y: number;
Expand All @@ -47,24 +63,51 @@ export default class Particle {
private _vx;
private _vy;

//TODO https://github.com/phetsims/gas-properties/issues/218 Can these be moved elsewhere?
public readonly colorProperty: ProfileColorProperty;
public readonly highlightColorProperty: ProfileColorProperty;

private _isDisposed: boolean;

// This should match ParticleStateObject, but with IOTypes.
protected static readonly STATE_SCHEMA = {
mass: NumberIO,
radius: NumberIO,
x: NumberIO,
y: NumberIO,
previousX: NumberIO,
previousY: NumberIO,
vx: NumberIO,
vy: NumberIO
};

protected constructor( providedOptions: ParticleOptions ) {

this._mass = providedOptions.mass;
this._radius = providedOptions.radius;
const options = optionize<ParticleOptions, SelfOptions>()( {

// SelfOptions
x: 0,
y: 0,
previousX: 0,
previousY: 0,
vx: 0,
vy: 0
}, providedOptions );

this._mass = options.mass;
this._radius = options.radius;

this.colorProperty = providedOptions.colorProperty;
this.highlightColorProperty = providedOptions.highlightColorProperty;
this._x = options.x;
this._y = options.y;

this._x = 0;
this._y = 0;
this._previousX = options.previousX;
this._previousY = options.previousY;

this._previousX = 0;
this._previousY = 0;
this._vx = options.vx;
this._vy = options.vy;

this._vx = 0;
this._vy = 0;
this.colorProperty = options.colorProperty;
this.highlightColorProperty = options.highlightColorProperty;

this._isDisposed = false;
}
Expand Down
22 changes: 22 additions & 0 deletions js/diffusion/model/DiffusionParticle1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import GasPropertiesColors from '../../common/GasPropertiesColors.js';
import gasProperties from '../../gasProperties.js';
import DiffusionParticle, { DiffusionParticleOptions } from './DiffusionParticle.js';
import Particle, { ParticleStateObject } from '../../common/model/Particle.js';
import IOType from '../../../../tandem/js/types/IOType.js';

type SelfOptions = EmptySelfOptions;

Expand All @@ -29,6 +31,26 @@ export default class DiffusionParticle1 extends DiffusionParticle {
highlightColorProperty: GasPropertiesColors.diffusionParticle1HighlightColorProperty
}, providedOptions ) );
}

/**
* Deserializes an instance of DiffusionParticle1.
*/
private static fromStateObject( stateObject: ParticleStateObject ): DiffusionParticle1 {
return new DiffusionParticle1( {
x: stateObject.x,
y: stateObject.y,
previousX: stateObject.previousX,
previousY: stateObject.previousY,
vx: stateObject.vx,
vy: stateObject.vy
} );
}

public static readonly DiffusionParticle1IO = new IOType<DiffusionParticle1, ParticleStateObject>( 'DiffusionParticle1IO', {
valueType: Particle,
stateSchema: Particle.STATE_SCHEMA,
fromStateObject: DiffusionParticle1.fromStateObject
} );
}

gasProperties.register( 'DiffusionParticle1', DiffusionParticle1 );
22 changes: 22 additions & 0 deletions js/diffusion/model/DiffusionParticle2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import GasPropertiesColors from '../../common/GasPropertiesColors.js';
import gasProperties from '../../gasProperties.js';
import DiffusionParticle, { DiffusionParticleOptions } from './DiffusionParticle.js';
import Particle, { ParticleStateObject } from '../../common/model/Particle.js';
import IOType from '../../../../tandem/js/types/IOType.js';

type SelfOptions = EmptySelfOptions;

Expand All @@ -29,6 +31,26 @@ export default class DiffusionParticle2 extends DiffusionParticle {
highlightColorProperty: GasPropertiesColors.diffusionParticle2HighlightColorProperty
}, providedOptions ) );
}

/**
* Deserializes an instance of DiffusionParticle1.
*/
private static fromStateObject( stateObject: ParticleStateObject ): DiffusionParticle2 {
return new DiffusionParticle2( {
x: stateObject.x,
y: stateObject.y,
previousX: stateObject.previousX,
previousY: stateObject.previousY,
vx: stateObject.vx,
vy: stateObject.vy
} );
}

public static readonly DiffusionParticle2IO = new IOType<DiffusionParticle2, ParticleStateObject>( 'DiffusionParticle2IO', {
valueType: Particle,
stateSchema: Particle.STATE_SCHEMA,
fromStateObject: DiffusionParticle2.fromStateObject
} );
}

gasProperties.register( 'DiffusionParticle2', DiffusionParticle2 );

0 comments on commit 52f604e

Please sign in to comment.