Skip to content

Commit

Permalink
use delegation pattern in Vector3IO, #100
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Mar 25, 2020
1 parent c5c7732 commit 8228c0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
25 changes: 25 additions & 0 deletions js/Vector3.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,11 +781,24 @@ inherit( Object, Vector3, {
return this.setXYZ( dot.Utils.roundSymmetric( this.x ),
dot.Utils.roundSymmetric( this.y ),
dot.Utils.roundSymmetric( this.z ) );
},

/**
* Returns a duck-typed object meant for use with tandem/phet-io serialization.
* @public
*
* @returns {{x:number, y:number, z:number}}
*/
toStateObject: function() {
return { x: this.x, y: this.y, z: this.z };
}
}, {
// static methods

/**
* Spherical linear interpolation between two unit vectors.
* @public
* @static
*
* @param {Vector3} start - Start unit vector
* @param {Vector3} end - End unit vector
Expand All @@ -795,6 +808,18 @@ inherit( Object, Vector3, {
slerp: function( start, end, ratio ) {
// NOTE: we can't create a require() loop here
return dot.Quaternion.slerp( new dot.Quaternion(), dot.Quaternion.getRotationQuaternion( start, end ), ratio ).timesVector3( start );
},

/**
* Constructs a Vector3 from a duck-typed object, for use with tandem/phet-io deserialization.
* @public
* @static
*
* @param {{x:number, y:number, z:number}} stateObject
* @returns {Vector3}
*/
fromStateObject: function( stateObject ) {
return new Vector3( stateObject.x, stateObject.y, stateObject.z);
}
} );

Expand Down
6 changes: 4 additions & 2 deletions js/Vector3IO.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ class Vector3IO extends ObjectIO {
* Encodes a Vector3 instance to a state.
* @param {Vector3} vector3
* @returns {Object}
* @public
*/
static toStateObject( vector3 ) {
validate( vector3, this.validator );
return { x: vector3.x, y: vector3.y, z: vector3.z };
return vector3.toStateObject();
}

/**
* Decodes a state into a Vector3.
* @param {Object} stateObject
* @returns {Vector3}
* @public
*/
static fromStateObject( stateObject ) {
return new Vector3( stateObject.x, stateObject.y, stateObject.z );
return Vector3.fromStateObject( stateObject.x, stateObject.y, stateObject.z );
}
}

Expand Down

0 comments on commit 8228c0c

Please sign in to comment.