Skip to content

Commit

Permalink
Port ACVoltage to TypeScript, see #749
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Oct 14, 2021
1 parent 6652175 commit abf7b56
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
23 changes: 16 additions & 7 deletions js/model/ACVoltage.js → js/model/ACVoltage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,29 @@
*/

import NumberProperty from '../../../axon/js/NumberProperty.js';
import Property from '../../../axon/js/Property.js';
import Range from '../../../dot/js/Range.js';
import merge from '../../../phet-core/js/merge.js';
import MathSymbols from '../../../scenery-phet/js/MathSymbols.js';
import Tandem from '../../../tandem/js/Tandem.js';
import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import VoltageSource from './VoltageSource.js';
import Circuit from './Circuit.js';
import Vertex from './Vertex.js';
import VoltageSource, {VoltageSourceOptions} from './VoltageSource.js';

// constants

// The maximum amplitude of the oscillating voltage
const MAX_VOLTAGE = 120;

type ACVoltageOptions = {} & VoltageSourceOptions;

class ACVoltage extends VoltageSource {
private readonly maximumVoltageProperty: NumberProperty;
private readonly frequencyProperty: NumberProperty;
private readonly phaseProperty: NumberProperty;
private time: number;

/**
* @param {Vertex} startVertex - one of the battery vertices
Expand All @@ -28,26 +38,25 @@ class ACVoltage extends VoltageSource {
* @param {Tandem} tandem
* @param {Object} [options]
*/
constructor( startVertex, endVertex, internalResistanceProperty, tandem, options ) {
constructor( startVertex: Vertex, endVertex: Vertex, internalResistanceProperty: Property<number>, tandem: Tandem, options?: Partial<ACVoltageOptions> ) {
assert && assert( internalResistanceProperty, 'internalResistanceProperty should be defined' );
options = merge( {
const filledOptions = merge( {
initialOrientation: 'right',
voltage: 9.0,
isFlammable: true,
numberOfDecimalPlaces: 1,
voltagePropertyOptions: {
range: new Range( -MAX_VOLTAGE, MAX_VOLTAGE )
}
}, options );
}, options ) as ACVoltageOptions;
super( startVertex, endVertex, internalResistanceProperty, CCKCConstants.BATTERY_LENGTH, tandem, options );

// @public {NumberProperty} - the maximum voltage, which can be controlled by the CircuitElementNumberControl
this.maximumVoltageProperty = new NumberProperty( options.voltage, {
this.maximumVoltageProperty = new NumberProperty( filledOptions.voltage, {
tandem: tandem.createTandem( 'maximumVoltageProperty' ),
range: new Range( 0, MAX_VOLTAGE )
} );


// @public {NumberProperty} - the frequency of oscillation in Hz
this.frequencyProperty = new NumberProperty( 0.5, {
tandem: tandem.createTandem( 'frequencyProperty' ),
Expand Down Expand Up @@ -92,7 +101,7 @@ class ACVoltage extends VoltageSource {
* @param {Circuit} circuit
* @public
*/
step( time, dt, circuit ) {
step( time:number, dt:number, circuit: Circuit ) {
super.step( time, dt, circuit );
this.time = time;
this.voltageProperty.set(
Expand Down
12 changes: 5 additions & 7 deletions js/model/Battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import {VoltageSourceOptions} from './VoltageSource.js';
// constants
const BATTERY_LENGTH = CCKCConstants.BATTERY_LENGTH;

type BatteryOptions = {
initialOrientation: string // TODO: enum
} & VoltageSourceOptions;
type BatteryOptions = {} & VoltageSourceOptions;

class Battery extends VoltageSource {
batteryType: BatteryType;
private readonly batteryType: BatteryType;

/**
* @param {Vertex} startVertex - one of the battery vertices
Expand All @@ -38,20 +36,20 @@ class Battery extends VoltageSource {
*/
constructor( startVertex: Vertex, endVertex: Vertex, internalResistanceProperty: Property<number>, batteryType: BatteryType, tandem: Tandem, options?: Partial<BatteryOptions> ) {
assert && assert( internalResistanceProperty, 'internalResistanceProperty should be defined' );
options = merge( {
const filledOptions = merge( {
initialOrientation: 'right',
voltage: 9.0,
isFlammable: true,
numberOfDecimalPlaces: batteryType === 'normal' ? 1 : 0,
voltagePropertyOptions: {
range: batteryType === 'normal' ? new Range( 0, 120 ) : new Range( 100, 100000 )
}
}, options );
}, options ) as BatteryOptions;
super( startVertex, endVertex, internalResistanceProperty, BATTERY_LENGTH, tandem, options );

// @public (read-only) {string} - track which way the battery "button" (plus side) was facing the initial state so
// the user can only create a certain number of "left" or "right" batteries from the toolbox.
this.initialOrientation = options.initialOrientation;
this.initialOrientation = filledOptions.initialOrientation;

// @public (read-only) {BatteryType} - the type of the battery - NORMAL | HIGH_VOLTAGE
this.batteryType = batteryType;
Expand Down
6 changes: 3 additions & 3 deletions js/model/VoltageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type VoltageSourceOptions = {
};

class VoltageSource extends FixedCircuitElement {
voltageProperty: NumberProperty;
internalResistanceProperty: Property<number>;
initialOrientation: any;
protected readonly voltageProperty: NumberProperty;
private readonly internalResistanceProperty: Property<number>;
protected initialOrientation: string; // TODO: enum

/**
* @param {Vertex} startVertex - one of the battery vertices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CCKCDemoScreenView extends ScreenView {
/**
* @param {Object} [options]
*/
constructor( options ) {
constructor( options?: object ) {
super( options );

// Classical zig-zag shape
Expand Down

0 comments on commit abf7b56

Please sign in to comment.