Skip to content

Commit

Permalink
replace static methods with clone(), #80
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Feb 24, 2023
1 parent e983009 commit fafd98f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
15 changes: 5 additions & 10 deletions js/common/model/Substance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,12 @@ export default class Substance {
}

/**
* Creates a shallow copy of a substance. AXON.Property observers are not copied.
* Creates a shallow copy of this Substance. AXON.Property observers are not copied.
* @param quantity - optional quantity, to override this.quantityProperty.value
*/
public static clone( substance: Substance ): Substance {
return new Substance( substance.coefficientProperty.value, substance.symbol, substance.iconProperty.value, substance.quantityProperty.value );
}

/**
* Creates a shallow copy of a substance with a specified quantity. AXON.Property observers are not copied.
*/
public static withQuantity( substance: Substance, quantity: number ): Substance {
return new Substance( substance.coefficientProperty.value, substance.symbol, substance.iconProperty.value, quantity );
public clone( quantity?: number ): Substance {
return new Substance( this.coefficientProperty.value, this.symbol, this.iconProperty.value,
( quantity === undefined ) ? this.quantityProperty.value : 0 );
}
}

Expand Down
7 changes: 3 additions & 4 deletions js/game/model/GameGuess.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

import BoxType from '../../common/model/BoxType.js';
import Substance from '../../common/model/Substance.js';
import reactantsProductsAndLeftovers from '../../reactantsProductsAndLeftovers.js';

export default class GameGuess {
Expand All @@ -35,19 +34,19 @@ export default class GameGuess {
// @public Clone reactants, quantities are initialized to zero for 'Before' challenges.
this.reactants = [];
reaction.reactants.forEach( reactant => {
this.reactants.push( ( interactiveBox === BoxType.BEFORE ) ? Substance.withQuantity( reactant, 0 ) : Substance.clone( reactant ) );
this.reactants.push( ( interactiveBox === BoxType.BEFORE ) ? reactant.clone( 0 ) : reactant.clone() );
} );

// @public Clone products, quantities are initialized to zero for 'After' challenges.
this.products = [];
reaction.products.forEach( product => {
this.products.push( ( interactiveBox === BoxType.AFTER ) ? Substance.withQuantity( product, 0 ) : Substance.clone( product ) );
this.products.push( ( interactiveBox === BoxType.AFTER ) ? product.clone( 0 ) : product.clone() );
} );

// @public Clone leftovers, quantities are initialized to zero for 'After' challenges.
this.leftovers = [];
reaction.leftovers.forEach( leftover => {
this.leftovers.push( ( interactiveBox === BoxType.AFTER ) ? Substance.withQuantity( leftover, 0 ) : Substance.clone( leftover ) );
this.leftovers.push( ( interactiveBox === BoxType.AFTER ) ? leftover.clone( 0 ) : leftover.clone() );
} );

assert && assert( this.reactants.length === reaction.reactants.length );
Expand Down

0 comments on commit fafd98f

Please sign in to comment.